forked from 1chipML/1chipML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_stratified_sampling.c
158 lines (109 loc) · 4.04 KB
/
mc_stratified_sampling.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
double myFunction(double x);
void monteCarloEstimateSTD(double lowBound, double upBound, int iterations,
double mcStats[]);
void monteCarloEstimateStrat(double lowBound, double upBound, int iterations,
double mcStats[], int subdomains);
int main() {
double lowerBound, upperBound;
int iterations;
lowerBound = 0;
upperBound = 20;
double mcStats[2]; // position 0 holds the estimate, position 1 holds the STD
printf("Normal Monte Carlo Integration\n");
for (int i = 1; i < 6; i++) {
iterations = 128 * pow(4, i);
monteCarloEstimateSTD(lowerBound, upperBound, iterations, mcStats);
printf("Estimate for %.1f -> %.1f is %.3f, STD = %.4f, (%i iterations)\n",
lowerBound, upperBound, mcStats[0], mcStats[1], iterations);
}
printf("Stratified Sampling Monte Carlo Integration\n");
for (int i = 1; i < 6; i++) {
iterations = 128 * pow(4, i);
monteCarloEstimateStrat(lowerBound, upperBound, iterations, mcStats, 4);
printf("Estimate for %.1f -> %.1f is %.3f, STD = %.4f, (%i iterations)\n",
lowerBound, upperBound, mcStats[0], mcStats[1], iterations);
}
return 0;
}
double myFunction(double x)
// Function to integrate
{
return exp(-1 * pow(x - 6, 4)) + +exp(-1 * pow(x - 14, 4));
}
void monteCarloEstimateSTD(double lowBound, double upBound, int iterations,
double statsArray[])
// Function to execute Monte Carlo integration on predefined function,
// calculates STD
{
double totalSum = 0;
double totalSumSquared = 0;
int iter = 0;
while (iter < iterations - 1) {
double randNum = lowBound + (float)rand() / RAND_MAX * (upBound - lowBound);
double functionVal = myFunction(randNum);
totalSum += functionVal;
totalSumSquared += pow(functionVal, 2);
iter++;
}
double estimate =
(upBound - lowBound) * totalSum / iterations; // For normal solve
double expected = totalSum / iterations;
double expectedSquare = totalSumSquared / iterations;
double std = (upBound - lowBound) *
pow((expectedSquare - pow(expected, 2)) / (iterations - 1), 0.5);
statsArray[0] = estimate;
statsArray[1] = std;
}
void monteCarloEstimateStrat(double lowBound, double upBound, int iterations,
double statsArray[], int subdomains)
// Function to execute Monte Carlo integration on predefined function, uses
// stratified sampling of equally sized subdomains
{
double totalSum[subdomains];
double totalSumSquared[subdomains];
int iter;
// Divide the local iterations amoung the subdomains
iterations = (int)((float)iterations / subdomains);
for (int i = 0; i < subdomains; i++) {
totalSum[i] = 0;
totalSumSquared[i] = 0;
}
// Amount of change the range by each time
double increment = (upBound - lowBound) / (float)subdomains;
for (int seg = 0; seg < subdomains; seg++) {
iter = 0;
double randNum;
double functionVal;
double startRange = lowBound + seg * increment;
while (iter < iterations - 1) {
randNum = startRange + (float)rand() / RAND_MAX * increment;
functionVal = myFunction(randNum);
totalSum[seg] += functionVal;
totalSumSquared[seg] += pow(functionVal, 2);
iter++;
}
}
double estimates[subdomains];
double expecteds[subdomains];
double expectedSquares[subdomains];
double STDs[subdomains];
for (int i = 0; i < subdomains; i++) {
estimates[i] = increment * totalSum[i] / iterations; // For normal solve
expecteds[i] = totalSum[i] / iterations;
expectedSquares[i] = totalSumSquared[i] / iterations;
STDs[i] = increment * pow((expectedSquares[i] - pow(expecteds[i], 2)) /
(iterations - 1),
0.5);
}
double estimate = 0;
double std = 0;
for (int i = 0; i < subdomains; i++) {
estimate += estimates[i];
std += pow(increment, 2) * pow(STDs[i], 2) / iterations;
}
statsArray[0] = estimate;
statsArray[1] = pow(std, 0.5);
}