Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
quick example of importance sampling
%% true probability distribution
true_func = @(x) betapdf(x,1+1,1+10);
%% Do importance sampling
N = 10^6;
% uniform proposal distribution
x_samples = rand(N,1);
proposal = 1/N;
% evaluate for each sample
target = true_func(x_samples);
% calculate importance weight
w = target ./ proposal;
w = w ./ sum(w);
% resample, with replacement, according to importance weight
samples = randsample(x_samples,N,true,w);
%% plot
x = linspace(0,1,1000);
plot(x, true_func(x) )
axis square
title('importance sampling')
axis square
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment