Advertisement
Guest User

Untitled

a guest
Dec 30th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4.  
  5. const double alphpa = 0.5; //Probability to miss the snowball
  6. const size_t N = 10000; //Amount of terms in the sum
  7.  
  8. int main()
  9. {
  10.     std::cout << std::setprecision(30);
  11.     double complementary_alpha = 1.0 - alphpa;
  12.     std::vector<double> a, b, result;
  13.     a.push_back(1.0);
  14.     a.push_back(0.0);
  15.     b.push_back(a[0]);
  16.     b.push_back(alphpa * b[0] + a[1]);
  17.     result.push_back(b.back());
  18.     //Print probability of getting 0 points
  19.     //std::cout << 0 << '\t' << result.back() << std::endl;
  20.     for (size_t i = 1; i <= N; ++i) {
  21.         a.push_back(0.0);
  22.         b.push_back(0.0);
  23.         for (size_t j = 0; j <= i - 1; ++j) {
  24.             a[j] = complementary_alpha * b[j];
  25.         }
  26.         a[i] = 0.0;
  27.         b[0] = a[0];
  28.         for (size_t j = 1; j <= i + 1; ++j) {
  29.             b[j] = alphpa * b[j - 1] + a[j];
  30.         }
  31.         result.push_back(b[i + 1]);
  32.         //Print probability of getting 10i points
  33.         //std::cout << 10 * i << '\t' << result.back() << std::endl;
  34.     }
  35.  
  36.     ////Print partial sums of probabilities of getting each amount of points
  37.     //for (size_t i = 0; i < result.size(); ++i) {
  38.     //    double sum = 0.0;
  39.     //    for (size_t j = 0; j <= i; ++j) {
  40.     //        sum += result[j];
  41.     //    }
  42.     //    std::cout << sum << std::endl;
  43.     //}
  44.  
  45.     double expectation = 0.0;
  46.     for (size_t i = 0; i < result.size(); ++i) {
  47.         expectation += 10 * i * result[i];
  48.         //Print partial sums of average amount of points
  49.         //std::cout << expectation << std::endl;
  50.     }
  51.     //Print average amount of points
  52.     std::cout << expectation << std::endl;
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement