I am trying to solve a system of two equations in Python using Sympy. This is bit trickier than a standard problem because it includes a summation in both equations, where both equations are found by taking the derivative of the negative loglikelihood of a lognormal pdf. Here is my code:
import numpy as np
from pprint import pprint
import sympy as sym
from sympy.solvers import solve
from sympy import Product, Function, oo, IndexedBase, diff, Eq, symbols, log, exp, pi, S, expand_log
from scipy.stats import lognorm
import scipy
np.random.seed(seed=111)
test = pd.DataFrame(data=lognorm.rvs(s=1,loc=2,scale=1,size=1000),columns=['y'])
x = IndexedBase('x')
i = symbols('i', positive=True)
n = symbols('n', positive=True)
mu = symbols('mu', positive=True)
sigma = symbols('sigma', positive=True)
pdf2 = 1 / (x[i] * sigma * sqrt(2 * pi)) * exp(-S.Half * ((log(x[i])-mu)/(sigma))**2)
Log_LL2 = -log(Product(pdf2, (i, 0, n-1)))
Log_LL22 = expand_log(Log_LL2, force=True)
pprint(Log_LL22)
Returns:
-Sum(-log(sigma) - log(x[i]) - log(pi)/2 - log(2)/2 - (-mu + log(x[i]))**2/(2*sigma**2), (i, 0, n - 1))
df_dmu = diff(Log_LL22, mu)
df_dsigma = diff(Log_LL22, sigma)
pprint(df_dmu )
pprint(df_dsigma )
Returns:
-Sum(-(2*mu - 2*log(x[i]))/(2*sigma**2), (i, 0, n - 1))
-Sum(-1/sigma + (-mu + log(x[i]))**2/sigma**3, (i, 0, n - 1))
solve([df_dmu.subs([(n,len(test['y'])),(x,test['y'])]),df_dsigma.subs([(n,len(test['y'])),(x,test['y'])])],mu,sigma,set=True)
This final command returns "([], set())". I am not sure how to implement this system of equations while telling the solver to substitute for x_i and n in order to solve for mu and sigma. I would also be happy not to plug in x_i and n and receive the answer in terms of x_i and n if this was possible. I understand these parameters can be solved for with scipy's fit function, however, when I compute the Hessian of the negative loglikelihood and plug in the scipy fitted parameters, the result is orders of magnitude difference between the scipy fitted parameters and the manually computed parameters.
I am running sympy 1.7.1, numpy 1.19.2 and scipy 1.5.2
Thank you!