# -*- coding: utf-8 -*-
"""
Created on Mon Nov 20 09:10:14 2023

Two components Gaussian mixture

@author: Márton
"""

import numpy as np;
from scipy.stats import norm;
from matplotlib import pyplot as plt;
from sklearn.mixture import GaussianMixture;

# Parameters
pi = 0.5;  # weight of first component
mu1 = -5;
mu2 = 5;
sig1 = 1;
sig2 = 3;
res = 0.01;

x = np.arange(-10,10,res);

fx = pi*norm.pdf(x, loc=mu1, scale=sig1) + (1-pi)*norm.pdf(x, loc=mu2, scale=sig2);

plt.plot(x, fx);
plt.show();

# Generating random sample from the mixture
n = 1000000;  # sample size
mix = np.zeros((n,1));
w = np.random.uniform(0,1,n);
for i in range(n):
    if w[i]<pi:
        mix[i] = np.random.normal(mu1,sig1,1);
    else:
        mix[i] = np.random.normal(mu2,sig2,1);
        
GM = GaussianMixture(n_components=2);   
GM.fit(mix);  

