Advertisement
VladNitu

chat_sample_multidim_integral

Mar 28th, 2024
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. # To apply importance sampling correctly, we need to account for the fact that the
  2. # importance distribution q(x) is not exactly the same as the integrand's distribution.
  3.  
  4. # We will use lambda = 1 for the exponential distribution q(x).
  5. lambda_param = 1.0
  6.  
  7. # Compute the importance sampling weights
  8. # Since the joint pdf of the exponential distribution for 10 independent variables is
  9. # q(x_1, ..., x_10) = lambda^10 * exp(-lambda * (x_1 + ... + x_10)), the weights would be
  10. # the function we want to integrate divided by this q(x).
  11. # However, because we're sampling from the exponential distribution itself, we can ignore the q(x) in the denominator.
  12.  
  13. # Now, compute the f(x)/q(x) for each sample
  14. weights = np.exp(-1/(2**20 * np.prod(samples, axis=1))) * \
  15.           np.prod(samples ** (np.arange(1, 11)/11 - 1), axis=1) * \
  16.           (lambda_param ** 10)
  17.  
  18. # Estimate the integral as the mean of the weights
  19. integral_estimate = np.mean(weights)
  20.  
  21. integral_estimate
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement