-
-
Save pat-hanbury/b2c028b1cd0336fb7501a144e84c01d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_crude_MC_variance(num_samples): | |
""" | |
This function returns the variance fo the Crude Monte Carlo. | |
Note that the inputed number of samples does not neccissarily | |
need to correspond to number of samples used in the Monte | |
Carlo Simulation. | |
Args: | |
- num_samples (int) | |
Return: | |
- Variance for Crude Monte Carlo approximation of f(x) (float) | |
""" | |
int_max = 5 # this is the max of our integration range | |
# get the average of squares | |
running_total = 0 | |
for i in range(num_samples): | |
x = get_rand_number(0, int_max) | |
running_total += f_of_x(x)**2 | |
sum_of_sqs = running_total*int_max / num_samples | |
# get square of average | |
running_total = 0 | |
for i in range(num_samples): | |
x = get_rand_number(0, int_max) | |
running_total = f_of_x(x) | |
sq_ave = (int_max*running_total/num_samples)**2 | |
return sum_of_sqs - sq_ave |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line 25 is it supposed to be
running_total += f_of_x(x)
?