Home Libraries People FAQ More

##### 查找均值和標準差實例(Find mean and standard deviation example)

```#include <boost/math/distributions/normal.hpp> // normal_distribution
using boost::math::normal; // typedef 提供的缺省類型為 double.
#include <boost/math/distributions/cauchy.hpp> //  cauchy_distribution
using boost::math::cauchy; // typedef 提供的缺省類型為 double.
#include <boost/math/distributions/find_location.hpp>
using boost::math::find_location;
#include <boost/math/distributions/find_scale.hpp>
using boost::math::find_scale;
using boost::math::complement;
using boost::math::policies::policy;

#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;
```

##### 使用函數 find_location 和函數 find_scale 來滿足分配和測量規範( dispensing and measurement specifications)

"一台機器用於在每個盒子中裝3 kg牛肉(ground beef)。經過很長一段時間發現每個盒子中的牛肉平均重量為3kg且標準差為0.1kg。假定將牛肉裝進例子的過程為正態分佈。"

```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)) * 100. << endl; // P(X > 3.1)```

```double minimum_weight = 2.9;
cout <<"Fraction of packs <= " << minimum_weight << " with a mean of " << mean
<< " is " << cdf(complement(packs, minimum_weight)) << endl;
// fraction of packs <= 2.9 with a mean of 3 is 0.841345```

```double over_mean = 3.06449;
normal xpacks(over_mean, standard_deviation);
cout << "Fraction of packs >= " << minimum_weight
<< " with a mean of " << xpacks.mean()
<< " is " << cdf(complement(xpacks, minimum_weight)) << endl;
// fraction of packs >= 2.9 with a mean of 3.06449 is 0.950005```

```double under_fraction = 0.05;  //  95% 的盒子超過最小重量均值 - sd = 2.9
double low_limit = standard_deviation;
double offset = mean - low_limit - quantile(packs, under_fraction);
double nominal_mean = mean + offset;
// mean + (mean - low_limit - quantile(packs, under_fraction));

normal nominal_packs(nominal_mean, standard_deviation);
cout << "Setting the packer to " << nominal_mean << " will mean that "
<< "fraction of packs >= " << minimum_weight
<< " is " << cdf(complement(nominal_packs, minimum_weight)) << endl;
// 設置盒子重量為 3.06449 將意味著 >= 2.9 的盒子的百分比為 0.95```

```#include <boost/math/distributions/find_location.hpp>
using boost::math::find_location;```

```double safe_mean = find_location<normal>(minimum_weight, under_fraction, standard_deviation);
normal good_packs(safe_mean, standard_deviation);```

```cout << "Setting the packer to " << nominal_mean << " will mean that "
<< "fraction of packs >= " << minimum_weight
<< " is " << cdf(complement(good_packs, minimum_weight)) << endl;
// 設置盒子重量為 3.06449 將意味著 >= 2.9 的盒子的百分比為 0.95```

##### 使用柯西－洛倫茨分佈取代正態分佈(Using Cauchy-Lorentz instead of normal distribution)

```cauchy cpacks(mean, standard_deviation);
cout << "Cauchy Setting the packer to " << mean << " will mean that "
<< "fraction of packs >= " << minimum_weight
<< " is " << cdf(complement(cpacks, minimum_weight)) << endl;
// 設置盒子重量為 3 將意味著 >= 2.9 的盒子的百分比為 0.75```

```double lc = find_location<cauchy>(minimum_weight, under_fraction, standard_deviation);
cout << "find_location<cauchy>(minimum_weight, over fraction, standard_deviation); " << lc << endl;
// find_location<cauchy>(minimum_weight, over fraction, packs.standard_deviation()); 3.53138```

```cauchy goodcpacks(lc, standard_deviation);
cout << "Cauchy Setting the packer to " << lc << " will mean that "
<< "fraction of packs >= " << minimum_weight
<< " is " << cdf(complement(goodcpacks, minimum_weight)) << endl;
// 設置盒子重量為 3.53138 將意味著 >= 2.9 的盒子的百分比為 0.95```

```cout << "Cauchy Setting the packer to "
<< find_location<cauchy>(minimum_weight, 0.99, standard_deviation)
<< " will mean that "
<< "fraction of packs >= " << minimum_weight
<< " is " << cdf(complement(goodcpacks, minimum_weight)) << endl;```

##### 改變尺度或標準差(Changing the scale or standard deviation)

```double p = 0.05; // wanted p th quantile.
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;
// Quantile of 0.05 = 2.91776, mean = 3, sd = 0.05

cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean
<< " and standard deviation of " << pack05.standard_deviation()
<< " is " << cdf(complement(pack05, minimum_weight)) << endl;
// 重量大於 2.9 ，均值為 3，標準差為0.05的包的百分比為 0.97725```

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

cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean
<< " and standard deviation of " << pack06.standard_deviation()
<< " is " << cdf(complement(pack06, minimum_weight)) << endl;
// 重量大於 2.9 ，均值為 3，標準差為0.06的包的百分比為 0.95221```

```normal N01; // 均值為0且標準差為1的標準正態分佈.
p = 0.05;
double qp = quantile(N01, p);
double sd95 = (minimum_weight - mean) / qp;

cout << "For the "<< p << "th quantile to be located at "
<< minimum_weight << ", would need a standard deviation of " << sd95 << endl;
// 為了使 0.05th 分位點位於 2.9處， 需要的標準差為 0.0607957```

```normal pack95(mean, sd95);
cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean
<< " and standard deviation of " << pack95.standard_deviation()
<< " is " << cdf(complement(pack95, minimum_weight)) << endl;
// 重量大於 2.9 ，均值為 3，百分比為 0.95```

```double ss = find_scale<normal>(minimum_weight, under_fraction, packs.mean());
cout << "find_scale<normal>(minimum_weight, under_fraction, packs.mean()); " << ss << endl;
// find_scale<normal>(minimum_weight, under_fraction, packs.mean()); 0.0607957```

`double over_fraction = 0.95;`

```double sso = find_scale<normal>(minimum_weight, over_fraction, packs.mean());
```

```Message from thrown exception was:
Error in function boost::math::find_scale<Dist, Policy>(double, double, double, Policy):
Computed scale (-0.060795683191176959) is <= 0! Was the complement intended?
```

```double ss1o = find_scale<normal>(minimum_weight, 1 - over_fraction, packs.mean());
cout << "find_scale<normal>(minimum_weight, under_fraction, packs.mean()); " << ss1o << endl;
// find_scale<normal>(minimum_weight, under_fraction, packs.mean()); 0.0607957```

```double ssc = find_scale<normal>(complement(minimum_weight, over_fraction, packs.mean()));
cout << "find_scale<normal>(complement(minimum_weight, over_fraction, packs.mean())); " << ssc << endl;
// find_scale<normal>(complement(minimum_weight, over_fraction, packs.mean())); 0.0607957```

```normal pack95c(mean, ssc);
cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean
<< " and standard deviation of " << pack95c.standard_deviation()
<< " is " << cdf(complement(pack95c, minimum_weight)) << endl;
// Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.0607957 is 0.95```

• 為了滿足最小的規範，打包機是否過度填充(over-fill)，或者為了，避免過度填充，打包機是否過少填充(under-fill)?

• 我們可以測量得更好一些嗎？