Home Libraries People FAQ More

##### 查找尺度(標準差)實例(Find Scale Example)

```#include <boost/math/distributions/normal.hpp> // normal_distribution
using boost::math::normal; // typedef 提供的缺省類型為 double.
#include <boost/math/distributions/find_scale.hpp>
using boost::math::find_scale;
using boost::math::complement; // 如果我們想要使用補集版本( complement version ).
using boost::math::policies::policy; // 指定錯誤處理策略.

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

```normal N01;  // 缺省的 '標準' 正態分佈均值為0
double sd = 1.; // 標準差為 1.```

```double z = -2.; // z to give prob p
double p = 0.001; // only 0.1% below z = -2

cout << "Normal distribution with mean = " << N01.location()  // aka N01.mean()
<< ", standard deviation " << N01.scale() // aka N01.standard_deviation()
<< ", has " << "fraction <= " << z
<< ", p = "  << cdf(N01, z) << endl;
cout << "Normal distribution with mean = " << N01.location()
<< ", standard deviation " << N01.scale()
<< ", has " << "fraction > " << z
<< ", p = "  << cdf(complement(N01, z)) << endl; // Note: uses complement.```

```Normal distribution with mean = 0 has fraction <= -2, p = 0.0227501
Normal distribution with mean = 0 has fraction > -2, p = 0.97725
```

```double l = N01.location();
double s = find_scale<normal>(z, p, l);
cout << "scale (standard deviation) = " << s << endl;```

```scale (standard deviation) = 0.647201
```

`normal np001pc(N01.location(), s);`

```cout << "Normal distribution with mean = " << l
<< " has " << "fraction <= " << z
<< ", p = "  << cdf(np001pc, z) << endl;
cout << "Normal distribution with mean = " << l
<< " has " << "fraction > " << z
<< ", p = "  << cdf(complement(np001pc, z)) << endl;```

```Normal distribution with mean = 0 has fraction <= -2, p = 0.001
Normal distribution with mean = 0 has fraction > -2, p = 0.999
```

##### 控制函數 find_scale 中錯誤的處理方式

```using boost::math::policies::policy;
using boost::math::policies::domain_error;
using boost::math::policies::ignore_error;```

```typedef policy<domain_error<ignore_error> > ignore_domain_policy;
// find_scale 使用一個新的策略， 使用 typedef.
l = find_scale<normal>(z, p, l, ignore_domain_policy());
// Default policy policy<>, needs using boost::math::policies::policy;

l = find_scale<normal>(z, p, l, policy<>());
// 缺省策略，完全指定.
l = find_scale<normal>(z, p, l, boost::math::policies::policy<>());
// 新的策略，沒有使用 typedef.
l = find_scale<normal>(z, p, l, policy<domain_error<ignore_error> >());```

```z = -2.;
double q = 0.999; // = 1 - p; //  0.001的補集.
sd = find_scale<normal>(complement(z, q, l));

normal np95pc(l, sd); // 同樣的 standard_deviation  但調整均值mean
cout << "Normal distribution with mean = " << l << " has "
<< "fraction <= " << z << " = "  << cdf(np95pc, z) << endl;
cout << "Normal distribution with mean = " << l << " has "
<< "fraction > " << z << " = "  << cdf(complement(np95pc, z)) << endl;```

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

```Example: Find scale (standard deviation).
Normal distribution with mean = 0, standard deviation 1, has fraction <= -2, p = 0.0227501
Normal distribution with mean = 0, standard deviation 1, has fraction > -2, p = 0.97725
scale (standard deviation) = 0.647201
Normal distribution with mean = 0 has fraction <= -2, p = 0.001
Normal distribution with mean = 0 has fraction > -2, p = 0.999
Normal distribution with mean = 0.946339 has fraction <= -2 = 0.001
Normal distribution with mean = 0.946339 has fraction > -2 = 0.999
```