# Mixture of Gaussian and Uniform Distribution

First we define a PDF:


```
from sympy import *
%matplotlib inline
init_printing()
sigma, sigma1, p, mu, mu1, x, a, b  = symbols('sigma, sigma1, p, mu, mu1, x, a, b')
sigma2, mu2 = symbols('sigma2, mu2')
pdf = p * (1 / sigma1) * (exp((-1/2)*((x-mu1)/sigma1)**2)/sqrt(2* pi)) + (1-p) * (1/(b-a))
pdf
```

Let's compute the CDF:


```
cdf = integrate(pdf,x)
cdf = simplify(cdf)
cdf
```

Now lets define mu and solve for mu1:


```
mu_simpli = p * mu1 + (1-p) * ((1/2) * (a+b))
mu1 = solve(Eq(mu_simpli, mu), mu1)
mu1
```


```
sigma_simpli = p * ( (mu1**2) + (sigma1**2)) + (1-p) * ((mu2**2) + (sigma2**2))
sigma1 = solve(Eq(sigma_simpli, sigma), sigma1)
sigma1
```

Now we can calculate sigma1 and mu1 with the simplified versions of mu and sigma


```
sigma1 = solve(Eq(sigma_simpli, sigma), sigma1)
mu1 = solve(Eq(mu_simpli, mu), mu1)
othersigma =  sigma1 = ( sigma / p) ** (1 / sqrt(2))/2
sigma1, othersigma
```

Let's try it with a mixture of two gaussians:


```
pdf = p * (1 / sigma1) * (exp((-1/2)*((x-mu1)/sigma1)**2)/sqrt(2* pi)) + (1-p) * (1 / sigma2) * (exp((-1/2)*((x-mu2)/sigma2)**2)/sqrt(2* pi))
pdf
```


```
cdf = integrate(pdf,x)
```


```
cdf = simplify(cdf)
cdf
```


```
f = pdf

```


```
mu2 = (1/2) * (a+b)
```


```
sigma2 = (1/12)*(b-a)**2
```


```
delta1 = mu1 - mu
delta2 = mu2 - mu
```


```
mean = p* mu1 + (1-p)*mu2
mu1 = mean
```


```
variance_simpli = p * (mu1**2 + sigma1**2) + (1-p) * (mu2**2 + sigma2**2)
variance_simpli
```


```
sigma1 = solve(Eq(variance_simpli, sigma), sigma1)
```


```
sigma1, mu1
```


```

```

176681693a53e40470635da806f86edb0430ef0e
ipynb
Jupyter Notebook
probabilistic_programming/mixture-distribution-tests/Mixture Model.ipynb
juliusf/ipython-notebooks
6aad8a92164927486dcd5f3054fb5b2ae9d5cc67
Apache-2.0
probabilistic_programming/mixture-distribution-tests/Mixture Model.ipynb
juliusf/ipython-notebooks
6aad8a92164927486dcd5f3054fb5b2ae9d5cc67
Apache-2.0
probabilistic_programming/mixture-distribution-tests/Mixture Model.ipynb
juliusf/ipython-notebooks
6aad8a92164927486dcd5f3054fb5b2ae9d5cc67
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES