1
votes

I am trying to run a code that outputs a Gaussian distribtuion by integrating the 1-D gaussian distribution equation using Monte Carlo integration. I am trying to use the mcint module. I defined the gaussian equation and the sampler function that is used in the mcint module. I am not sure what the 'measure' part in the mcint function does and what it should be set to. Does anyone know what measure is supposed to be? And how do I know what to set it as?

from matplotlib import pyplot as mp
import numpy as np
import mcint
import random


#f equation
def gaussian(x,x0,sig0,time,var):
    [velocity,diffussion_coeffient] = var
    mu = x0 + (velocity*time)
    sig = sig0 + np.sqrt(2.0*diffussion_coeffient*time)
    return (1/(np.sqrt(2.0*np.pi*(sig**2.0))))*(np.exp((-(x-mu)**2.0)/(2.0*(sig**2.0))))

#random variables that are generated during the integration
def sampler(varinterval):
    while True:
        velocity = random.uniform(varinterval[0][0],varinterval[0][1])
        diffussion_coeffient = random.uniform(varinterval[1][0],varinterval[1][1])
        yield (velocity,diffussion_coeffient)



if __name__ == "__main__":

    x0 = 0
    #ranges for integration
    velocitymin = -3.0
    velocitymax = 3.0
    diffussion_coeffientmin = 0.01
    diffussion_coeffientmax = 0.89
    varinterval = [[velocitymin,velocitymax],[diffussion_coeffientmin,diffussion_coeffientmax]]

    time = 1
    sig0 = 0.05

    x = np.linspace(-20, 20, 120)

    res = []
    for i in np.linspace(-10, 10, 120):
        result, error = mcint.integrate(lambda v: gaussian(i,x0,sig0,time,v), sampler(varinterval), measure=1, n=1000)
        res.append(result)

    mp.plot(x,res)
    mp.show()
1

1 Answers

1
votes

Is this the module you are talking about? If that's the case the whole source is only 17 lines long (at the times of writing). The relevant line is the last one, which reads:

return (measure*sample_mean, measure*math.sqrt(sample_var/n))

As you can see, the measure argument (whose default value is unity) is used to scale the values returned by the integrate method.