StatMech
Loading...
Searching...
No Matches
maximum_randN.c File Reference

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
16 {
17 if (argc < 3) {
18 printf("Usage: 1.This 2.ndata 3.InFile\n");
19 exit(EXIT_FAILURE);
20 }
21 Integer_t i, j, n, count;
22 Integer_t seed=0;
23 Integer_t const ndata = atoi(argv[1]);
24 Integer_t const MAX = 1000;
25
26 double max, maxData, dtemp;
27 double rand, rand1, rand2;
28 double CDF[MAX]; for(i = 0;i < MAX; ++i) CDF[i] = 0;
29
30 Integer_t subMax;
31 Integer_t *histogram, maxCount;
32 double mean, stddev, mode;
33 double binwidth=0.05;
34
35 Integer_t const BUFSIZE = 1000;
36 char str[BUFSIZE];
37 char *filename = str;
38
39 FILE *Infp;
40 strcpy(str, argv[2]);
41 Infp = fopen(filename, "r");
42 if (Infp == NULL) {
43 printf("Can't open a file %s\n", filename);
44 exit(EXIT_FAILURE);
45 }
46 skip_header(Infp);
47 count=0;
48 while( fscanf(Infp, "%lld %lld %le", &i, &j, &dtemp) != EOF ) {
49 count+=1;
50 CDF[i] = dtemp;
51 printf("(count=%lld) %lld %lld %le\n", count, i, j, dtemp);
52 fflush(stdout);
53 }
54
55
56 FILE *Outfp;
57 sprintf(str, "Dist_maximum_raw.txt");
58 Outfp = fopen(filename, "w");
59 if (Outfp == NULL) {
60 printf("Can't open a file %s\n", filename);
61 exit(EXIT_FAILURE);
62 }
63 fprintf(Outfp, "#(n=%lld, seed=%lld) Distribution of maximum of |x| \n\n", n, seed);
64
65 init_genrand(seed);
66 maxData = NAN;
67 mean = 0;
68 stddev = 0;
69 for(i = 0;i < ndata; ++i) {
70 //******************** Choose the number of random variables
71 rand1 = genrand_real3();
72 for(j = 0;j < MAX; ++j) if(CDF[j] > rand1) break;
73 n = j-1;
74 //******************** (END) Choose the number of random variables
75 max = NAN;
76 for(j = 0;j < n; ++j) {
77 rand1 = genrand_real3();
78 rand = -log(rand1);
79 max = fmax(max, rand);
80 // max = rand;
81 }
82 maxData = fmax(maxData, max);
83 mean += max;
84 stddev += max*max;
85 fprintf(Outfp, "%lld %le %lld\n", i, max, n);
86 }
87 mean /= (double)ndata;
88 stddev /= (double)ndata;
89 stddev = sqrt( stddev -mean*mean );
90 binwidth *= stddev;
91 subMax = (Integer_t)(maxData/binwidth) +1;
92 histogram = alloc_ivector(subMax); for(i = 0;i < subMax; ++i) histogram[i] = 0;
93
94 sprintf(str, "Dist_maximum_raw.txt");
95 Infp = fopen(filename, "r");
96 if (Infp == NULL) {
97 printf("Can't open a file %s\n", filename);
98 exit(EXIT_FAILURE);
99 }
100 skip_header(Infp);
101
102 count=0;
103 while ( fscanf(Infp, "%lld %le %lld", &i, &max, &j) != EOF ) {
104 histogram[ (Integer_t)(max/binwidth) ] += 1;
105 count += 1;
106 }
107 maxCount = 0;
108 for(i = 0;i < subMax; ++i) {
109 if(maxCount < histogram[i]) {
110 mode = binwidth*(i+0.5);
111 maxCount = histogram[i];
112 }
113 }
114
115 sprintf(str, "Dist_maximum.txt");
116 Outfp = fopen(filename, "w");
117 if (Outfp == NULL) {
118 printf("Can't open a file %s\n", filename);
119 exit(EXIT_FAILURE);
120 }
121 fprintf(Outfp, "# Input: %s\n", argv[2]);
122 fprintf(Outfp, "# mean=%le, stddev=%le, mode=%le\n", mean, stddev, mode);
123 fprintf(Outfp, "# 1.Maximum value 2.Probability density\n\n");
124 for(i = 0;i < subMax; ++i) {
125 fprintf(Outfp, "%le %le\n", binwidth*(i+0.5), histogram[i]/(binwidth*count));
126 }
127
128 return 0;
129}
double CDF(double x)
Definition maximum.c:16
MKL_INT Integer_t
Definition mytypes.hpp:359