Home Libraries People FAQ More

##### 正態分佈的幾個例子

```#include <boost/math/distributions/normal.hpp> // normal_distribution
using boost::math::normal; // typedef 提供的缺省類型為 double.

#include <iostream>
using std::cout; using std::endl; using std::left; using std::showpoint; using std::noshowpoint;
#include <iomanip>
using std::setw; using std::setprecision;
#include <limits>
using std::numeric_limits;

int main()
{
cout << "Example: Normal distribution, Miscellaneous Applications.";

try
{
{ // Traditional tables and values.```

```double step = 1.; // in z
double range = 4; // min and max z = -range to +range.
int precision = 17; // traditional tables 僅計算到非常低的精度.

// Construct a standard normal distribution s
normal s; // (缺省均值 = 0，且標準差 = 1)
cout << "Standard normal distribution, mean = "<< s.mean()
<< ", standard deviation = " << s.standard_deviation() << endl;```

```cout << "Probability distribution function values" << endl;
cout << "  z " "      pdf " << endl;
cout.precision(5);
for (double z = -range; z < range + step; z += step)
{
cout << left << setprecision(3) << setw(6) << z << " "
<< setprecision(precision) << setw(12) << pdf(s, z) << endl;
}
cout.precision(6); // default```

```// 對於一個標準正態分佈( standard normal distribution )
cout << "Standard normal mean = "<< s.mean()
<< ", standard deviation = " << s.standard_deviation() << endl;
cout << "Integral (area under the curve) from - infinity up to z " << endl;
cout << "  z " "      cdf " << endl;
for (double z = -range; z < range + step; z += step)
{
cout << left << setprecision(3) << setw(6) << z << " "
<< setprecision(precision) << setw(12) << cdf(s, z) << endl;
}
cout.precision(6); // default```

```double z = 2.;
cout << "Area for z = " << z << " is " << cdf(s, z) << endl; // 獲取 z的區域.```

```  cout << "95% of area has a z below " << quantile(s, 0.95) << endl;
// 95% 的區域有一個小於 1.64485 的z值```

```  cout << "95% of area has a z between " << quantile(s, 0.975)
<< " and " << -quantile(s, 0.975) << endl;
// 95% 的區域有一個在 1.95996 和 -1.95996之間的z值```

```double alpha1 = cdf(s, -1) * 2; // 0.3173105078629142
cout << setprecision(17) << "Significance level for z == 1 is " << alpha1 << endl;```

```double alpha[] = {0.3173105078629142, // z for 1 standard deviation.
0.20, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };```

```cout << "level of significance (alpha)" << setprecision(4) << endl;
cout << "2-sided       1 -sided          z(alpha) " << endl;
for (int i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
{
cout << setw(15) << alpha[i] << setw(15) << alpha[i] /2 << setw(10) << quantile(complement(s,  alpha[i]/2)) << endl;
// 使用 quantile(complement(s, alpha[i]/2)) 來避免潛在的精度損失}
cout << endl;```

##### 均值的任一邊的標準差(Standard deviations either side of the Mean)

```cout.precision(3);
cout << showpoint << "cdf(s, s.standard_deviation()) = "
<< cdf(s, s.standard_deviation()) << endl;  // from -infinity to 1 sd
cout << "cdf(complement(s, s.standard_deviation())) = "
<< cdf(complement(s, s.standard_deviation())) << endl;
cout << "Fraction 1 standard deviation within either side of mean is "
<< 1 -  cdf(complement(s, s.standard_deviation())) * 2 << endl;
cout << "Fraction 2 standard deviations within either side of mean is "
<< 1 -  cdf(complement(s, 2 * s.standard_deviation())) * 2 << endl;
cout << "Fraction 3 standard deviations within either side of mean is "
<< 1 -  cdf(complement(s, 3 * s.standard_deviation())) * 2 << endl;```

```Fraction 1 standard deviation within either side of mean is 0.683
Fraction 2 standard deviations within either side of mean is 0.954
Fraction 3 standard deviations within either side of mean is 0.997
```

```Fraction 1 standard deviation within either side of mean is 0.682689492137086
Fraction 2 standard deviations within either side of mean is 0.954499736103642
Fraction 3 standard deviations within either side of mean is 0.997300203936740
```

##### 燈泡的壽命(Life of light bulbs)

```// K. Krishnamoorthy, Handbook of Statistical Distributions with Applications,
// ISBN 1 58488 635 8, page 125, example 10.3.5```

100 W 燈泡的平均壽命是 1100 h 標準差為 100 h。假定，或許是一點證據和更多的信心，分佈是正態的，我們使用這些值來構造一個稱之為bulbs 的正態分佈：

```double mean_life = 1100.;
double life_standard_deviation = 100.;
normal bulbs(mean_life, life_standard_deviation);
double expected_life = 1000.;```

```cout << "Fraction of bulbs that will last at best (<=) " // P(X <= 1000)
<< expected_life << " is "<< cdf(bulbs, expected_life) << endl;
cout << "Fraction of bulbs that will last at least (>) " // P(X > 1000)
<< expected_life << " is "<< cdf(complement(bulbs, expected_life)) << endl;
double min_life = 900;
double max_life = 1200;
cout << "Fraction of bulbs that will last between "
<< min_life << " and " << max_life << " is "
<< cdf(bulbs, max_life)  // P(X <= 1200)
- cdf(bulbs, min_life) << endl; // P(X <= 900)```

注意 Real-life failures are often very ab-normal, with a significant number that 'dead-on-arrival' or suffer failure very early in their life: the lifetime of the survivors of 'early mortality' may be well described by the normal distribution.

##### 需要多少個洋蔥?

```double mean = 140.; // 每週的袋數.
double standard_deviation = 10;
normal sacks(mean, standard_deviation);

double stock = 160.; // 每週.
cout << "Percentage of weeks overstocked "
<< cdf(sacks, stock) * 100. << endl; // P(X <=160)
// Percentage of weeks overstocked 97.7```

```double stock_95 = quantile(sacks, 0.95);
cout << "Store should stock " << int(stock_95) << " sacks to meet 95% of demands." << endl;```

```double stock_80 = quantile(sacks, 0.80);
cout << "Store should stock " << int(stock_80) << " sacks to meet 8 out of 10 demands." << endl;
```

##### 打包牛肉(Packing beef)

```double mean = 3.; // kg
double standard_deviation = 0.1; // kg
normal packs(mean, standard_deviation);

double max_weight = 3.1; // kg
cout << "Percentage of packs > " << max_weight << " is "
<< cdf(complement(packs, max_weight)) << endl; // P(X > 3.1)

double under_weight = 2.9;
cout <<"fraction of packs <= " << under_weight << " with a mean of " << mean
<< " is " << cdf(complement(packs, under_weight)) << endl;
// <= 2.9 且均值為 3 的百分比是 0.841345
// 也就是 0.84 - 多於目標的 0.95
// 要求 95% 的盒子超過這個重量，那麼我們應當將均值設為多少呢 ?
// KK StatCalc says:
double over_mean = 3.0664;
normal xpacks(over_mean, standard_deviation);
cout << "fraction of packs >= " << under_weight
<< " with a mean of " << xpacks.mean()
<< " is " << cdf(complement(xpacks, under_weight)) << endl;
// f>= 2.9 且均值為 3.06449 的百分比是 0.950005
double under_fraction = 0.05;  //  95% 超過這個重量的最小平均重量是 mean - sd = 2.9
double low_limit = standard_deviation;
double offset = mean - low_limit - quantile(packs, under_fraction);
double nominal_mean = mean + offset;

normal nominal_packs(nominal_mean, standard_deviation);
cout << "Setting the packer to " << nominal_mean << " will mean that "
<< "fraction of packs >= " << under_weight
<< " is " << cdf(complement(nominal_packs, under_weight)) << endl;```

```double p = 0.05; // 需要第 p 個分位點.
cout << "Quantile of " << p << " = " << quantile(packs, p)
<< ", mean = " << packs.mean() << ", sd = " << packs.standard_deviation() << endl; //```

Quantile of 0.05 = 2.83551, mean = 3, sd = 0.1

```normal pack05(mean, 0.05);
cout << "Quantile of " << p << " = " << quantile(pack05, p)
<< ", mean = " << pack05.mean() << ", sd = " << pack05.standard_deviation() << endl;

cout <<"Fraction of packs >= " << under_weight << " with a mean of " << mean
<< " and standard deviation of " << pack05.standard_deviation()
<< " is " << cdf(complement(pack05, under_weight)) << endl;
//```

>= 2.9 且均值為3 ，標準差為 0.05 的盒子的百分比是 0.9772

```normal pack06(mean, 0.06);
cout << "Quantile of " << p << " = " << quantile(pack06, p)
<< ", mean = " << pack06.mean() << ", sd = " << pack06.standard_deviation() << endl;

cout <<"Fraction of packs >= " << under_weight << " with a mean of " << mean
<< " and standard deviation of " << pack06.standard_deviation()
<< " is " << cdf(complement(pack06, under_weight)) << endl;```

```normal s; // 標準正態分佈(standard normal distribution),
double sd = 0.1;
double x = 2.9; // 我們要求的界限.
// 那麼概率 p = N((x - mean) / sd)
// 如果我們想要找到滿足要求的標準差,
// 使得第 p 個分位點位於 x,
// 在這種開發部下是 0.95 (95%) 分位點在 2.9 kg 處, 當均值為 3 kg.

double prob =  pdf(s, (x - mean) / sd);
double qp = quantile(s, 0.95);
cout << "prob = " << prob << ", quantile(p) " << qp << endl; // p = 0.241971, quantile(p) 1.64485
// 重新整理，我們可以直接計算要求的標準差:
double sd95 = abs((x - mean)) / qp;

cout << "If we want the "<< p << " th quantile to be located at "
<< x << ", would need a standard deviation of " << sd95 << endl;

normal pack95(mean, sd95);  // Distribution of the 'ideal better' packer.
cout <<"Fraction of packs >= " << under_weight << " with a mean of " << mean
<< " and standard deviation of " << pack95.standard_deviation()
<< " is " << cdf(complement(pack95, under_weight)) << endl;

// 重量大於 2.9 ，均值為 3的包的百分比為 0.95```

##### 螺栓的長度(Length of bolts)

```    normal bolts(3.95, 0.1);
double top = 4.1;
double bottom = 3.9;

cout << "Fraction long enough [ P(X <= " << top << ") ] is " << cdf(bolts, top) << endl;
cout << "Fraction too short [ P(X <= " << bottom << ") ] is " << cdf(bolts, bottom) << endl;
cout << "Fraction OK  -between " << bottom << " and " << top
<< "[ P(X <= " << top  << ") - P(X<= " << bottom << " ) ] is "
<< cdf(bolts, top) - cdf(bolts, bottom) << endl;

cout << "Fraction too long [ P(X > " << top << ") ] is "
<< cdf(complement(bolts, top)) << endl;

cout << "95% of bolts are shorter than " << quantile(bolts, 0.95) << endl;

```