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

📄 normal_misc_examples.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//] [/normal_bulbs_example3 Quickbook end]  }  { // K. Krishnamoorthy, Handbook of Statistical Distributions with Applications,    // ISBN 1 58488 635 8, page 125, Example 10.3.7 //[normal_bulbs_example4/*`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.  Assuming the packing is normally distributed,we can find the fraction (or %) of packages that weigh more than 3.1 kg.*/double mean = 3.; // kgdouble standard_deviation = 0.1; // kgnormal packs(mean, standard_deviation);double max_weight = 3.1; // kgcout << "Percentage of packs > " << max_weight << " is "<< cdf(complement(packs, max_weight)) << endl; // P(X > 3.1)double under_weight = 2.9;cout <<"fraction of packs <= " << under_weight << " with a mean of " << mean   << " is " << cdf(complement(packs, under_weight)) << endl;// fraction of packs <= 2.9 with a mean of 3 is 0.841345// This is 0.84 - more than the target 0.95// Want 95% to be over this weight, so what should we set the mean weight to be?// KK StatCalc says:double over_mean = 3.0664;normal xpacks(over_mean, standard_deviation);cout << "fraction of packs >= " << under_weight<< " with a mean of " << xpacks.mean()   << " is " << cdf(complement(xpacks, under_weight)) << endl;// fraction of packs >= 2.9 with a mean of 3.06449 is 0.950005double 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;normal nominal_packs(nominal_mean, standard_deviation);cout << "Setting the packer to " << nominal_mean << " will mean that "  << "fraction of packs >= " << under_weight   << " is " << cdf(complement(nominal_packs, under_weight)) << endl;/*`Setting the packer to 3.06449 will mean that fraction of packs >= 2.9 is 0.95.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.Alternatively, we could invest in a better (more precise) packer with a lower standard deviation.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*/normal pack05(mean, 0.05); cout << "Quantile of " << p << " = " << quantile(pack05, p)   << ", mean = " << pack05.mean() << ", sd = " << pack05.standard_deviation() << endl;cout <<"Fraction of packs >= " << under_weight << " with a mean of " << mean   << " and standard deviation of " << pack05.standard_deviation()  << " is " << cdf(complement(pack05, under_weight)) << endl;// /*`Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.05 is 0.9772So 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 to 0.06*/normal pack06(mean, 0.06); cout << "Quantile of " << p << " = " << quantile(pack06, p)   << ", mean = " << pack06.mean() << ", sd = " << pack06.standard_deviation() << endl;cout <<"Fraction of packs >= " << under_weight << " with a mean of " << mean   << " and standard deviation of " << pack06.standard_deviation()  << " is " << cdf(complement(pack06, under_weight)) << endl;/*`Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.06 is 0.9522Now we are getting really close, but to do the job properly,we could 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 could be even smarter and make a direct calculation.*/normal s; // For standard normal distribution, double sd = 0.1;double x = 2.9; // Our required limit.// then probability p = N((x - mean) / sd)// So if we want to find the standard deviation that would be required to meet this limit,// so that the p th quantile is located at x,// in this case the 0.95 (95%) quantile at 2.9 kg pack weight, when the mean is 3 kg.double prob =  pdf(s, (x - mean) / sd);double qp = quantile(s, 0.95);cout << "prob = " << prob << ", quantile(p) " << qp << endl; // p = 0.241971, quantile(p) 1.64485// Rearranging, we can directly calculate the required standard deviation:double sd95 = abs((x - mean)) / qp;cout << "If we want the "<< p << " th quantile to be located at "    << x << ", would need a standard deviation of " << sd95 << endl;normal pack95(mean, sd95);  // Distribution of the 'ideal better' packer.cout <<"Fraction of packs >= " << under_weight << " with a mean of " << mean   << " and standard deviation of " << pack95.standard_deviation()  << " is " << cdf(complement(pack95, under_weight)) << endl;// Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.0608 is 0.95/*`Notice that these two deceptively simple questions(do we over-fill or measure better) are actually very common.The weight of beef might be replaced by a measurement of more or less anything.But the calculations rely on the accuracy of the standard deviation - somethingthat is almost always less good than we might wish,especially if based on a few measurements.*///] [/normal_bulbs_example4 Quickbook end]  }  { // K. Krishnamoorthy, Handbook of Statistical Distributions with Applications,    // ISBN 1 58488 635 8, page 125, example 10.3.8//[normal_bulbs_example5/*`A bolt is usable if between 3.9 and 4.1 long.From a large batch of bolts, a sample of 50 show a mean length of 3.95 with standard deviation 0.1.Assuming a normal distribution, what proportion is usable?The true sample mean is unknown, but we can use the sample mean and standard deviation to find approximate solutions.*/    normal bolts(3.95, 0.1);    double top = 4.1;    double bottom = 3.9; cout << "Fraction long enough [ P(X <= " << top << ") ] is " << cdf(bolts, top) << endl;cout << "Fraction too short [ P(X <= " << bottom << ") ] is " << cdf(bolts, bottom) << endl;cout << "Fraction OK  -between " << bottom << " and " << top  << "[ P(X <= " << top  << ") - P(X<= " << bottom << " ) ] is "  << cdf(bolts, top) - cdf(bolts, bottom) << endl;cout << "Fraction too long [ P(X > " << top << ") ] is "  << cdf(complement(bolts, top)) << endl;cout << "95% of bolts are shorter than " << quantile(bolts, 0.95) << endl; //] [/normal_bulbs_example5 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:Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\normal_misc_examples.exe"Example: Normal distribution, Miscellaneous Applications.Standard normal distribution, mean = 0, standard deviation = 1Probability distribution function values  z       pdf -4     0.00013383022576488537-3     0.0044318484119380075-2     0.053990966513188063-1     0.241970724519143370      0.39894228040143271      0.241970724519143372      0.0539909665131880633      0.00443184841193800754      0.00013383022576488537Standard normal mean = 0, standard deviation = 1Integral (area under the curve) from - infinity up to z   z       cdf -4     3.1671241833119979e-005-3     0.0013498980316300959-2     0.022750131948179219-1     0.15865525393145710      0.5         1      0.841344746068542932      0.977249868051820793      0.99865010196836994      0.99996832875816688Area for z = 2 is 0.9772595% of area has a z below 1.6448595% of area has a z between 1.95996 and -1.95996Significance level for z == 1 is 0.3173105078629142level of significance (alpha)2-sided       1 -sided          z(alpha) 0.3173         0.1587         1         0.2            0.1            1.282     0.1            0.05           1.645     0.05           0.025          1.96      0.01           0.005          2.576     0.001          0.0005         3.291     0.0001         5e-005         3.891     1e-005         5e-006         4.417     cdf(s, s.standard_deviation()) = 0.841cdf(complement(s, s.standard_deviation())) = 0.159Fraction 1 standard deviation within either side of mean is 0.683Fraction 2 standard deviations within either side of mean is 0.954Fraction 3 standard deviations within either side of mean is 0.997Fraction of bulbs that will last at best (<=) 1.00e+003 is 0.159Fraction of bulbs that will last at least (>) 1.00e+003 is 0.841Fraction of bulbs that will last between 900. and 1.20e+003 is 0.819Percentage of weeks overstocked 97.7Store should stock 156 sacks to meet 95% of demands.Store should stock 148 sacks to meet 8 out of 10 demands.Percentage of packs > 3.10 is 0.159fraction of packs <= 2.90 with a mean of 3.00 is 0.841fraction of packs >= 2.90 with a mean of 3.07 is 0.952Setting the packer to 3.06 will mean that fraction of packs >= 2.90 is 0.950Quantile of 0.0500 = 2.84, mean = 3.00, sd = 0.100Quantile of 0.0500 = 2.92, mean = 3.00, sd = 0.0500Fraction of packs >= 2.90 with a mean of 3.00 and standard deviation of 0.0500 is 0.977Quantile of 0.0500 = 2.90, mean = 3.00, sd = 0.0600Fraction of packs >= 2.90 with a mean of 3.00 and standard deviation of 0.0600 is 0.952prob = 0.242, quantile(p) 1.64If we want the 0.0500 th quantile to be located at 2.90, would need a standard deviation of 0.0608Fraction of packs >= 2.90 with a mean of 3.00 and standard deviation of 0.0608 is 0.950Fraction long enough [ P(X <= 4.10) ] is 0.933Fraction too short [ P(X <= 3.90) ] is 0.309Fraction OK  -between 3.90 and 4.10[ P(X <= 4.10) - P(X<= 3.90 ) ] is 0.625Fraction too long [ P(X > 4.10) ] is 0.066895% of bolts are shorter than 4.11*/

⌨️ 快捷键说明

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