import numpy as np
import seaborn as sns
from tqdm import trange
from scipy.stats import norm
from scipy.stats import uniform
import matplotlib.pyplot as plt

# plotting params
%matplotlib inline
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['xtick.labelsize'] = 8
plt.rcParams['ytick.labelsize'] = 8
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['figure.titlesize'] = 12
plt.rcParams['figure.figsize'] = (15.0, 8.0)
sns.set_style("white")

# path params
plot_dir = './plots/'

def importance_sampling(num_simulations, num_samples, plot=False):
    probability_values = []    
    num_simulations = int(num_simulations)
    num_samples = int(num_samples)
    
    
    for i in range(num_simulations):
        mu_1, sigma_1 = 0, 1
        mu_2, sigma_2 = -5, 5
        
        # setup pdfs
        pdf_norm = norm(mu_1, sigma_1)
        pdf_uni = uniform(mu_1_n, sigma_1_n)

        X = np.random.normal(mu_1, sigma_1, num_samples)
        Y = np.random.uniform(mu_2, sigma_2, num_samples)
        
        # calculate current
        num = pdf_norm.pdf(X)
        denum = uniform_pdf.pdf(Y)
        weights = num / denum
        
        # calculate X^2
        f_of_X = pow(X,2)
        f_of_Y = pow(Y,2)

        # calculate the expected value
        probability_values.append((weights.dot(f_of_X))/num_samples)
        
    # take statistical measures of the results from Impotance Sampling
    mean_expected_value = np.mean(probability_values)
    var_expected_value = np.var(probability_values)
    real_expected_value = sigma_1
    max_expected_value = max(probability_values)
    min_expected_value = min(probability_values)
    
    # print resuts
    print("===================================================")
    print("{} simulations".format(num_simulations))
    print("Importance Sampling of size {}".format(num_samples))
    print("Simulated Expected value: {}".format(mean_expected_value))
    print("Variance of the Expected value: {}".format(var_expected_value))
    print("Max $E(X^2)$: {} Min $E(X^2)$: {}".format(max_expected_value, min_expected_value))
    print("Actual Expected value: {}".format(real_expected_value))
    print("===================================================\n")
 
        
    
    IS_distribution = weights*f_of_X
    
    if plot:
        # histogram generated by Importance Sampling
        n, bins, patches = plt.hist(probability_value, 50, density=1, facecolor='green', alpha=0.75)
        plt.ylabel('Frequency')
        plt.xlabel(r'Expected Value')
        plt.title("Histogram, Importance Sampling")
        plt.grid(True)
        plt.savefig(plot_dir + 'histogram_expected_value{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
        
        # histogram from uniform samples weighted by the normal distribution
        n, bins, patches = plt.hist(IS_distribution, 50, density=1, facecolor='green', alpha=0.75)
        plt.ylabel('Number of Samples')
        plt.xlabel(r'$w*X^2$, X sampled from uniform distribution')
        plt.title("Histogram, Importance Sampling")
        plt.grid(True)
        plt.savefig(plot_dir + 'uniform_sample_histogram{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
        
        # histogram from normal samples
        n, bins, patches = plt.hist(f_of_Y, 50, density=1, facecolor='blue', alpha=0.75)
        plt.ylabel('Number of Samples')
        plt.xlabel(r'$Y^2$, sampled from normal distribution')
        plt.title("Histogram")
        plt.grid(True)
        plt.savefig(plot_dir + 'normal_sample_histogram{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
        
        # weights 
        plt.scatter(f_of_X, weights)
        plt.ylabel('weights')
        plt.xlabel(r'$X^2$')
        plt.title("Weights values")
        plt.grid(True)
        plt.savefig(plot_dir + 'weights{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()

cdf16e8817f9955a6e073d9d5a480156630814aa
import numpy as np
import seaborn as sns
from tqdm import trange
from scipy.stats import norm
from scipy.stats import uniform
import matplotlib.pyplot as plt

# plotting params
%matplotlib inline
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['xtick.labelsize'] = 8
plt.rcParams['ytick.labelsize'] = 8
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['figure.titlesize'] = 12
plt.rcParams['figure.figsize'] = (15.0, 8.0)
sns.set_style("white")

# path params
plot_dir = './plots/'

def importance_sampling(num_simulations, num_samples, plot=False):
    probability_values = []    
    num_simulations = int(num_simulations)
    num_samples = int(num_samples)
    
    
    for i in range(num_simulations):
        mu_1, sigma_1 = 0, 1
        mu_2, sigma_2 = -5, 5
        
        # setup pdfs
        pdf_norm = norm(mu_1, sigma_1)
        pdf_uni = uniform(mu_1_n, sigma_1_n)

        X = np.random.normal(mu_1, sigma_1, num_samples)
        Y = np.random.uniform(mu_2, sigma_2, num_samples)
        
        # calculate current
        num = pdf_norm.pdf(X)
        denum = uniform_pdf.pdf(Y)
        weights = num / denum
        
        # calculate X^2
        f_of_X = pow(X,2)
        f_of_Y = pow(Y,2)

        # calculate the expected value
        probability_values.append((weights.dot(f_of_X))/num_samples)
        
    # take statistical measures of the results from Impotance Sampling
    mean_expected_value = np.mean(probability_values)
    var_expected_value = np.var(probability_values)
    real_expected_value = sigma_1
    max_expected_value = max(probability_values)
    min_expected_value = min(probability_values)
    
    # print resuts
    print("===================================================")
    print("{} simulations".format(num_simulations))
    print("Importance Sampling of size {}".format(num_samples))
    print("Simulated Expected value: {}".format(mean_expected_value))
    print("Variance of the Expected value: {}".format(var_expected_value))
    print("Max $E(X^2)$: {} Min $E(X^2)$: {}".format(max_expected_value, min_expected_value))
    print("Actual Expected value: {}".format(real_expected_value))
    print("===================================================\n")
 
        
    
    IS_distribution = weights*f_of_X
    
    if plot:
        # histogram generated by Importance Sampling
        n, bins, patches = plt.hist(probability_value, 50, density=1, facecolor='green', alpha=0.75)
        plt.ylabel('Frequency')
        plt.xlabel(r'Expected Value')
        plt.title("Histogram, Importance Sampling")
        plt.grid(True)
        plt.savefig(plot_dir + 'histogram_expected_value{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
        
        # histogram from uniform samples weighted by the normal distribution
        n, bins, patches = plt.hist(IS_distribution, 50, density=1, facecolor='green', alpha=0.75)
        plt.ylabel('Number of Samples')
        plt.xlabel(r'$w*X^2$, X sampled from uniform distribution')
        plt.title("Histogram, Importance Sampling")
        plt.grid(True)
        plt.savefig(plot_dir + 'uniform_sample_histogram{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
        
        # histogram from normal samples
        n, bins, patches = plt.hist(f_of_Y, 50, density=1, facecolor='blue', alpha=0.75)
        plt.ylabel('Number of Samples')
        plt.xlabel(r'$Y^2$, sampled from normal distribution')
        plt.title("Histogram")
        plt.grid(True)
        plt.savefig(plot_dir + 'normal_sample_histogram{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
        
        # weights 
        plt.scatter(f_of_X, weights)
        plt.ylabel('weights')
        plt.xlabel(r'$X^2$')
        plt.title("Weights values")
        plt.grid(True)
        plt.savefig(plot_dir + 'weights{}.pdf'.format(num_samples), format='pdf', dpi=300)
        plt.show()
