Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

tryrand.cpp File Reference

#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include "include.h"
#include "newran.h"

Include dependency graph for tryrand.cpp:

Include dependency graph

Go to the source code of this file.

Defines

#define WANT_STREAM

Functions

void test1 (int)
void test2 (int)
void test3 (int)
void test4 (int)
void Histogram (Random *, int)
void Hist (Random *rx, int n, const int cnt)
int main (int argc, char **argv)


Define Documentation

#define WANT_STREAM
 

Definition at line 1 of file tryrand.cpp.


Function Documentation

void Hist Random rx,
int  n,
const int  cnt
 

Definition at line 53 of file hist.cpp.

References al_trace, Random::Next(), and Real.

Referenced by main().

00054 {
00055    int i,j; int *count = new int[cnt];
00056    Real* a = new Real[n];
00057    for (i = 0; i < n; i++) a[i] = rx->Next();
00058    Real amax = a[0]; Real amin = a[0]; Real mean = a[0]; Real sd = 0;
00059    for (i = 1; i < n; i++)
00060    {
00061       if (amin > a[i]) amin = a[i]; else if (amax < a[i]) amax = a[i];
00062       mean += a[i];
00063    }
00064    mean /= n;
00065    for (i = 0; i < cnt ;  i++) count[i]=0;
00066    for (i = 0; i < n; i++)
00067    {
00068      Real rat= (amax != amin) ? (a[i] - amin)/(amax - amin) : 1.0;
00069      j = (int)( (cnt - 0.001) * rat ); count[j]++;
00070      Real diff = a[i] - mean; sd += diff*diff;
00071    }
00072    sd = sqrt(sd/(n-1));
00073    j = 0;
00074    for (i = 0; i < cnt; i++) { if (j < count[i]) j = count[i]; }
00075    if (j > 70) { for (i = 0; i < cnt; i++) if( count[i] )       count[i] = (int)((70L*count[i])/j); }
00076 
00077    al_trace( "\n" );
00078    for (i = 0; i < cnt; i++)
00079         {
00080                 al_trace( "\n%02d|" , i );
00081                 for (j = 1; j < count[i] ; j = j+1) 
00082                         al_trace( "*" );
00083         }
00084 //   al_trace( "\n%s\np. mean = %9f, p. var = %9f\ns. mean = %9f, s. var = %9f\n" , 
00085 //              rx->Name() , rx->Mean() , rx->Variance() , mean , sd * sd );
00086 
00087         delete count;
00088    delete a;
00089 }

void Histogram Random ,
int 
 

Definition at line 16 of file hist.cpp.

00017 {
00018    int i,j; int count[20];
00019    Real* a = new Real[n];
00020    for (i = 0; i < n; i++) a[i] = rx->Next();
00021    Real amax = a[0]; Real amin = a[0]; Real mean = a[0]; Real sd = 0;
00022    for (i = 1; i < n; i++)
00023    {
00024       if (amin > a[i]) amin = a[i]; else if (amax < a[i]) amax = a[i];
00025       mean += a[i];
00026    }
00027    mean /= n;
00028    for (i = 0; i < 20; i++) count[i]=0;
00029    for (i = 0; i < n; i++)
00030    {
00031      Real rat= (amax != amin) ? (a[i] - amin)/(amax - amin) : 1.0;
00032      j = (int)( 19.999 * rat ); count[j]++;
00033      Real diff = a[i] - mean; sd += diff*diff;
00034    }
00035    sd = sqrt(sd/(n-1));
00036    j = 0;
00037    for (i = 0; i < 20; i++) { if (j < count[i]) j = count[i]; }
00038    if (j > 70) { for (i = 0; i < 20; i++) count[i] = (int)((70L*count[i])/j); }
00039 
00040    al_trace( "\n" );
00041    for (i = 0; i < 20; i++)
00042         {
00043                 al_trace( "\n|" );
00044                 for (j = 1; j < count[i]; j = j+1) 
00045                         al_trace( "*" );
00046         }
00047 /*   al_trace( "\n%s\np. mean = %9f, p. var = %9f\ns. mean = %9f, s. var = %9f\n" , 
00048                 rx->Name() , rx->Mean() , rx->Variance() , mean , sd * sd );*/
00049 
00050    delete a;
00051 }

int main int  argc,
char **  argv
 

Definition at line 22 of file tryrand.cpp.

References Hist(), Histogram(), Real, Random::Set(), and test2().

00023 {
00024 
00025         Random::Set(0.46875);
00026         long n = 1000;
00027     Uniform u , u1 , u2 , u3 , u4 , u5;
00028 
00029         SumRandom       *sr;
00030 
00031         sr = new SumRandom( 2 * u * 2 * u );
00032         Hist( sr , 1000 , 42 );
00033         delete sr;
00034 
00035    {
00036 
00037       SumRandom sr1 = (u * u) + (1 - (u * u));
00038       Histogram(&sr1,n);
00039       cout << "Mean and variance should be 0.25 and 0.048611\n";
00040       
00041       SumRandom sr2 = u + 2 * u * u;
00042       Histogram(&sr2,n);
00043       cout << "Mean and variance should be 0.25 and 0.048611\n";
00044       
00045       SumRandom sr3 = (u-0.5) * (u-0.5) + u;
00046           Histogram(&sr3,n);
00047       cout << "Mean and variance should be 0.0 and 0.006944\n";
00048 
00049    }
00050 
00051    Real* s1; Real* s2; Real* s3; Real* s4;
00052    cout << "\nBegin test\n";   // Forces cout to allocate memory at beginning
00053    { s1 = new Real[8000]; delete [] s1; }
00054    { s3 = new Real; delete s3;}
00055    {
00056 
00057       Real* A = new Real[3750];
00058 
00059       long n_large = 1000000;
00060 
00061 /*
00062       Uniform u;
00063       SumRandom sr1 = u*u;
00064       SumRandom sr2 = (u-0.5)*(u-0.5);
00065       Histogram(&sr1,n);
00066       cout << "Mean and variance should be 0.25 and 0.048611" << endl;
00067       Histogram(&sr2,n);
00068       cout << "Mean and variance should be 0.0 and 0.006944" << endl;
00069 */   
00070                 //      RING    
00071 #if 1
00072 /*
00073                 {
00074       static Real probs[]={.05 , 0.25, .025 , 0 , 0.025 ,.3 , .025 , 0 , 0.025 , .25 , .05};
00075       DiscreteGen discrete(11,probs); Normal nn;
00076       SumRandom sr = discrete + (nn * 0.25)(2) + 10.0;
00077       Histogram(&discrete,n);
00078       Histogram(&sr,n);
00079    }
00080 */
00081                 {
00082       static Real probs[]={.05 , 0.25, .02 , 0.01 , 0.02 ,.3 , .02 , 0.01 , 0.02 , .25 , .05};
00083       DiscreteGen discrete(11,probs); Normal nn;
00084       SumRandom sr = discrete + (nn * 0.25)(2) + 10.0;
00085       Hist(&discrete,n,50);
00086       Hist(&sr,n,50);
00087                 }
00088 
00089                 {
00090                         
00091                 static Real probs[]={ 0.4 , 0.000 ,0.000 ,0.12 
00092                         ,0.000 ,0.000 ,0.000
00093                         ,0.12 
00094                         ,0.000 ,0.000 ,0.000,0.000 ,0.000 ,0.000,0.000 ,0.000
00095                         ,0.12 , 0.12 , 0.12 };
00096       DiscreteGen discrete(19,probs); 
00097       Hist(&discrete,n,19);
00098                 Uniform nn;     //      Normal nn;
00099       {
00100                         SumRandom sr = discrete + (nn * 0.1)( 2 );
00101 //                      Hist(&sr,n,7);
00102                 }
00103       {
00104                         SumRandom sr = discrete + (nn * 0.25)( 2 );
00105                         Hist(&sr,n,19);
00106                 }
00107       {
00108                         SumRandom sr = discrete + (nn * 0.5)( 2 );
00109 //                      Hist(&sr,n,7);
00110                 }
00111       {
00112                         SumRandom sr = discrete + (nn * 0.75)( 2 );
00113 //                      Hist(&sr,n,7);
00114                 }
00115    }
00116 #endif
00117 
00118 //      test1(n);
00119                 test2(n);
00120 //      test3(n_large);
00121 //      test4(n);
00122 
00123       cout << "\nEnd of tests\n";
00124 
00125       delete [] A;
00126    }
00127 
00128  /*
00129  { s2 = new Real[8000]; delete [] s2; }
00130    cout << "\n(The following memory checks are probably not valid with all\n";
00131    cout << "compilers - see documentation)\n";
00132    cout << "\nChecking for lost memory: "
00133       << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
00134    if (s1 != s2) cout << " - error\n"; else cout << " - ok\n\n";
00135    { s4 = new Real; delete s4;}
00136    cout << "\nChecking for lost memory: "
00137       << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
00138    if (s3 != s4) cout << " - error\n"; else cout << " - ok\n\n";
00139 */
00140    return 0;
00141 }

void test1 int   ) 
 

Definition at line 24 of file tryrand1.cpp.

References Histogram(), SymGen::Next(), NORMAL10(), Real, TRIANG(), and UNIF().

00025 {
00026    Normal nn;
00027    Uniform uniform;
00028    { for (int i=0; i<20; i++) cout << nn.Next() << "\n" << flush; }
00029    { Constant c(5.0);                         Histogram(&c, n); }
00030    { Uniform u;                               Histogram(&u, n); }
00031    { SumRandom sr=uniform(3)-1.5;             Histogram(&sr, n); }
00032    { SumRandom sr=uniform-uniform;            Histogram(&sr, n); }
00033    { Normal normal;                           Histogram(&normal, n); }
00034    { Cauchy cauchy;                           Histogram(&cauchy, n); }
00035    { AsymGenX normal10(NORMAL10, 10.0);       Histogram(&normal10, n); }
00036    cout << "Mean and variance should be 10.0 and 4.0" << endl;
00037    { AsymGenX uniform2(UNIF,0.5);             Histogram(&uniform2, n); }
00038    cout << "Mean and variance should be 0.5 and 0.083333" << endl;
00039    { SymGenX triang(TRIANG);                  Histogram(&triang, n); }
00040    cout << "Mean and variance should be 0 and 0.16667" << endl;
00041    { Poisson p(0.25);                         Histogram(&p, n); }
00042    { Poisson p(10.0);                         Histogram(&p, n); }
00043    { Poisson p(16.0);                         Histogram(&p, n); }
00044    { Binomial b(18,0.3);                      Histogram(&b, n); }
00045    { Binomial b(19,0.3);                      Histogram(&b, n); }
00046    { Binomial b(20,0.3);                      Histogram(&b, n); }
00047    { Binomial b(58,0.3);                      Histogram(&b, n); }
00048    { Binomial b(59,0.3);                      Histogram(&b, n); }
00049    { Binomial b(60,0.3);                      Histogram(&b, n); }
00050    { Binomial b(18,0.05);                     Histogram(&b, n); }
00051    { Binomial b(19,0.05);                     Histogram(&b, n); }
00052    { Binomial b(20,0.05);                     Histogram(&b, n); }
00053    { Binomial b(98,0.01);                     Histogram(&b, n); }
00054    { Binomial b(99,0.01);                     Histogram(&b, n); }
00055    { Binomial b(100,0.01);                    Histogram(&b, n); }
00056    { Binomial b(18,0.95);                     Histogram(&b, n); }
00057    { Binomial b(19,0.95);                     Histogram(&b, n); }
00058    { Binomial b(20,0.95);                     Histogram(&b, n); }
00059    { Binomial b(98,0.99);                     Histogram(&b, n); }
00060    { Binomial b(99,0.99);                     Histogram(&b, n); }
00061    { Binomial b(100,0.99);                    Histogram(&b, n); }
00062    { NegativeBinomial nb(100,6.0);            Histogram(&nb, n); }
00063    { NegativeBinomial nb(11,9.0);             Histogram(&nb, n); }
00064    { NegativeBinomial nb(11,1.9);             Histogram(&nb, n); }
00065    { NegativeBinomial nb(11,0.10);            Histogram(&nb, n); }
00066    { NegativeBinomial nb(1.5,1.9);            Histogram(&nb, n); }
00067    { NegativeBinomial nb(1.0,1.9);            Histogram(&nb, n); }
00068    { NegativeBinomial nb(0.3,19);             Histogram(&nb, n); }
00069    { NegativeBinomial nb(0.3,1.9);            Histogram(&nb, n); }
00070    { NegativeBinomial nb(0.3,0.05);           Histogram(&nb, n); }
00071    { NegativeBinomial nb(100.8,0.18);         Histogram(&nb, n); }
00072    { ChiSq c(1,2.0);                          Histogram(&c, n); }
00073    { ChiSq c(2,2.0);                          Histogram(&c, n); }
00074    { ChiSq c(3,2.0);                          Histogram(&c, n); }
00075    { ChiSq c(4,2.0);                          Histogram(&c, n); }
00076    { ChiSq c(1    );                          Histogram(&c, n); }
00077    { ChiSq c(2    );                          Histogram(&c, n); }
00078    { ChiSq c(3    );                          Histogram(&c, n); }
00079    { ChiSq c(4    );                          Histogram(&c, n); }
00080    { Gamma g1(1.0);                           Histogram(&g1, n); }
00081    { Gamma g2(0.5);                           Histogram(&g2, n); }
00082    { Gamma g3(1.01);                          Histogram(&g3, n); }
00083    { Gamma g4(2.0);                           Histogram(&g4, n); }
00084    { Pareto p1(0.5);                          Histogram(&p1, n); }
00085    { Pareto p2(1.5);                          Histogram(&p2, n); }
00086    { Pareto p3(2.5);                          Histogram(&p3, n); }
00087    { Pareto p4(4.5);                          Histogram(&p4, n); }
00088    Real probs[]={.1,.3,.05,.11,.05,.04,.05,.05,.1,.15};
00089    Real val[]={2,3,4,6,8,12,16,24,32,48};
00090    { DiscreteGen discrete(10,probs);          Histogram(&discrete, n); }
00091    { DiscreteGen discrete(10,probs,val);      Histogram(&discrete, n); }
00092 }

void test2 int   ) 
 

Definition at line 11 of file tryrand2.cpp.

References Histogram(), Ars::Graf::M, Real, and Ars::Graf::X.

Referenced by main().

00012 {
00013    {
00014       Uniform u;
00015       SumRandom sr1 = -u;
00016       SumRandom sr2 = 5.0-u;
00017       SumRandom sr3 = 5.0-2*u;
00018       MixedRandom sr4 = u(0.5) + (-u)(0.5);
00019       Histogram(&sr1,n);
00020       cout << "Mean and variance should be -0.5 and 0.083333" << endl;
00021       Histogram(&sr2,n);
00022       cout << "Mean and variance should be 4.5 and 0.083333" << endl;
00023       Histogram(&sr3,n);
00024       cout << "Mean and variance should be 4.0 and 0.33333" << endl;
00025       Histogram(&sr4,n);
00026       cout << "Mean and variance should be 0.0 and 0.33333" << endl;
00027    }
00028    {
00029       Uniform u;
00030       SumRandom sr1 = u*u;
00031       SumRandom sr2 = (u-0.5)*(u-0.5);
00032       Histogram(&sr1,n);
00033       cout << "Mean and variance should be 0.25 and 0.048611" << endl;
00034       Histogram(&sr2,n);
00035       cout << "Mean and variance should be 0.0 and 0.006944" << endl;
00036    }
00037    {
00038       static Real probs[]={.4,.2,.4};
00039       DiscreteGen discrete(3,probs); Normal nn;
00040       SumRandom sr=discrete+(nn*0.25)(2)+10.0;
00041       Histogram(&discrete,n);
00042       Histogram(&sr,n);
00043    }
00044    {
00045       static Real probs[]={.2,.8};
00046       Random* rv[2];
00047       Normal nn; SumRandom snn=nn*10.0;
00048       rv[0]=&snn; rv[1]=&nn;
00049       MixedRandom mr(2,probs,rv);
00050       MixedRandom mr2=snn(.2)+nn(.8);
00051       Histogram(&mr2,n);
00052       Histogram(&mr,n);
00053    }
00054 
00055    {
00056       Normal nn; Constant c1(0.0); Constant c2(10.0);
00057       MixedRandom mr=c1(0.25)+(nn+5.0)(0.5)+c2(0.25);
00058       Histogram(&mr,n);
00059    }
00060    {
00061       Cauchy cy; Normal nn; SumRandom sr = cy*.01+nn+2.0;
00062       MixedRandom mr=sr(0.1)+nn(0.9);
00063       Histogram(&sr,n);
00064       Histogram(&mr,n);
00065    }
00066    {
00067       Constant c0(0.0); Constant c1(1.0); Constant c2(2.0);
00068       Constant c3(3.0); Constant c4(4.0); Constant c5(5.0);
00069       MixedRandom mr=c0(.1)+c1(.2)+c2(.2)+c3(.2)+c4(.2)+c5(.1);
00070       Histogram(&mr,n);
00071    }
00072    {
00073       Uniform u; Normal nl;
00074       MixedRandom m=( u(3)-1.5 )(0.5)+( nl*0.5+10.0 )(0.5);
00075       Histogram(&m,n);
00076    }
00077    {
00078       Real prob[] = { .25, .25, .25, .25 };
00079       Real val[] = { 3, 1.5, 1, 0.75 };
00080       DiscreteGen X(4, prob, val);
00081       SumRandom Y = 1/X;
00082       Histogram(&Y,n);  // mean should be 0.83333, variance should be 0.13889
00083       cout << "Mean and variance should be 0.83333 and 0.13889" << endl;
00084       Uniform U;
00085       SumRandom Z = U/X;
00086       Histogram(&Z,n);  // mean should be 0.41667, variance should be 0.10417
00087       cout << "Mean and variance should be 0.41667 and 0.10417" << endl;
00088    }
00089    {
00090       int M = 5, N = 9;
00091       ChiSq Num(M); ChiSq Den(N);
00092       SumRandom F = (double)N/(double)M * Num / Den;
00093       Histogram(&F,n);
00094       cout << "Mean and variance should be " << N / (double)(N-2)
00095          << " and "
00096          << 2 * N * N * (M+N-2) / (double)(M * (N-2) * (N-2) * (N-4))
00097          << endl;
00098    }
00099 }

void test3 int   ) 
 

Definition at line 25 of file tryrand3.cpp.

References KS(), NormalDF(), Real, TestBinomial(), TestDiscreteGen(), TestNegativeBinomial(), TestPoisson(), and Ars::Graf::X.

00026 {
00027    cout << endl;
00028 
00029    // Do chi-squared tests to discrete data
00030    cout << "ChiSquared tests" << endl;
00031 
00032    {
00033       Real p[] = { 0.05, 0.10, 0.05, 0.5, 0.01, 0.01, 0.03, 0.20, 0.05 };
00034       TestDiscreteGen(9, p, n);
00035    }
00036 
00037    {
00038       Real p[] = { 0.4, 0.2, 0.1, 0.05, 0.025, 0.0125, 0.00625, 0.00625, 0.2 };
00039       TestDiscreteGen(9, p, n);
00040    }
00041 
00042 
00043    TestNegativeBinomial(200.3, 0.05, n);
00044    TestNegativeBinomial(150.3, 0.15, n);
00045    TestNegativeBinomial(100.8, 0.18, n);
00046    TestNegativeBinomial(100.8, 1.22, n);
00047    TestNegativeBinomial(100.8, 9.0, n);
00048    TestNegativeBinomial(10.5, 0.18, n);
00049    TestNegativeBinomial(10.5, 1.22, n);
00050    TestNegativeBinomial(10.5, 9.0, n);
00051    TestNegativeBinomial(0.35, 0.18, n);
00052    TestNegativeBinomial(0.35, 1.22, n);
00053    TestNegativeBinomial(0.35, 9.0, n);
00054 
00055    TestBinomial(100, 0.45, n);
00056    TestBinomial(100, 0.25, n);
00057    TestBinomial(100, 0.02, n);
00058    TestBinomial(100, 0.01, n);
00059    TestBinomial(49, 0.60, n);
00060    TestBinomial(21, 0.70, n);
00061    TestBinomial(10, 0.90, n);
00062    TestBinomial(10, 0.25, n);
00063    TestBinomial(10, 0.10, n);
00064 
00065    TestPoisson(0.75, n);
00066    TestPoisson(4.3, n);
00067    TestPoisson(10, n);
00068    TestPoisson(100, n);
00069 
00070    Real* data = new Real[n];
00071    if (!data) Throw(Bad_alloc());
00072 
00073 // Apply KS test to a variety of continuous distributions
00074 //    - use cdf transform to convert to uniform
00075 
00076    cout << endl;
00077    cout << "Kolmogorov-Smirnoff tests" << endl;
00078    cout << "25%, 5%, 1%, .1% upper points are 1.019, 1.358, 1.628, 1.950"
00079       << endl;
00080    cout << "5% lower point is 0.520" << endl;
00081 
00082    {
00083       ChiSq X(1, 1.44);
00084       for (int i = 0; i < n; i++)
00085       {
00086          Real x = sqrt(X.Next());
00087          data[i] = NormalDF(x - 1.2) - NormalDF(-x - 1.2);
00088       }
00089       cout << X.Name() << ":   "  << KS(data, n) << endl;
00090    }
00091 
00092    {
00093       ChiSq X(4);
00094       for (int i = 0; i < n; i++)
00095          { Real x = 0.5 * X.Next(); data[i] = (1+x)*exp(-x); }
00096       cout << X.Name() << ":   "  << KS(data, n) << endl;
00097    }
00098 
00099    {
00100       ChiSq X(2);
00101       for (int i = 0; i < n; i++) data[i] = exp(-0.5 * X.Next());
00102       cout << X.Name() << ":   "  << KS(data, n) << endl;
00103    }
00104 
00105    {
00106       Pareto X(0.5);
00107       for (int i = 0; i < n; i++)
00108          { Real x = X.Next(); data[i] = 1.0 / sqrt(x); }
00109       cout << X.Name() << ":   "  << KS(data, n) << endl;
00110    }
00111 
00112    {
00113       Pareto X(1.5);
00114       for (int i = 0; i < n; i++)
00115          { Real x = X.Next(); data[i] = 1.0 / (x * sqrt(x)); }
00116       cout << X.Name() << ":   "  << KS(data, n) << endl;
00117    }
00118 
00119    {
00120       Normal X;
00121       for (int i = 0; i < n; i++)
00122          { Real x = X.Next(); data[i] = NormalDF(x); }
00123       cout << X.Name() << ":   "  << KS(data, n) << endl;
00124    }
00125 
00126    {
00127       Normal N; SumRandom X = 10 + 5 * N;
00128       for (int i = 0; i < n; i++)
00129          { Real x = X.Next(); data[i] = NormalDF((x-10)/5); }
00130       cout << X.Name() << ":   "  << KS(data, n) << endl;
00131    }
00132 
00133    {
00134       Normal N; Cauchy C; MixedRandom X = N(0.9) + C(0.1);
00135       for (int i = 0; i < n; i++)
00136       {
00137          Real x = X.Next();
00138          data[i] = 0.9*NormalDF(x)+0.1*(atan(x)/3.141592654 + 0.5);
00139       }
00140       cout << X.Name() << ":   "  << KS(data, n) << endl;
00141    }
00142 
00143    {
00144       Normal N; MixedRandom X = N(0.9) + (10*N)(0.1);
00145       for (int i = 0; i < n; i++)
00146       {
00147          Real x = X.Next();
00148          data[i] = 0.9*NormalDF(x)+0.1*NormalDF(x/10);
00149       }
00150       cout << X.Name() << ":   "  << KS(data, n) << endl;
00151    }
00152 
00153    {
00154       Normal  X0; SumRandom X = X0 * 0.6 + X0 * 0.8;
00155       for (int i = 0; i < n; i++)
00156          { Real x = X.Next(); data[i] = NormalDF(x); }
00157       cout << X.Name() << ":   "  << KS(data, n) << endl;
00158    }
00159 
00160    {
00161       Normal X1;
00162       MixedRandom X = X1(0.2) + (X1 * 2.5 + 1.1)(0.35) + (X1 + 2.3)(0.45);
00163       for (int i = 0; i < n; i++)
00164       {
00165          Real x = X.Next();
00166          data[i] = 0.20 * NormalDF(x)
00167                  + 0.35 * NormalDF((x - 1.1) / 2.5)
00168                  + 0.45 * NormalDF(x - 2.3);
00169       }
00170       cout << X.Name() << ":   "  << KS(data, n) << endl;
00171    }
00172 
00173    {
00174       Gamma X(0.5);
00175       for (int i = 0; i < n; i++)
00176          { Real x = X.Next(); data[i] = 2.0 * NormalDF(-sqrt(2 * x)); }
00177       cout << X.Name() << ":   "  << KS(data, n) << endl;
00178    }
00179 
00180    {
00181       Gamma X(3);
00182       for (int i = 0; i < n; i++)
00183          { Real x = X.Next(); data[i] = (1+x+0.5*x*x)*exp(-x); }
00184       cout << X.Name() << ":   "  << KS(data, n) << endl;
00185    }
00186 
00187    {
00188       Gamma X1(0.85); Gamma X2(2.15); SumRandom X = X1 + X2;
00189       for (int i = 0; i < n; i++)
00190          { Real x = X.Next(); data[i] = (1+x+0.5*x*x)*exp(-x); }
00191       cout << X.Name() << ":   "  << KS(data, n) << endl;
00192    }
00193 
00194    {
00195       Gamma X1(0.75); Gamma X2(0.25); SumRandom X = X1 + X2;
00196       for (int i = 0; i < n; i++) data[i] = exp(-X.Next());
00197       cout << X.Name() << ":   "  << KS(data, n) << endl;
00198    }
00199 
00200    {
00201       Gamma X(2);
00202       for (int i = 0; i < n; i++)
00203          { Real x = X.Next(); data[i] = (1+x)*exp(-x); }
00204       cout << X.Name() << ":   "  << KS(data, n) << endl;
00205    }
00206 
00207    {
00208       Exponential X;
00209       for (int i = 0; i < n; i++) data[i] = exp(-X.Next());
00210       cout << X.Name() << ":   "  << KS(data, n) << endl;
00211    }
00212 
00213    {
00214       Cauchy X;
00215       for (int i = 0; i < n; i++) data[i] = atan(X.Next())/3.141592654 + 0.5;
00216       cout << X.Name() << ":   "  << KS(data, n) << endl;
00217    }
00218 
00219    {
00220       Cauchy X0; SumRandom X = X0 * 0.3 + X0 * 0.7;
00221       for (int i = 0; i < n; i++) data[i] = atan(X.Next())/3.141592654 + 0.5;
00222       cout << X.Name() << ":   "  << KS(data, n) << endl;
00223    }
00224 
00225    {
00226       Uniform X;
00227       for (int i = 0; i < n; i++) data[i] = X.Next();
00228       cout << X.Name() << ":   "  << KS(data, n) << endl;
00229    }
00230 
00231    delete [] data;
00232 
00233 
00234 }

void test4 int   ) 
 

Definition at line 9 of file tryrand4.cpp.

References RandomPermutation::Next(), and RandomCombination::Next().

00010 {
00011    cout << endl << endl;
00012 
00013    {
00014       cout << "Doing permutations" << endl;
00015       RandomPermutation RP;
00016       int i, j;
00017       int p[10];
00018 
00019       cout << "... select 10 items from 100...119 without replacement" << endl;
00020       for (i = 1; i <= 10; i++)
00021       {
00022          RP.Next(20,10,p,100);
00023          for (j = 0; j < 10; j++) cout << p[j] << " ";
00024          cout << "\n";
00025       }
00026       cout << "\n";
00027 
00028       cout << "... select 10 items from 100...109 without replacement" << endl;
00029       for (i = 1; i <= 10; i++)
00030       {
00031          RP.Next(10,10,p,100);
00032          for (j = 0; j < 10; j++) cout << p[j] << " ";
00033          cout << "\n";
00034       }
00035       cout << "\n";
00036    }
00037 
00038    cout << endl << endl;
00039 
00040    {
00041       cout << "Doing combinations" << endl;
00042       RandomCombination RC;
00043       int i, j;
00044       int p[10];
00045 
00046       cout << "... select 10 items from 100...119 without replacement" << endl;
00047       for (i = 1; i <= 10; i++)
00048       {
00049          RC.Next(20,10,p,100);
00050          for (j = 0; j < 10; j++) cout << p[j] << " ";
00051          cout << "\n";
00052       }
00053       cout << "\n";
00054 
00055       cout << "... select 10 items from 100...109 without replacement" << endl;
00056       for (i = 1; i <= 10; i++)
00057       {
00058          RC.Next(10,10,p,100);
00059          for (j = 0; j < 10; j++) cout << p[j] << " ";
00060          cout << "\n";
00061       }
00062       cout << "\n";
00063    }
00064 }


Generated on Fri Dec 5 04:06:09 2003 for Borqueror by doxygen 1.3.3