4

I'm trying to fit a mixture of 3 normal distributions to my log of transformed data and I'm a bit confused how to do it. I tried gmm function from scikit learn python but it does not seem to work correctly.

g = mixture.GMM(n_components=3)
g.fit(lines)  
f1 = arange(0, 13, 0.01)   
f2 = arange(0, 13, 0.01)    
f3 = arange(0, 13, 0.01)    
f = arange(0, 13, 0.01)

for x in arange(0, 13, 0.01):       
    f1[x] = numpy.round(g.weights_[0],5) * numpy.exp(-numpy.power(x - means[0], 2) / 2 *  numpy.power(covars[0], 2)) * (1 / (covars[0] * numpy.power(2 * pi, 0.5)))    
    f2[x] = numpy.round(g.weights_[1],5) * numpy.exp(-numpy.power(x - means[1], 2) / 2 * numpy.power(covars[1], 2)) * (1 / (covars[1] * numpy.power(2 * pi, 0.5)))    
    f3[x] = numpy.round(g.weights_[2],5) * numpy.exp(-numpy.power(x - means[2], 2) / 2 * numpy.power(covars[2], 2)) * (1 / (covars[2] * numpy.power(2 * pi, 0.5)))

f=f1+f2+f3   
plt.plot(f)   
plt.show()

In the end I want to get a pdf plot of 3 components, i.e. f=f1+f2+f3. However it doesn't work.

Is it because I'm trying to fit a mixture of normals to lognormal data?

Could you please explain my mistake and/or advise me on a package that is used for fitting mixture of lognormals?

user27241
  • 201
  • 3
  • 10

1 Answers1

0

This is the I make a mixture of 3 LogNormal distributions using OpenTURNS library

import openturns as ot
from openturns.viewer import View

distribution1 = ot.LogNormal(0.01, 0.8, 3.)
distribution2 = ot.LogNormal(0.01, 0.5, 0.)
distribution3 = ot.LogNormal(0.1, 1., 7.)

final_dist = ot.Mixture([distribution1, distribution2, distribution3]) 

graph = final_dist.drawPDF()

View(graph, add_legend=False)

mixture of 3 lognormal distributions

Is it what you are looking for?

You can access the value of the resulting PDF in any point p by calling final_dist.computePDF([p]) and fit it to your data.

If you are acquainted to matplotlib and numpy, here's the same plot using these

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-3, 18, 100).reshape(-1, 1)
plt.plot(x, final_dist.computePDF(x))

enter image description here

Jean A.
  • 291
  • 1
  • 17