CS计算机代考程序代写 In [5]:

In [5]:
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as spst
import sympy
%matplotlib inline
In [6]:
sympy.init_printing()
a=sympy.Symbol(“a”)
b=sympy.Symbol(“b”)
u=sympy.Symbol(“u”)
x=sympy.Symbol(“x”)
In [9]:
#computing cdf from pdf

#this is the cdf of continuous distribution
pdf=1/(b-a)
cdf=sympy.integrate(pdf,(x,a,x)).simplify() # with respecty of x from a to x
In [12]:
#computing inverse cdf from cdf
# let cdf(above)=u, simply, we could get the result below
inv_cdf=sympy.solve(cdf-u,x)[0]

In [16]:
#convert inverse cdf solution to a function

#what values do you want to pass into the function
#what operation do you want to do inside the function
#translate the sympy operations into numpy functions
sampling=sympy.lambdify((a,b,u),inv_cdf,”numpy”)
In [18]:
#sampling
sympy.init_printing(False)
#a=1, b=3, what if my u/cdf=0.1?
sampling(1,3,np.random.rand(10))
#corresponding sample equals to 0.2
#is still the sympy thing without using sympy.init_printing(False)
Out[18]:
array([2.7290056 , 1.74152241, 1.11121009, 1.55062162, 1.5982005 ,
2.28109327, 1.39794813, 1.78360066, 2.66897855, 2.24478104])
In [0]: