Example #1
0
int main()
{

   Random::Set(0.46875);

Normal Z;
cout << Z.Next() << endl;

   return 0;
}
Example #2
0
void test1(int n)
{
   Normal nn;
   Uniform uniform;
   cout <<
     "Print 20 N(0,1) random numbers - should be the same as in sample output" <<
     endl;
   {
      Format F; F.FormatType(Format::DEC_FIGS); F.Precision(12); F.Width(15);
      for (int i=0; i<20; i++) cout << F << nn.Next() << endl;
   }
   cout << endl;

   cout << "Print histograms of data from a variety distributions" << endl;
   cout << "Histograms should be close to those in sample output" << endl;
   cout << "s. mean and s. var should be close to p. mean and s. mean" << endl << endl;

   { Constant c(5.0);                         Histogram(&c, n); }
   { Uniform u;                               Histogram(&u, n); }
   { SumRandom sr=uniform(3)-1.5;             Histogram(&sr, n); }
   { SumRandom sr=uniform-uniform;            Histogram(&sr, n); }
   { Normal normal;                           Histogram(&normal, n); }
   { Cauchy cauchy;                           Histogram(&cauchy, n); }
   { AsymGenX normal10(NORMAL10, 10.0);       Histogram(&normal10, n); }
   cout << "Mean and variance should be 10.0 and 4.0" << endl;
   { AsymGenX uniform2(UNIF,0.5);             Histogram(&uniform2, n); }
   cout << "Mean and variance should be 0.5 and 0.083333" << endl;
   { SymGenX triang(TRIANG);                  Histogram(&triang, n); }
   cout << "Mean and variance should be 0 and 0.16667" << endl;
   { Poisson p(0.25);                         Histogram(&p, n); }
   { Poisson p(10.0);                         Histogram(&p, n); }
   { Poisson p(16.0);                         Histogram(&p, n); }
   { Binomial b(18,0.3);                      Histogram(&b, n); }
   { Binomial b(19,0.3);                      Histogram(&b, n); }
   { Binomial b(20,0.3);                      Histogram(&b, n); }
   { Binomial b(58,0.3);                      Histogram(&b, n); }
   { Binomial b(59,0.3);                      Histogram(&b, n); }
   { Binomial b(60,0.3);                      Histogram(&b, n); }
   { Binomial b(18,0.05);                     Histogram(&b, n); }
   { Binomial b(19,0.05);                     Histogram(&b, n); }
   { Binomial b(20,0.05);                     Histogram(&b, n); }
   { Binomial b(98,0.01);                     Histogram(&b, n); }
   { Binomial b(99,0.01);                     Histogram(&b, n); }
   { Binomial b(100,0.01);                    Histogram(&b, n); }
   { Binomial b(18,0.95);                     Histogram(&b, n); }
   { Binomial b(19,0.95);                     Histogram(&b, n); }
   { Binomial b(20,0.95);                     Histogram(&b, n); }
   { Binomial b(98,0.99);                     Histogram(&b, n); }
   { Binomial b(99,0.99);                     Histogram(&b, n); }
   { Binomial b(100,0.99);                    Histogram(&b, n); }
   { NegativeBinomial nb(100,6.0);            Histogram(&nb, n); }
   { NegativeBinomial nb(11,9.0);             Histogram(&nb, n); }
   { NegativeBinomial nb(11,1.9);             Histogram(&nb, n); }
   { NegativeBinomial nb(11,0.10);            Histogram(&nb, n); }
   { NegativeBinomial nb(1.5,1.9);            Histogram(&nb, n); }
   { NegativeBinomial nb(1.0,1.9);            Histogram(&nb, n); }
   { NegativeBinomial nb(0.3,19);             Histogram(&nb, n); }
   { NegativeBinomial nb(0.3,1.9);            Histogram(&nb, n); }
   { NegativeBinomial nb(0.3,0.05);           Histogram(&nb, n); }
   { NegativeBinomial nb(100.8,0.18);         Histogram(&nb, n); }
   { ChiSq c(1,2.0);                          Histogram(&c, n); }
   { ChiSq c(2,2.0);                          Histogram(&c, n); }
   { ChiSq c(3,2.0);                          Histogram(&c, n); }
   { ChiSq c(4,2.0);                          Histogram(&c, n); }
   { ChiSq c(1    );                          Histogram(&c, n); }
   { ChiSq c(2    );                          Histogram(&c, n); }
   { ChiSq c(3    );                          Histogram(&c, n); }
   { ChiSq c(4    );                          Histogram(&c, n); }
   { Gamma g1(1.0);                           Histogram(&g1, n); }
   { Gamma g2(0.5);                           Histogram(&g2, n); }
   { Gamma g3(1.01);                          Histogram(&g3, n); }
   { Gamma g4(2.0);                           Histogram(&g4, n); }
   { Pareto p1(0.5);                          Histogram(&p1, n); }
   { Pareto p2(1.5);                          Histogram(&p2, n); }
   { Pareto p3(2.5);                          Histogram(&p3, n); }
   { Pareto p4(4.5);                          Histogram(&p4, n); }
   Real probs[]={.1,.3,.05,.11,.05,.04,.05,.05,.1,.15};
   Real val[]={2,3,4,6,8,12,16,24,32,48};
   { DiscreteGen discrete(10,probs);          Histogram(&discrete, n); }
   { DiscreteGen discrete(10,probs,val);      Histogram(&discrete, n); }
}
Example #3
0
void test3(int n)
{
   cout << endl;

   // Do chi-squared tests to discrete data
   cout << "ChiSquared tests for discrete data" << endl;
   cout << "chisq should be less than 95% point in most cases" << endl;
   cout << "   and 99% point in almost all cases" << endl << endl;
   {
      Real p[] = { 0.05, 0.10, 0.05, 0.5, 0.01, 0.01, 0.03, 0.20, 0.05 };
      TestDiscreteGen(9, p, n);
   }

   {
      Real p[] = { 0.4, 0.2, 0.1, 0.05, 0.025, 0.0125, 0.00625, 0.00625, 0.2 };
      TestDiscreteGen(9, p, n);
   }


   TestNegativeBinomial(200.3, 0.05, n);
   TestNegativeBinomial(150.3, 0.15, n);
   TestNegativeBinomial(100.8, 0.18, n);
   TestNegativeBinomial(100.8, 1.22, n);
   TestNegativeBinomial(100.8, 9.0, n);
   TestNegativeBinomial(10.5, 0.18, n);
   TestNegativeBinomial(10.5, 1.22, n);
   TestNegativeBinomial(10.5, 9.0, n);
   TestNegativeBinomial(0.35, 0.18, n);
   TestNegativeBinomial(0.35, 1.22, n);
   TestNegativeBinomial(0.35, 9.0, n);

   TestBinomial(100, 0.45, n);
   TestBinomial(100, 0.25, n);
   TestBinomial(100, 0.02, n);
   TestBinomial(100, 0.01, n);
   TestBinomial(49, 0.60, n);
   TestBinomial(21, 0.70, n);
   TestBinomial(10, 0.90, n);
   TestBinomial(10, 0.25, n);
   TestBinomial(10, 0.10, n);

   TestPoisson(0.75, n);
   TestPoisson(4.3, n);
   TestPoisson(10, n);
   TestPoisson(100, n);

   Real* data = new Real[n];
   if (!data) Throw(Bad_alloc());

// Apply KS test to a variety of continuous distributions
//    - use cdf transform to convert to uniform

   cout << endl;
   cout << "Kolmogorov-Smirnoff tests for continuous distributions" << endl;
   cout << "25%, 5%, 1%, .1% upper points are 1.019, 1.358, 1.628, 1.950"
      << endl;
   cout << "5% lower point is 0.520" << endl;
   cout << "Values should be mostly less than 5% upper point" << endl;
   cout << "   and less than 1% point almost always" << endl << endl;

   {
      ChiSq X(1, 1.44);
      for (int i = 0; i < n; i++)
      {
         Real x = sqrt(X.Next());
         data[i] = NormalDF(x - 1.2) - NormalDF(-x - 1.2);
      }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      ChiSq X(4);
      for (int i = 0; i < n; i++)
         { Real x = 0.5 * X.Next(); data[i] = (1+x)*exp(-x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      ChiSq X(2);
      for (int i = 0; i < n; i++) data[i] = exp(-0.5 * X.Next());
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Pareto X(0.5);
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = 1.0 / sqrt(x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Pareto X(1.5);
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = 1.0 / (x * sqrt(x)); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Normal X;
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = NormalDF(x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Normal N; SumRandom X = 10 + 5 * N;
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = NormalDF((x-10)/5); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Normal N; Cauchy C; MixedRandom X = N(0.9) + C(0.1);
      for (int i = 0; i < n; i++)
      {
         Real x = X.Next();
         data[i] = 0.9*NormalDF(x)+0.1*(atan(x)/3.141592654 + 0.5);
      }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Normal N; MixedRandom X = N(0.9) + (10*N)(0.1);
      for (int i = 0; i < n; i++)
      {
         Real x = X.Next();
         data[i] = 0.9*NormalDF(x)+0.1*NormalDF(x/10);
      }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Normal  X0; SumRandom X = X0 * 0.6 + X0 * 0.8;
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = NormalDF(x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Normal X1;
      MixedRandom X = X1(0.2) + (X1 * 2.5 + 1.1)(0.35) + (X1 + 2.3)(0.45);
      for (int i = 0; i < n; i++)
      {
         Real x = X.Next();
         data[i] = 0.20 * NormalDF(x)
                 + 0.35 * NormalDF((x - 1.1) / 2.5)
                 + 0.45 * NormalDF(x - 2.3);
      }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Gamma X(0.5);
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = 2.0 * NormalDF(-sqrt(2 * x)); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Gamma X(3);
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = (1+x+0.5*x*x)*exp(-x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Gamma X1(0.85); Gamma X2(2.15); SumRandom X = X1 + X2;
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = (1+x+0.5*x*x)*exp(-x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Gamma X1(0.75); Gamma X2(0.25); SumRandom X = X1 + X2;
      for (int i = 0; i < n; i++) data[i] = exp(-X.Next());
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Gamma X(2);
      for (int i = 0; i < n; i++)
         { Real x = X.Next(); data[i] = (1+x)*exp(-x); }
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Exponential X;
      for (int i = 0; i < n; i++) data[i] = exp(-X.Next());
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Cauchy X;
      for (int i = 0; i < n; i++) data[i] = atan(X.Next())/3.141592654 + 0.5;
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Cauchy X0; SumRandom X = X0 * 0.3 + X0 * 0.7;
      for (int i = 0; i < n; i++) data[i] = atan(X.Next())/3.141592654 + 0.5;
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   {
      Uniform X;
      for (int i = 0; i < n; i++) data[i] = X.Next();
      cout << X.Name() << ":   "  << KS(data, n) << endl;
   }

   delete [] data;


}