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

📄 find_mean_and_sd_normal.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// find_mean_and_sd_normal.cpp// Copyright Paul A. Bristow 2007.// Use, modification and distribution are subject to 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)// Example of finding mean or sd for normal distribution.// Note that this file contains Quickbook mark-up as well as code// and comments, don't change any of the special comment mark-ups!//[normal_std/*`First we need some includes to access the normal distribution,the algorithms to find location and scale(and some std output of course).*/#include <boost/math/distributions/normal.hpp> // for normal_distribution  using boost::math::normal; // typedef provides default type is double.#include <boost/math/distributions/cauchy.hpp> // for cauchy_distribution  using boost::math::cauchy; // typedef provides default type is 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;//] [/normal_std Quickbook]int main(){  cout << "Find_location (mean) and find_scale (standard deviation) examples." << endl;  try  {//[normal_find_location_and_scale_eg/*`[h4 Using find_location and find_scale to meet dispensing and measurement specifications]Consider an example from K Krishnamoorthy,Handbook of Statistical Distributions with Applications,ISBN 1-58488-635-8, (2006) p 126, example 10.3.7."A machine is set to pack 3 kg of ground beef per pack.Over a long period of time it is found that the average packed was 3 kgwith a standard deviation of 0.1 kg.Assume the packing is normally distributed."We start by constructing a normal distribution with the given parameters:*/double mean = 3.; // kgdouble standard_deviation = 0.1; // kgnormal packs(mean, standard_deviation);/*`We can then find the fraction (or %) of packages that weigh more than 3.1 kg.*/double max_weight = 3.1; // kgcout << "Percentage of packs > " << max_weight << " is "<< cdf(complement(packs, max_weight)) * 100. << endl; // P(X > 3.1)/*`We might want to ensure that 95% of packs are over a minimum weight specification,then we want the value of the mean such that P(X < 2.9) = 0.05.Using the mean of 3 kg, we can estimatethe fraction of packs that fail to meet the specification of 2.9 kg.*/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/*`This is 0.84 - more than the target fraction of 0.95.If we want 95% to be over the minimum weight,what should we set the mean weight to be?Using the KK StatCalc program supplied with the book and the method given on page 126 gives 3.06449.We can confirm this by constructing a new distribution which we call 'xpacks'with a safety margin mean of 3.06449 thus:*/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/*`Using this Math Toolkit, we can calculate the required mean directly thus:*/double under_fraction = 0.05;  // so 95% are above the minimum weight mean - sd = 2.9double 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;// Setting the packer to 3.06449 will mean that fraction of packs >= 2.9 is 0.95/*`This calculation is generalized as the free function called[link math_toolkit.dist.dist_ref.dist_algorithms find_location].To use this we will need to*/#include <boost/math/distributions/find_location.hpp>  using boost::math::find_location;/*`and then use find_location function to find safe_mean,& construct a new normal distribution called 'goodpacks'.*/double safe_mean = find_location<normal>(minimum_weight, under_fraction, standard_deviation);normal good_packs(safe_mean, standard_deviation);/*`with the same confirmation as before:*/cout << "Setting the packer to " << nominal_mean << " will mean that "  << "fraction of packs >= " << minimum_weight  << " is " << cdf(complement(good_packs, minimum_weight)) << endl;// Setting the packer to 3.06449 will mean that fraction of packs >= 2.9 is 0.95/*`[h4 Using Cauchy-Lorentz instead of normal distribution]After examining the weight distribution of a large number of packs, we might decide that,after all, the assumption of a normal distribution is not really justified.We might find that the fit is better to a __cauchy_distrib.This distribution has wider 'wings', so that whereas most of the valuesare closer to the mean than the normal, there are also more values than 'normal'that lie further from the mean than the normal.This might happen because a larger than normal lump of meat is either included or excluded.We first create a __cauchy_distrib with the original mean and standard deviation,and estimate the fraction that lie below our minimum weight specification.*/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;// Cauchy Setting the packer to 3 will mean that fraction of packs >= 2.9 is 0.75/*`Note that far fewer of the packs meet the specification, only 75% instead of 95%.Now we can repeat the find_location, using the cauchy distribution as template parameter,in place of the normal used above.*/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/*`Note that the safe_mean setting needs to be much higher, 3.53138 instead of 3.06449,so we will make rather less profit.And again confirm that the fraction meeting specification is as expected.*/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;// Cauchy Setting the packer to 3.53138 will mean that fraction of packs >= 2.9 is 0.95/*`Finally we could estimate the effect of a much tighter specification,that 99% of packs met the specification.*/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;/*`Setting the packer to 3.13263 will mean that fraction of packs >= 2.9 is 0.99,but will more than double the mean loss from 0.0644 to 0.133 kg per pack.Of course, this calculation is not limited to packs of meat, it applies to dispensing anything,and it also applies to a 'virtual' material like any measurement.The only caveat is that the calculation assumes that the standard deviation (scale) is known witha reasonably low uncertainty, something that is not so easy to ensure in practice.And that the distribution is well defined, __normal_distrib or __cauchy_distrib, or some other.If one is simply dispensing a very large number of packs,then it may be feasible to measure the weight of hundreds or thousands of packs.With a healthy 'degrees of freedom', the confidence intervals for the standard deviationare not too wide, typically about + and - 10% for hundreds of observations.For other applications, where it is more difficult or expensive to make many observations,the confidence intervals are depressingly wide.

⌨️ 快捷键说明

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