Search code examples
transformmontecarlo

Calculate the improper integral Monte Carlo method


For exaple, I'm trying to transform the integral:

The improper integral

I need to transform it to an integral that goes from 0 to 1 (or from a to b) in order to apply the algorithm of Monte Carlo I implemented. I already have Monte Carlo function, which calculate definite integrals, but I need to transform this improper integral to definite integral and I really don't know how to do it.

Ideally, I want to transform this integral (from -inf to inf):

enter image description here

Also I tried to do transformation, which I found on Inthernet (integration by substitution: x=-ln(y), dx=-1/y), but it doesn't work:

enter image description here

So how can I get transformation of this integral?


Solution

  • I need to transform it to an integral that goes from 0 to 1 (or from a to b) in order to apply the algorithm of Monte Carlo I implemented.

    No, you don't.

    if you have integral

    0 w(x) g(x) dx

    you could sample from w(x) and compute mean value of g(x) at sampled points.

    You integral

    0 e-x cos(x) dx

    is pretty perfect for such approach - you sample from e-x and compute E[cos(x)]

    Along the lines (Python 3.9, Win10 x64)

    import numpy as np
    
    rng = np.random.default_rng()
    
    N = 1000000
    U = rng.random(N)
    
    W = -np.log(1.0 - U) # sampling exp(-x)
    
    G = np.cos(W)
    
    ans = np.mean(G)
    print(ans)
    

    will print something like

    0.5002769491719996
    

    And concerning your second integral, see What is the issue in my array division step?