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

tryrand2.cpp File Reference

#include "include.h"
#include "newran.h"

Include dependency graph for tryrand2.cpp:

Include dependency graph

Go to the source code of this file.

Defines

#define WANT_STREAM

Functions

void Histogram (Random *, int)
void test2 (int n)


Define Documentation

#define WANT_STREAM
 

Definition at line 1 of file tryrand2.cpp.


Function Documentation

void Histogram Random ,
int 
 

Definition at line 16 of file hist.cpp.

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

Referenced by main(), test1(), and test2().

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 }

void test2 int  n  ) 
 

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 }


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