-
-
Save pat-hanbury/aa068ace0a0121cc59884b83ffb59031 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
# this is the template of our weight function g(x) | |
def g_of_x(x, A, lamda): | |
e = 2.71828 | |
return A*math.pow(e, -1*lamda*x) | |
def inverse_G_of_r(r, lamda): | |
return (-1 * math.log(float(r)))/lamda | |
def get_IS_variance(lamda, num_samples): | |
""" | |
This function calculates the variance if a Monte Carlo | |
using importance sampling. | |
Args: | |
- lamda (float) : lamdba value of g(x) being tested | |
Return: | |
- Variance | |
""" | |
A = lamda | |
int_max = 5 | |
# get sum of squares | |
running_total = 0 | |
for i in range(num_samples): | |
x = get_rand_number(0, int_max) | |
running_total += (f_of_x(x)/g_of_x(x, A, lamda))**2 | |
sum_of_sqs = running_total / num_samples | |
# get squared average | |
running_total = 0 | |
for i in range(num_samples): | |
x = get_rand_number(0, int_max) | |
running_total += f_of_x(x)/g_of_x(x, A, lamda) | |
sq_ave = (running_total/num_samples)**2 | |
return sum_of_sqs - sq_ave | |
# get variance as a function of lambda by testing many | |
# different lambdas | |
test_lamdas = [i*0.05 for i in range(1, 61)] | |
variances = [] | |
for i, lamda in enumerate(test_lamdas): | |
print(f"lambda {i+1}/{len(test_lamdas)}: {lamda}") | |
A = lamda | |
variances.append(get_IS_variance(lamda, 10000)) | |
clear_output(wait=True) | |
optimal_lamda = test_lamdas[np.argmin(np.asarray(variances))] | |
IS_variance = variances[np.argmin(np.asarray(variances))] | |
print(f"Optimal Lambda: {optimal_lamda}") | |
print(f"Optimal Variance: {IS_variance}") | |
print(f"Error: {(IS_variance/10000)**0.5}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment