vendredi 13 décembre 2019

How to incorporate random variables into integration?

I am trying to compute an integral that contains a lognormal variable, but i'm not sure if I am introducing the random variable in the right way in the code. I have some code below and a picture (mathjax is not working for me) of the equation I would like to integrate:

enter image description here

import numpy as np
import math 
import scipy.integrate as spi

rand = np.random.lognormal(0.0162,0.180)

def fun1(s):
    return s ** 2

def fun2(s):
    return math.exp( spi.quad(fun1,0,2)[0])

def fun3(s):
    return s * rand * fun2(s)

spi.quad(fun3,0,2)

You can run the above code and each time that you run it you will get a different answer which is indicative that the rand variable is being pulled from the LogNormal distribution. However, there are two ways that I can think of the rand variable is working in the operation.

1) The random variable is being assigned some constant value from the lognormal distribution, call it a, and when the integral is being computed, the program treats a like any other constant throughout the entirety of the integration. A similar analogy would be if you integrated 2x from 0 to 2, we know what happens with the 2.

2) Because the integrand has to be evaluated multiple times through some type of approximation algorithm, is the random variable changing on each evaluation of the integral. To be a random variable, I think this is what would be the ideal outcome.

A third option is that I am wrong about either of my two ideas on how the random variable is working within the overall integration. If somebody has any insight on how you could incorporate random variables, or confirm that I am doing it correctly, please comment below.




Aucun commentaire:

Enregistrer un commentaire