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

📄 binomial_quiz_example.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Copyright Paul A. Bristow 2007// Copyright John Maddock 2006// 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)// binomial_examples_quiz.cpp// Simple example of computing probabilities and quantiles for a binomial random variable// representing the correct guesses on a multiple-choice test.// source http://www.stat.wvu.edu/SRS/Modules/Binomial/test.html//[binomial_quiz_example1/*`A multiple choice test has four possible answers to each of 16 questions.A student guesses the answer to each question,so the probability of getting a correct answer on any given question isone in four, a quarter, 1/4, 25% or fraction 0.25.The conditions of the binomial experiment are assumed to be met:n = 16 questions constitute the trials;each question results in one of two possible outcomes (correct or incorrect);the probability of being correct is 0.25 and is constant if no knowledge about the subject is assumed;the questions are answered independently if the student's answer to a questionin no way influences his/her answer to another question.First, we need to be able to use the binomial distribution constructor(and some std input/output, of course).*/#include <boost/math/distributions/binomial.hpp>  using boost::math::binomial;#include <iostream>  using std::cout; using std::endl;  using std::ios; using std::flush; using std::left; using std::right; using std::fixed;#include <iomanip>  using std::setw; using std::setprecision;//][/binomial_quiz_example1]int main(){  try  {  cout << "Binomial distribution example - guessing in a quiz." << endl;//[binomial_quiz_example2/*`The number of correct answers, X, is distributed as a binomial random variablewith binomial distribution parameters: questions n = 16 and success fraction probability p = 0.25.So we construct a binomial distribution:*/  int questions = 16; // All the questions in the quiz.  int answers = 4; // Possible answers to each question.  double success_fraction = (double)answers / (double)questions; // If a random guess.  // Caution:  = answers / questions would be zero (because they are integers)!  binomial quiz(questions, success_fraction);/*`and display the distribution parameters we used thus:*/  cout << "In a quiz with " << quiz.trials()    << " questions and with a probability of guessing right of "    << quiz.success_fraction() * 100 << " %"     << " or 1 in " << static_cast<int>(1. / quiz.success_fraction()) << endl;/*`Show a few probabilities of just guessing:*/  cout << "Probability of getting none right is " << pdf(quiz, 0) << endl; // 0.010023  cout << "Probability of getting exactly one right is " << pdf(quiz, 1) << endl;  cout << "Probability of getting exactly two right is " << pdf(quiz, 2) << endl;  int pass_score = 11;  cout << "Probability of getting exactly " << pass_score << " answers right by chance is "     << pdf(quiz, questions) << endl;/*`[preProbability of getting none right is 0.0100226Probability of getting exactly one right is 0.0534538Probability of getting exactly two right is 0.133635Probability of getting exactly 11 answers right by chance is 2.32831e-010]These don't give any encouragement to guessers!We can tabulate the 'getting exactly right' ( == ) probabilities thus:*/  cout << "\n" "Guessed Probability" << right << endl;  for (int successes = 0; successes <= questions; successes++)  {    double probability = pdf(quiz, successes);    cout << setw(2) << successes << "      " << probability << endl;  }  cout << endl;/*`[preGuessed Probability 0      0.0100226 1      0.0534538 2      0.133635 3      0.207876 4      0.225199 5      0.180159 6      0.110097 7      0.0524273 8      0.0196602 9      0.0058252610      0.0013592311      0.00024713212      3.43239e-00513      3.5204e-00614      2.51457e-00715      1.11759e-00816      2.32831e-010]Then we can add the probabilities of some 'exactly right' like this: */  cout << "Probability of getting none or one right is " << pdf(quiz, 0) + pdf(quiz, 1) << endl;/*`[preProbability of getting none or one right is 0.0634764]But if more than a couple of scores are involved, it is more convenient (and may be more accurate)to use the Cumulative Distribution Function (cdf) instead:*/  cout << "Probability of getting none or one right is " << cdf(quiz, 1) << endl;/*`[preProbability of getting none or one right is 0.0634764]Since the cdf is inclusive, we can get the probability of getting up to 10 right ( <= ) */  cout << "Probability of getting <= 10 right (to fail) is " << cdf(quiz, 10) << endl;/*`[preProbability of getting <= 10 right (to fail) is 0.999715]To get the probability of getting 11 or more right (to pass),it is tempting to use ``1 - cdf(quiz, 10)`` to get the probability of > 10*/  cout << "Probability of getting > 10 right (to pass) is " << 1 - cdf(quiz, 10) << endl;/*`[preProbability of getting > 10 right (to pass) is 0.000285239]But this should be resisted in favor of using the complement function. [link why_complements Why complements?]*/  cout << "Probability of getting > 10 right (to pass) is " << cdf(complement(quiz, 10)) << endl;/*`[preProbability of getting > 10 right (to pass) is 0.000285239]And we can check that these two, <= 10 and > 10,  add up to unity.*/BOOST_ASSERT((cdf(quiz, 10) + cdf(complement(quiz, 10))) == 1.); /*`If we want a < rather than a <= test, because the CDF is inclusive, we must subtract one from the score.*/  cout << "Probability of getting less than " << pass_score    << " (< " << pass_score << ") answers right by guessing is "    << cdf(quiz, pass_score -1) << endl;/*`[preProbability of getting less than 11 (< 11) answers right by guessing is 0.999715]and similarly to get a >= rather than a > testwe also need to subtract one from the score (and can again check the sum is unity).This is because if the cdf is /inclusive/, then its complement must be /exclusive/ otherwise there would be one possibleoutcome counted twice!*/  cout << "Probability of getting at least " << pass_score     << "(>= " << pass_score << ") answers right by guessing is "    << cdf(complement(quiz, pass_score-1))    << ", only 1 in " << 1/cdf(complement(quiz, pass_score-1)) << endl;  BOOST_ASSERT((cdf(quiz, pass_score -1) + cdf(complement(quiz, pass_score-1))) == 1);/*`[preProbability of getting at least 11 (>= 11) answers right by guessing is 0.000285239, only 1 in 3505.83]Finally we can tabulate some probabilities:*/  cout << "\n" "At most (<=)""\n""Guessed OK   Probability" << right << endl;  for (int score = 0; score <= questions; score++)  {    cout << setw(2) << score << "           " << setprecision(10)      << cdf(quiz, score) << endl;  }  cout << endl;/*`[preAt most (<=)Guessed OK   Probability 0           0.01002259576 1           0.0634764398 2           0.1971110499 3           0.4049871101 4           0.6301861752 5           0.8103454274 6           0.9204427481 7           0.9728700437 8           0.9925302796 9           0.998355534610           0.999714760811           0.999961892812           0.999996216713           0.999999737114           0.999999988615           0.999999999816           1]*/  cout << "\n" "At least (>)""\n""Guessed OK   Probability" << right << endl;  for (int score = 0; score <= questions; score++)  {    cout << setw(2) << score << "           "  << setprecision(10)      << cdf(complement(quiz, score)) << endl;  }/*`[preAt least (>)Guessed OK   Probability 0           0.9899774042 1           0.9365235602 2           0.8028889501 3           0.5950128899 4           0.3698138248 5           0.1896545726 6           0.07955725188 7           0.02712995629 8           0.00746972044 9           0.00164446537410           0.000285239191711           3.810715862e-00512           3.783265129e-00613           2.628657967e-00714           1.140870154e-00815           2.328306437e-01016           0]We now consider the probabilities of *ranges* of correct guesses.First, calculate the probability of getting a range of guesses right,by adding the exact probabilities of each from low ... high.*/  int low = 3; // Getting at least 3 right.  int high = 5; // Getting as most 5 right.  double sum = 0.;  for (int i = low; i <= high; i++)  {    sum += pdf(quiz, i);  }  cout.precision(4);  cout << "Probability of getting between "    << low << " and " << high << " answers right by guessing is "    << sum  << endl; // 0.61323/*`[preProbability of getting between 3 and 5 answers right by guessing is 0.6132]

⌨️ 快捷键说明

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