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

📄 negative_binomial_example1.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      << cdf(complement(nb, all_houses - sales_quota)) << endl;/*`[preProbability of failing to sell his quota of 5 barseven after visiting all 30 houses is 0.0015101]We can also use the quantile (percentile), the inverse of the cdf, topredict which house Pat will finish on.  So for the 8th house:*/ double p = cdf(nb, (8 - sales_quota));  cout << "Probability of meeting sales quota on or before 8th house is "<< p << endl;/*`[preProbability of meeting sales quota on or before 8th house is 0.174]*/    cout << "If the confidence of meeting sales quota is " << p        << ", then the finishing house is " << quantile(nb, p) + sales_quota << endl;    cout<< " quantile(nb, p) = " << quantile(nb, p) << endl;/*`[preIf the confidence of meeting sales quota is 0.17367, then the finishing house is 8]Demanding absolute certainty that all 5 will be sold,implies an infinite number of trials.(Of course, there are only 30 houses on the estate,so he can't ever be *certain* of selling his quota).*/    cout << "If the confidence of meeting sales quota is " << 1.        << ", then the finishing house is " << quantile(nb, 1) + sales_quota << endl;    //  1.#INF == infinity./*`[preIf the confidence of meeting sales quota is 1, then the finishing house is 1.#INF]And similarly for a few other probabilities:*/    cout << "If the confidence of meeting sales quota is " << 0.        << ", then the finishing house is " << quantile(nb, 0.) + sales_quota << endl;    cout << "If the confidence of meeting sales quota is " << 0.5        << ", then the finishing house is " << quantile(nb, 0.5) + sales_quota << endl;    cout << "If the confidence of meeting sales quota is " << 1 - 0.00151 // 30 th        << ", then the finishing house is " << quantile(nb, 1 - 0.00151) + sales_quota << endl;/*`[preIf the confidence of meeting sales quota is 0, then the finishing house is 5If the confidence of meeting sales quota is 0.5, then the finishing house is 11.337If the confidence of meeting sales quota is 0.99849, then the finishing house is 30]Notice that because we chose a discrete quantile policy of real,the result can be an 'unreal' fractional house.If the opposite is true, we don't want to assume any confidence, then this is tantamountto assuming that all the first sales_quota trials will be successful sales.*/    cout << "If confidence of meeting quota is zero\n(we assume all houses are successful sales)"       ", then finishing house is " << sales_quota << endl;/*`[preIf confidence of meeting quota is zero (we assume all houses are successful sales), then finishing house is 5If confidence of meeting quota is 0, then finishing house is 5]We can list quantiles for a few probabilities:*/    double ps[] = {0., 0.001, 0.01, 0.05, 0.1, 0.5, 0.9, 0.95, 0.99, 0.999, 1.};    // Confidence as fraction = 1-alpha, as percent =  100 * (1-alpha[i]) %    cout.precision(3);    for (int i = 0; i < sizeof(ps)/sizeof(ps[0]); i++)    {      cout << "If confidence of meeting quota is " << ps[i]        << ", then finishing house is " << quantile(nb, ps[i]) + sales_quota        << endl;   }/*`[preIf confidence of meeting quota is 0, then finishing house is 5If confidence of meeting quota is 0.001, then finishing house is 5If confidence of meeting quota is 0.01, then finishing house is 5If confidence of meeting quota is 0.05, then finishing house is 6.2If confidence of meeting quota is 0.1, then finishing house is 7.06If confidence of meeting quota is 0.5, then finishing house is 11.3If confidence of meeting quota is 0.9, then finishing house is 17.8If confidence of meeting quota is 0.95, then finishing house is 20.1If confidence of meeting quota is 0.99, then finishing house is 24.8If confidence of meeting quota is 0.999, then finishing house is 31.1If confidence of meeting quota is 1, then finishing house is 1.#INF]We could have applied a ceil function to obtain a 'worst case' integer value for house.``ceil(quantile(nb, ps[i]))``Or, if we had used the default discrete quantile policy, integer_outside, by omitting``#define BOOST_MATH_DISCRETE_QUANTILE_POLICY real``we would have achieved the same effect.The real result gives some suggestion which house is most likely.For example, compare the real and integer_outside for 95% confidence.[preIf confidence of meeting quota is 0.95, then finishing house is 20.1If confidence of meeting quota is 0.95, then finishing house is 21]The real value 20.1 is much closer to 20 than 21, so integer_outside is pessimistic.We could also use integer_round_nearest policy to suggest that 20 is more likely.Finally, we can tabulate the probability for the last sale being exactly on each house.*/   cout << "\nHouse for " << sales_quota << "th (last) sale.  Probability (%)" << endl;   cout.precision(5);   for (int i = (int)sales_quota; i < all_houses+1; i++)   {     cout << left << setw(3) << i << "                             " << setw(8) << cdf(nb, i - sales_quota)  << endl;   }   cout << endl;/*`[preHouse for 5 th (last) sale.  Probability (%)5                               0.01024 6                               0.04096 7                               0.0962568                               0.17367 9                               0.26657 10                              0.3669  11                              0.46723 12                              0.56182 13                              0.64696 14                              0.72074 15                              0.78272 16                              0.83343 17                              0.874   18                              0.90583 19                              0.93039 20                              0.94905 21                              0.96304 22                              0.97342 23                              0.98103 24                              0.98655 25                              0.99053 26                              0.99337 27                              0.99539 28                              0.99681 29                              0.9978  30                              0.99849]As noted above, using a catch block is always a good idea, even if you do not expect to use it.*/  }  catch(const std::exception& e)  { // Since we have set an overflow policy of ignore_error,    // an overflow exception should never be thrown.     std::cout << "\nMessage from thrown exception was:\n " << e.what() << std::endl;/*`For example, without a ignore domain error policy, if we asked for ``pdf(nb, -1)`` for example, we would get:[preMessage from thrown exception was: Error in function boost::math::pdf(const negative_binomial_distribution<double>&, double): Number of failures argument is -1, but must be >= 0 !]*///] [/ negative_binomial_eg1_2]  }   return 0;}  // int main()/*Output is:Selling candy bars - using the negative binomial distribution.by Dr. Diane Evans,Professor of Mathematics at Rose-Hulman Institute of Technology,see http://en.wikipedia.org/wiki/Negative_binomial_distributionPat has a sales per house success rate of 0.4.Therefore he would, on average, sell 40 bars after trying 100 houses.With a success rate of 0.4, he might expect, on average,to need to visit about 12 houses in order to sell all 5 bars. Probability that Pat finishes on the 5th house is 0.01024Probability that Pat finishes on the 6th house is 0.03072Probability that Pat finishes on the 7th house is 0.055296Probability that Pat finishes on the 8th house is 0.077414Probability that Pat finishes on or before the 8th house is sum pdf(sales_quota) + pdf(6) + pdf(7) + pdf(8) = 0.17367Probability of selling his quota of 5 barson or before the 8th house is 0.17367Probability that Pat finishes exactly on the 10th house is 0.10033Probability of selling his quota of 5 barson or before the 10th house is 0.3669Probability that Pat finishes exactly on the 11th house is 0.10033Probability of selling his quota of 5 barson or before the 11th house is 0.46723Probability that Pat finishes exactly on the 12th house is 0.094596Probability of selling his quota of 5 barson or before the 12th house is 0.56182Probability that Pat finishes on the 30 house is 0.00069145Probability of selling his quota of 5 barson or before the 30th house is 0.99849Probability of failing to sell his quota of 5 barseven after visiting all 30 houses is 0.0015101Probability of meeting sales quota on or before 8th house is 0.17367If the confidence of meeting sales quota is 0.17367, then the finishing house is 8 quantile(nb, p) = 3If the confidence of meeting sales quota is 1, then the finishing house is 1.#INFIf the confidence of meeting sales quota is 0, then the finishing house is 5If the confidence of meeting sales quota is 0.5, then the finishing house is 11.337If the confidence of meeting sales quota is 0.99849, then the finishing house is 30If confidence of meeting quota is zero(we assume all houses are successful sales), then finishing house is 5If confidence of meeting quota is 0, then finishing house is 5If confidence of meeting quota is 0.001, then finishing house is 5If confidence of meeting quota is 0.01, then finishing house is 5If confidence of meeting quota is 0.05, then finishing house is 6.2If confidence of meeting quota is 0.1, then finishing house is 7.06If confidence of meeting quota is 0.5, then finishing house is 11.3If confidence of meeting quota is 0.9, then finishing house is 17.8If confidence of meeting quota is 0.95, then finishing house is 20.1If confidence of meeting quota is 0.99, then finishing house is 24.8If confidence of meeting quota is 0.999, then finishing house is 31.1If confidence of meeting quota is 1, then finishing house is 1.#JHouse for 5th (last) sale.  Probability (%)5                               0.01024 6                               0.04096 7                               0.0962568                               0.17367 9                               0.26657 10                              0.3669  11                              0.46723 12                              0.56182 13                              0.64696 14                              0.72074 15                              0.78272 16                              0.83343 17                              0.874   18                              0.90583 19                              0.93039 20                              0.94905 21                              0.96304 22                              0.97342 23                              0.98103 24                              0.98655 25                              0.99053 26                              0.99337 27                              0.99539 28                              0.99681 29                              0.9978  30                              0.99849 */

⌨️ 快捷键说明

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