📄 normal_misc_examples.cpp
字号:
// negative_binomial_example3.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 using 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_basic1/*`First we need some includes to access the normal distribution(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 <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;int main(){ cout << "Example: Normal distribution, Miscellaneous Applications."; try { { // Traditional tables and values./*`Let's start by printing some traditional tables.*/ double step = 1.; // in z double range = 4; // min and max z = -range to +range. int precision = 17; // traditional tables are only computed to much lower precision. // Construct a standard normal distribution s normal s; // (default mean = zero, and standard deviation = unity) cout << "Standard normal distribution, mean = "<< s.mean() << ", standard deviation = " << s.standard_deviation() << endl;/*` First the probability distribution function (pdf).*/ cout << "Probability distribution function values" << endl; cout << " z " " pdf " << endl; cout.precision(5); for (double z = -range; z < range + step; z += step) { cout << left << setprecision(3) << setw(6) << z << " " << setprecision(precision) << setw(12) << pdf(s, z) << endl; } cout.precision(6); // default /*`And the area under the normal curve from -[infin] up to z, the cumulative distribution function (cdf).*/ // For a standard normal distribution cout << "Standard normal mean = "<< s.mean() << ", standard deviation = " << s.standard_deviation() << endl; cout << "Integral (area under the curve) from - infinity up to z " << endl; cout << " z " " cdf " << endl; for (double z = -range; z < range + step; z += step) { cout << left << setprecision(3) << setw(6) << z << " " << setprecision(precision) << setw(12) << cdf(s, z) << endl; } cout.precision(6); // default/*`And all this you can do with a nanoscopic amount of work compared tothe team of *human computers* toiling with Milton Abramovitz and Irene Stegenat the US National Bureau of Standards (now [@http://www.nist.gov NIST]). Starting in 1938, their "Handbook of Mathematical Functions with Formulas, Graphs and Mathematical Tables",was eventually published in 1964, and has been reprinted numerous times since.(A major replacement is planned at [@http://dlmf.nist.gov Digital Library of Mathematical Functions]).Pretty-printing a traditional 2-dimensional table is left as an exercise for the student,but why bother now that the Math Toolkit lets you write*/ double z = 2.; cout << "Area for z = " << z << " is " << cdf(s, z) << endl; // to get the area for z./*`Correspondingly, we can obtain the traditional 'critical' values for significance levels.For the 95% confidence level, the significance level usually called alpha,is 0.05 = 1 - 0.95 (for a one-sided test), so we can write*/ cout << "95% of area has a z below " << quantile(s, 0.95) << endl; // 95% of area has a z below 1.64485/*`and a two-sided test (a comparison between two levels, rather than a one-sided test)*/ cout << "95% of area has a z between " << quantile(s, 0.975) << " and " << -quantile(s, 0.975) << endl; // 95% of area has a z between 1.95996 and -1.95996/*`First, define a table of significance levels: these are the probabilitiesthat the true occurrence frequency lies outside the calculated interval.It is convenient to have an alpha level for the probability that z lies outside just one standard deviation.This will not be some nice neat number like 0.05, but we can easily calculate it,*/ double alpha1 = cdf(s, -1) * 2; // 0.3173105078629142 cout << setprecision(17) << "Significance level for z == 1 is " << alpha1 << endl;/*` and place in our array of favorite alpha values.*/ double alpha[] = {0.3173105078629142, // z for 1 standard deviation. 0.20, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };/*`Confidence value as % is (1 - alpha) * 100 (so alpha 0.05 == 95% confidence)that the true occurrence frequency lies *inside* the calculated interval.*/ cout << "level of significance (alpha)" << setprecision(4) << endl; cout << "2-sided 1 -sided z(alpha) " << endl; for (int i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i) { cout << setw(15) << alpha[i] << setw(15) << alpha[i] /2 << setw(10) << quantile(complement(s, alpha[i]/2)) << endl; // Use quantile(complement(s, alpha[i]/2)) to avoid potential loss of accuracy from quantile(s, 1 - alpha[i]/2) } cout << endl;/*`Notice the distinction between one-sided (also called one-tailed)where we are using a > *or* < test (and not both)and considering the area of the tail (integral) from z up to +[infin],and a two-sided test where we are using two > *and* < tests, and thus considering two tails,from -[infin] up to z low and z high up to +[infin].So the 2-sided values alpha[i] are calculated using alpha[i]/2.If we consider a simple example of alpha = 0.05, then for a two-sided test,the lower tail area from -[infin] up to -1.96 is 0.025 (alpha/2)and the upper tail area from +z up to +1.96 is also 0.025 (alpha/2),and the area between -1.96 up to 12.96 is alpha = 0.95.and the sum of the two tails is 0.025 + 0.025 = 0.05,*///] [/[normal_basic1]//[normal_basic2/*`Armed with the cumulative distribution function, we can easily calculate theeasy to remember proportion of values that lie within 1, 2 and 3 standard deviations from the mean.*/ cout.precision(3); cout << showpoint << "cdf(s, s.standard_deviation()) = " << cdf(s, s.standard_deviation()) << endl; // from -infinity to 1 sd cout << "cdf(complement(s, s.standard_deviation())) = " << cdf(complement(s, s.standard_deviation())) << endl; cout << "Fraction 1 standard deviation within either side of mean is " << 1 - cdf(complement(s, s.standard_deviation())) * 2 << endl; cout << "Fraction 2 standard deviations within either side of mean is " << 1 - cdf(complement(s, 2 * s.standard_deviation())) * 2 << endl; cout << "Fraction 3 standard deviations within either side of mean is " << 1 - cdf(complement(s, 3 * s.standard_deviation())) * 2 << endl;/*`To a useful precision, the 1, 2 & 3 percentages are 68, 95 and 99.7,and these are worth memorising as useful 'rules of thumb', as, for example, in[@http://en.wikipedia.org/wiki/Standard_deviation standard deviation]:[preFraction 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.997]We could of course get some really accurate values for these[@http://en.wikipedia.org/wiki/Confidence_interval confidence intervals]by using cout.precision(15);[pre Fraction 1 standard deviation within either side of mean is 0.682689492137086Fraction 2 standard deviations within either side of mean is 0.954499736103642Fraction 3 standard deviations within either side of mean is 0.997300203936740]But before you get too excited about this impressive precision,don't forget that the *confidence intervals of the standard deviation* are surprisingly wide,especially if you have estimated the standard deviation from only a few measurements.*///] [/[normal_basic2]//[normal_bulbs_example1/*`Examples from K. Krishnamoorthy, Handbook of Statistical Distributions with Applications,ISBN 1 58488 635 8, page 125... implemented using the Math Toolkit library.A few very simple examples are shown here:*/// K. Krishnamoorthy, Handbook of Statistical Distributions with Applications, // ISBN 1 58488 635 8, page 125, example 10.3.5/*`Mean lifespan of 100 W bulbs is 1100 h with standard deviation of 100 h.Assuming, perhaps with little evidence and much faith, that the distribution is normal,we construct a normal distribution called /bulbs/ with these values:*/ double mean_life = 1100.; double life_standard_deviation = 100.; normal bulbs(mean_life, life_standard_deviation); double expected_life = 1000.;/*`The we can use the Cumulative distribution function to predict fractions(or percentages, if * 100) that will last various lifetimes.*/ cout << "Fraction of bulbs that will last at best (<=) " // P(X <= 1000) << expected_life << " is "<< cdf(bulbs, expected_life) << endl; cout << "Fraction of bulbs that will last at least (>) " // P(X > 1000) << expected_life << " is "<< cdf(complement(bulbs, expected_life)) << endl; double min_life = 900; double max_life = 1200; cout << "Fraction of bulbs that will last between " << min_life << " and " << max_life << " is " << cdf(bulbs, max_life) // P(X <= 1200) - cdf(bulbs, min_life) << endl; // P(X <= 900) /*`[note Real-life failures are often very ab-normal,with a significant number that 'dead-on-arrival' or suffer failure very early in their life:the lifetime of the survivors of 'early mortality' may be well described by the normal distribution.]*///] [/normal_bulbs_example1 Quickbook end] } { // K. Krishnamoorthy, Handbook of Statistical Distributions with Applications, // ISBN 1 58488 635 8, page 125, Example 10.3.6 //[normal_bulbs_example3/*`Weekly demand for 5 lb sacks of onions at a store is normally distributed with mean 140 sacks and standard deviation 10.*/ double mean = 140.; // sacks per week. double standard_deviation = 10; normal sacks(mean, standard_deviation); double stock = 160.; // per week. cout << "Percentage of weeks overstocked " << cdf(sacks, stock) * 100. << endl; // P(X <=160) // Percentage of weeks overstocked 97.7/*`So there will be lots of mouldy onions!So we should be able to say what stock level will meet demand 95% of the weeks.*/ double stock_95 = quantile(sacks, 0.95); cout << "Store should stock " << int(stock_95) << " sacks to meet 95% of demands." << endl;/*`And it is easy to estimate how to meet 80% of demand, and waste even less.*/ double stock_80 = quantile(sacks, 0.80); cout << "Store should stock " << int(stock_80) << " sacks to meet 8 out of 10 demands." << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -