Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # To apply importance sampling correctly, we need to account for the fact that the
- # importance distribution q(x) is not exactly the same as the integrand's distribution.
- # We will use lambda = 1 for the exponential distribution q(x).
- lambda_param = 1.0
- # Compute the importance sampling weights
- # Since the joint pdf of the exponential distribution for 10 independent variables is
- # q(x_1, ..., x_10) = lambda^10 * exp(-lambda * (x_1 + ... + x_10)), the weights would be
- # the function we want to integrate divided by this q(x).
- # However, because we're sampling from the exponential distribution itself, we can ignore the q(x) in the denominator.
- # Now, compute the f(x)/q(x) for each sample
- weights = np.exp(-1/(2**20 * np.prod(samples, axis=1))) * \
- np.prod(samples ** (np.arange(1, 11)/11 - 1), axis=1) * \
- (lambda_param ** 10)
- # Estimate the integral as the mean of the weights
- integral_estimate = np.mean(weights)
- integral_estimate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement