00001 #define WANT_STREAM
00002 #include "include.h"
00003 #include "newran.h"
00004
00005 #ifdef use_namespace
00006 using namespace NEWRAN;
00007 #endif
00008
00009 void Histogram(Random*, int);
00010
00011 void test2(int n)
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);
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);
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 }