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

In [32]:
import numpy as np
import matplotlib.pyplot as plt

import math
import scipy.special as spsp

import scipy.stats as spst

%matplotlib inline

import sympy

Let’s assume that in the previous example, we used unnormalized $f(x)=exp(-\lambda x)$ instead of the normalized pdf. Can we determine the best $\lambda$ using the previous method?
In [40]:
#analytical
sympy.init_printing()
x=sympy.Symbol(“x”)
lmbda=sympy.Symbol(“lambda”, positive=True)

pdf_t=2/sympy.sqrt(2*sympy.pi)*sympy.exp(-x**2/2)
#use unnormalized pdf
pdf_p1=sympy.exp(-lmbda*x)
#use normalized pdf
pdf_p2=lmbda*sympy.exp(-lmbda*x)
In [44]:
#cvalue normalized
ratio1=pdf_t/pdf_p1
ratio2=pdf_t/pdf_p2
#first order
print(sympy.solve(sympy.diff(ratio1,x),x))
print(sympy.solve(sympy.diff(ratio2,x),x))

[lambda]
[lambda]
In [45]:
#c using unnormalized proposal
c1=ratio1.subs({x:lmbda}).simplify()
c1
Out[45]:
$\displaystyle \frac{\sqrt{2} e^{\frac{\lambda^{2}}{2}}}{\sqrt{\pi}}$
In [47]:
#c using normalized proposal
c2=ratio2.subs({x:lmbda}).simplify()
c2
Out[47]:
$\displaystyle \frac{\sqrt{2} e^{\frac{\lambda^{2}}{2}}}{\sqrt{\pi} \lambda}$
In [48]:
#first order
sympy.solve(sympy.diff(c1,lmbda),lmbda)
Out[48]:
$\displaystyle \left[ \right]$
In [50]:
#first prder
sympy.solve(sympy.diff(c2,lmbda),lmbda)
Out[50]:
$\displaystyle \left[ 1\right]$
In [0]: