1
$\begingroup$

I am trying to understand the gamma mixture models, especially the significance of the 'loc' parameter in scipy.stats. In the code below, I generate a mixture distribution with two gamma distributions with different shape and scale parameters.

import scipy.stats as st
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

#Define shape parameters
alpha1 = 2
alpha2 = 2

#Define rate parameters
beta1 = 3
beta2 = 5

#Define location parameters
loc1 = 0
loc2 = 0

#Define weights
pi_1 = 0.2

#Create gamma rvs and concatenate them to generate mixture distribution
k = st.bernoulli(pi_1).rvs(30000)
x1 = st.gamma(a = alpha1, loc = loc1, scale = 1/beta1).rvs(sum(k==1))
x2 = st.gamma(a = alpha2, loc = loc2, scale = 1/beta2).rvs(sum(k==0))
x = np.concatenate([x1,x2])

#Plot the histogram
fig = plt.figure(figsize=(8,4))
sns.histplot(x=x)
plt.vlines(x1, 0, 50, color='orange', alpha=0.1)
plt.vlines(x2, 0, 50, color='green', alpha=0.1)
plt.xlabel('x')

plt.show() 

When I have the 'loc1' and 'loc2' values as 0, I don't observe two gamma distributions visually from the histogram. The resulting histogram is visualized as a single gamma function as seen in

1

When I change the 'loc1' = 2 and 'loc2' = 3, the resulting histogram can be seen as a mixture of two gamma distributions as in

2

Can anyone help me to understand this difference? Is 'loc' the mean of the variables and why do two distributions with zero mean but different alpha and beta appear as a single distribution?

$\endgroup$

1 Answer 1

4
$\begingroup$

Here are the densities you're sampling from with the first set of parameter values:

enter image description here

The x1 values are drawn from the green density and the x2 from the orange density. The black density is the mixture of the two, with weights pi_1 and 1-pi_1, respectively. The mixture distribution doesn't have distinct peaks because the two overlap.

In your second example, you've shifted the support of both densities, i.e. instead of $(0, \infty)$, x1 takes values in $(2, \infty)$, and x2 takes values in $(3, \infty)$. This is the result: enter image description here Notice that the mixture density now has two distinct peaks.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you very much! $\endgroup$
    – amitha
    Jun 28, 2023 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.