<a href="https://colab.research.google.com/github/pranjalrawat007/Econometrics-in-R/blob/main/Method_of_Moment_Estimators.ipynb" target="_parent"></a>


```python
from numpy.random import binomial, exponential, normal
from numpy import mean, sqrt
import matplotlib.pyplot as plt
from sympy import symbols, solve, Eq
import warnings
warnings.filterwarnings("ignore")
```


```python
# Bernoulli

p = 0.8 #parameter
N = 1000 #sample size
K = 100 #sampling iterations
p_mm = [] #parameter estimates
for i in range(0,K):
    X = binomial(1, p, N)
    mm1 = mean(X)
    p_mm.append(mm1) # p = E(X)

plt.hist(X)
plt.title(f'Particular Random Sample Distribution')
plt.show()

plt.hist(p_mm)
plt.title(f'{K} estimates of p_mm')
plt.vlines(x=p, ymin=0, ymax=10, color = 'b')
plt.show()
```


```python
# Exponential

β = 10 #parameter
N = 1000 #sample size
K = 100 #sampling iterations
β_mm = [] #parameter estimates
for i in range(0,K):
    X = exponential(β, N)
    mm1 = mean(X)
    β_mm.append(mm1) # β = E(X)

plt.hist(X)
plt.title(f'Particular Random Sample Distribution')
plt.show()

plt.hist(β_mm)
plt.title(f'{K} estimates of β_mm')
plt.vlines(x=β, ymin=0, ymax=10, color = 'b')
plt.show()
```


```python

```


```python
# Normal 

μ = 10 #true mean
σ = 2 #true var
N = 1000 #sample size
K = 100 #sampling iterations
μ_mm = []
σ_mm = []
for i in range(0,K):
    X = normal(μ,σ, N) # N realisations of normal(μ,σ)
    mm1 = mean(X) # first moment estimate
    mm2 = mean(X**2) # second moment estimate
    μ_mm.append(mm1) # μ = E(X)
    σ_mm.append(sqrt(mm2-mm1**2)) # σ2 = E(X2) - μ^2

plt.hist(X)
plt.title(f'Particular Random Sample Distribution')
plt.show()

plt.hist(μ_mm)
plt.title(f'{K} estimates of μ_mm')
plt.vlines(x=μ, ymin=0, ymax=10, color = 'b')
plt.show()

plt.hist(σ_mm)
plt.title(f'{K} estimates of σ_mm')
plt.vlines(x=σ, ymin=0, ymax=10, color = 'b')
plt.show()
```


```python
# Binomial

p = 0.8 #parameter
n = 10 #parameter
N = 1000 #sample size
K = 100 #sampling iterations
p_mm_list = [] #parameter estimates
n_mm_list = [] #parameter estimates
for i in range(0,K):
    X = binomial(n, p, N)
    p_mm, n_mm = symbols('p_mm n_mm') #endog vars
    Condition2 = Eq(mean(X**2) - n_mm*p_mm*(1-p_mm) - (n_mm*p_mm)**2) # equation1
    Condition1 = Eq(mean(X) - n_mm*p_mm) # equation 2
    p_mm, n_mm = solve((Condition1, Condition2),(p_mm, n_mm))[0]
    p_mm_list.append(float(p_mm))
    n_mm_list.append(float(n_mm))

plt.hist(X)
plt.title(f'Particular Random Sample Distribution')
plt.show()

plt.hist(p_mm_list)
plt.title(f'{K} estimates of p_mm')
plt.vlines(x=p, ymin=0, ymax=10, color = 'b')
plt.show()

plt.hist(n_mm_list)
plt.title(f'{K} estimates of n_mm')
plt.vlines(x=n, ymin=0, ymax=10, color = 'b')
plt.show()
```


```python
# Solving the Moment Conditions in the Binomial Case
p_mm, n_mm = symbols('p_mm n_mm') #endog vars
Condition2 = Eq(mean(X**2) - n_mm*p_mm*(1-p_mm) - (n_mm*p_mm)**2) # equation1
Condition1 = Eq(mean(X) - n_mm*p_mm) # equation 2
```


```python
Condition2
```




$\displaystyle - n_{mm}^{2} p_{mm}^{2} - n_{mm} p_{mm} \left(1 - p_{mm}\right) + 64.554 = 0$




```python
Condition1
```




$\displaystyle - n_{mm} p_{mm} + 7.932 = 0$




```python
solve((Condition1, Condition2),(p_mm, n_mm))
```




    [(0.793573373676248, 9.99529503271363)]




```python
# Gamma

p = 0.8 #parameter
n = 10 #parameter
N = 1000 #sample size
K = 100 #sampling iterations
p_mm_list = [] #parameter estimates
n_mm_list = [] #parameter estimates
for i in range(0,K):
    X = binomial(n, p, N)
    p_mm, n_mm = symbols('p_mm n_mm') #endog vars
    Condition2 = Eq(mean(X**2) - n_mm*p_mm*(1-p_mm) - (n_mm*p_mm)**2) # equation1
    Condition1 = Eq(mean(X) - n_mm*p_mm) # equation 2
    p_mm, n_mm = solve((Condition1, Condition2),(p_mm, n_mm))[0]
    p_mm_list.append(float(p_mm))
    n_mm_list.append(float(n_mm))

plt.hist(X)
plt.title(f'Particular Random Sample Distribution')
plt.show()

plt.hist(p_mm_list)
plt.title(f'{K} estimates of p_mm')
plt.vlines(x=p, ymin=0, ymax=10, color = 'b')
plt.show()

plt.hist(n_mm_list)
plt.title(f'{K} estimates of n_mm')
plt.vlines(x=n, ymin=0, ymax=10, color = 'b')
plt.show()
```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```

7683d96055d17c3a289cdba2218d95c34e20aac2
ipynb
Jupyter Notebook
1_Examples/MOM.ipynb
rawatpranjal/Statistical-Inference
a1430539a4f0a353fcafe60b3e3d977732e30b45
MIT
1_Examples/MOM.ipynb
rawatpranjal/Statistical-Inference
a1430539a4f0a353fcafe60b3e3d977732e30b45
MIT
1_Examples/MOM.ipynb
rawatpranjal/Statistical-Inference
a1430539a4f0a353fcafe60b3e3d977732e30b45
MIT
Qwen/Qwen-72B
1. YES
2. YES