Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scikitlearn- How can I get cdf of a gaussian mixture model?

Right now I do something like this, and im wonedring if there are better ways.

import numpy as np
from scipy import integrate
from sklearn.mixture import GaussianMixture as GMM

model = GMM(n, covariance_type = "full").fit(X)

def cdf(x):
 return integrate.quad(lambda t: np.exp(model.score(t)), -inf, x)[0]
like image 919
h3h325 Avatar asked Jan 01 '26 08:01

h3h325


1 Answers

The CDF of mixed Gaussian distributions with CDF of F_1,F_2,F_3...,and weights of ω_1,ω_2,ω_3..., equals to F_mixed = ω_1 * F_1 + ω_2 * F_2 + ω_3 * F_3 + ... Therefore, the answer is:

from scipy.stats import norm

weights = [0.163, 0.131, 0.486, 0.112, 0.107]
means = [45.279, 55.969, 49.315, 53.846, 61.953]
covars = [0.047, 1.189, 3.632, 0.040, 0.198]


def mix_norm_cdf(x, weights, means, covars):
    mcdf = 0.0
    for i in range(len(weights)):
        mcdf += weights[i] * norm.cdf(x, loc=means[i], scale=covars[i])
    return mcdf


print(mix_norm_cdf(50, weights, means, covars))

output

0.442351546658755
like image 135
James Avatar answered Jan 02 '26 20:01

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!