I am new to Python so some of my questions or ideas might be silly, but...
I want to graph a distribution D(x). m and s2 are some given real numbers. I have been told that the best way to graph D(x) is to write a function which solves the integral (which is inside a function D(x)) for every x.
where chi2 is so defined:
So, as much as I know math, I am supposed to integrate first and then I can solve for each x, correct me if I am wrong.
I have also been told to calculate the integral numerically, but I do not know how to do it because the functions includes symbols. I have already tried using symbolical integration (despite what I have been told), but the kernel never ends the process of integration, as well as when I try to compute it numerically. When I tried integrationg numericaly, I used lamdify, of course.
So here are my codes: 1. trying to solve symbolic integral
from sympy import symbols, integrate, sqrt, exp, oo
s2= 0.0628777415586
m= 5.02422436191
x, n, z=symbols ('x, n, z')
integrate(exp(-n/(z+1) * (x-m)**2/2*s2) * 1/2 / sqrt(z+1), (z, 0, oo))
Not working, Kernel never stops. [I put 1/2 instead of chi2 in formula, intented to change it later]
An alternative is trying to solve numerical integral (calling it from a function D(x))
import scipy.integrate
from scipy.stats import chi2
from math import *
s2= 0.0628777415586
m= 5.02422436191
def numint(z, n, x):
return exp(-n/(z+1) * (x-m)**2/2*s2) * scipy.stats.chi2(z) / sqrt(z+1)
scipy.integrate.quad(numint, 0, np.inf, args=(1, 2))
Error comes from the line with return and says:
TypeError: unsupported operand type(s) for *: 'float' and 'rv_frozen'
I suppose that the problem here comes becaue of chi2 which is rv_frozen, but how to make it work? Is there anything in my code that is wrong? I have no idea if it is right what I wrote and how to fix this... I have been working on this for a very long time and am a bit desperate, so any help is welcome.