-
Notifications
You must be signed in to change notification settings - Fork 2
/
hist.cpp
56 lines (51 loc) · 1.78 KB
/
hist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#define WANT_STREAM
#define WANT_MATH
#include "include.h"
#include "boolean.h"
#include "extreal.h"
#include "newran.h"
#ifdef use_namespace
//using namespace NEWRAN;
#endif
void Histogram(Random* rx, int n) // draw histogram with n obsv
{
int i,j; int count[20];
Real* a = new Real[n];
if (!a) {
std::cout<<"\nNo memory for Histogram\n";
return;
}
for (i = 0; i < n; i++) a[i] = rx->Next();
Real amax = a[0]; Real amin = a[0]; Real mean = a[0]; Real sd = 0;
for (i = 1; i < n; i++)
{
if (amin > a[i]) amin = a[i]; else if (amax < a[i]) amax = a[i];
mean += a[i];
}
mean /= n;
for (i = 0; i < 20; i++) count[i]=0;
for (i = 0; i < n; i++)
{
Real rat= (amax != amin) ? (a[i] - amin)/(amax - amin) : 1.0;
j = (int)( 19.999 * rat ); count[j]++;
Real diff = a[i] - mean; sd += diff*diff;
}
sd = sqrt(sd/(n-1));
j = 0;
for (i = 0; i < 20; i++) { if (j < count[i]) j = count[i]; }
if (j > 70) { for (i = 0; i < 20; i++) count[i] = (int)((70L*count[i])/j); }
std::cout << "\n";
for (i = 0; i < 20; i++)
{ std::cout << "\n|"; for (j = 1; j < count[i]; j = j+1) std::cout << "*"; }
std::cout << "\n" << rx->Name() << "\n";
std::cout << "p. mean = " << std::setw(9) << std::setprecision(5) << rx->Mean()
<< ", p. var = " << std::setw(9) << std::setprecision(5)
<< rx->Variance() << "\n";
std::cout << "s. mean = " << std::setw(9) << std::setprecision(5) << mean
<< ", s. var = " << std::setw(9) << std::setprecision(5) << sd*sd
<< ", max = " << std::setw(9) << std::setprecision(5) << amax
<< ", min = " << std::setw(9) << std::setprecision(5) << amin
<< "\n";
std::cout << std::flush;
delete a;
}