Home Libraries People FAQ More

##### 查找位置(均值)( Find Location )實例

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

cout << "Normal distribution with mean = " << N01.location()
<< ", standard deviation " << N01.scale()
<< ", 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, standard deviation 1, has fraction <= -2, p = 0.0227501
Normal distribution with mean = 0, standard deviation 1, has fraction > -2, p = 0.97725
```

```double l = find_location<normal>(z, p, sd);
cout << "offset location (mean) = " << l << endl;```

```offset location (mean) = 1.09023
```

`normal np001pc(l, sd); // 同相的 standard_deviation (scale) 但均值改變.`

```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 = 1.09023 has fraction <= -2, p = 0.001
Normal distribution with mean = 1.09023 has fraction > -2, p = 0.999
```

##### 控制函數find_location對錯誤的處理

```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_location 使用新的策略，使用 typedef.
l = find_location<normal>(z, p, sd, ignore_domain_policy());
// 缺省策略 policy<>, 需要"using boost::math::policies::policy;"
l = find_location<normal>(z, p, sd, policy<>());
// 缺省策略，完全地指定.
l = find_location<normal>(z, p, sd, boost::math::policies::policy<>());
// 一個新的策略，忽略定義域錯誤，不使用 typedef.
l = find_location<normal>(z, p, sd, policy<domain_error<ignore_error> >());```

```z = 2.;
double q = 0.95; // = 1 - p; // 補集.
l = find_location<normal>(complement(z, q, sd));

normal np95pc(l, sd); // 同樣的 standard_deviation 但均值改變了
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;
```

```Example: Find location (mean).
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
offset location (mean) = 1.09023
Normal distribution with mean = 1.09023 has fraction <= -2, p = 0.001
Normal distribution with mean = 1.09023 has fraction > -2, p = 0.999
Normal distribution with mean = 0.355146 has fraction <= 2 = 0.95
Normal distribution with mean = 0.355146 has fraction > 2 = 0.05
```

 Copyright ?2006 , 2007, 2008 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang and Bruno Lalande Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)