⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 find_mean_and_sd_normal.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
See [link math_toolkit.dist.stat_tut.weg.cs_eg.chi_sq_intervals Confidence Intervals on the standard deviation]for a worked example[@../../../example/chi_square_std_dev_test.cpp chi_square_std_dev_test.cpp]of estimating these intervals.[h4 Changing the scale or standard deviation]Alternatively, we could invest in a better (more precise) packer(or measuring device) with a lower standard deviation, or scale.This might cost more, but would reduce the amount we have to 'give away'in order to meet the specification.To estimate how much better (how much smaller standard deviation) it would have to be,we need to get the 5% quantile to be located at the under_weight limit, 2.9*/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.1With the current packer (mean = 3, sd = 0.1), the 5% quantile is at 2.8551 kg,a little below our target of 2.9 kg.So we know that the standard deviation is going to have to be smaller.Let's start by guessing that it (now 0.1) needs to be halved, to a standard deviation of 0.05 kg.*/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.05cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean  << " and standard deviation of " << pack05.standard_deviation()  << " is " << cdf(complement(pack05, minimum_weight)) << endl;// Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.05 is 0.97725/*`So 0.05 was quite a good guess, but we are a little over the 2.9 target,so the standard deviation could be a tiny bit more. So we could do somemore guessing to get closer, say by increasing standard deviation to 0.06 kg,constructing another new distribution called pack06.*/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.06cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean  << " and standard deviation of " << pack06.standard_deviation()  << " is " << cdf(complement(pack06, minimum_weight)) << endl;// Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.06 is 0.95221/*`Now we are getting really close, but to do the job properly,we might need to use root finding method, for example the tools provided,and used elsewhere, in the Math Toolkit, see[link math_toolkit.toolkit.internals1.roots2  Root Finding Without Derivatives].But in this (normal) distribution case, we can and should be even smarterand make a direct calculation.*//*`Our required limit is minimum_weight = 2.9 kg, often called the random variate z.For a standard normal distribution, then probability p = N((minimum_weight - mean) / sd).We want to find the standard deviation that would be required to meet this limit,so that the p th quantile is located at z (minimum_weight).In this case, the 0.05 (5%) quantile is at 2.9 kg pack weight, when the mean is 3 kg,ensuring that 0.95 (95%) of packs are above the minimum weight.Rearranging, we can directly calculate the required standard deviation:*/normal N01; // standard normal distribution with meamn zero and unit standard deviation.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;// For the 0.05th quantile to be located at 2.9, would need a standard deviation of 0.0607957/*`We can now construct a new (normal) distribution pack95 for the 'better' packer,and check that our distribution will meet the specification.*/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;// Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.0607957 is 0.95/*`This calculation is generalized in the free function find_scale,as shown below, giving the same standard deviation.*/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/*`If we had defined an over_fraction, or percentage that must pass specification*/double over_fraction = 0.95;/*`And (wrongly) written double sso = find_scale<normal>(minimum_weight, over_fraction, packs.mean());With the default policy, we would get a message like[preMessage 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?]But this would return a *negative* standard deviation - obviously impossible.The probability should be 1 - over_fraction, not over_fraction, thus:*/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/*`But notice that using '1 - over_fraction' - will lead to a[link why_complements loss of accuracy, especially if over_fraction was close to unity.]In this (very common) case, we should instead use the __complements,giving the most accurate result.*/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/*`Note that our guess of 0.06 was close to the accurate value of 0.060795683191176959.We can again confirm our prediction thus:*/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/*`Notice that these two deceptively simple questions:* Do we over-fill to make sure we meet a minimum specification (or under-fill to avoid an overdose)?and/or* Do we measure better?are actually extremely common.The weight of beef might be replaced by a measurement of more or less anything,from drug tablet content, Apollo landing rocket firing, X-ray treatment doses...The scale can be variation in dispensing or uncertainty in measurement.*///] [/normal_find_location_and_scale_eg Quickbook end]  }  catch(const std::exception& e)  { // Always useful to include try & catch blocks because default policies    // are to throw exceptions on arguments that cause errors like underflow, overflow.    // Lacking try & catch blocks, the program will abort without a message below,    // which may give some helpful clues as to the cause of the exception.    std::cout <<      "\n""Message from thrown exception was:\n   " << e.what() << std::endl;  }  return 0;}  // int main()/*Output is:Find_location and find_scale examples.Percentage of packs > 3.1 is 15.8655Fraction of packs <= 2.9 with a mean of 3 is 0.841345Fraction of packs >= 2.9 with a mean of 3.06449 is 0.950005Setting the packer to 3.06449 will mean that fraction of packs >= 2.9 is 0.95Setting the packer to 3.06449 will mean that fraction of packs >= 2.9 is 0.95Cauchy Setting the packer to 3 will mean that fraction of packs >= 2.9 is 0.75find_location<cauchy>(minimum_weight, over fraction, standard_deviation); 3.53138Cauchy Setting the packer to 3.53138 will mean that fraction of packs >= 2.9 is 0.95Cauchy Setting the packer to -0.282052 will mean that fraction of packs >= 2.9 is 0.95Quantile of 0.05 = 2.83551, mean = 3, sd = 0.1Quantile of 0.05 = 2.91776, mean = 3, sd = 0.05Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.05 is 0.97725Quantile of 0.05 = 2.90131, mean = 3, sd = 0.06Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.06 is 0.95221For the 0.05th quantile to be located at 2.9, would need a standard deviation of 0.0607957Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.0607957 is 0.95find_scale<normal>(minimum_weight, under_fraction, packs.mean()); 0.0607957find_scale<normal>(minimum_weight, under_fraction, packs.mean()); 0.0607957find_scale<normal>(complement(minimum_weight, over_fraction, packs.mean())); 0.0607957Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.0607957 is 0.95*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -