MC - Simple Linear Regression with Intercept

Model Building

y = β 0 + β 1 x + u y=\beta_0+\beta_1x+u y=β0+β1x+u

Variable Declaration

y i = β 0 + β 1 x i + u i       { ( x i , y i ) , i = 1 , ⋯   , n } y_i=\beta_0+\beta_1x_i+u_i\ \ \ \ \ \{(x_i,y_i), i=1,\cdots,n\} yi=β0+β1xi+ui     {(xi,yi),i=1,,n} >

Variable (Parameter)MeaningSet Value (Distribution)
x x xexplanatory variable x ∼ N o r m a l ( 5 , 50 ) x\sim Normal(5,50) xNormal(5,50)
u u uerror term u ∼ N o r m a l ( 0 , 200 ) u \sim Normal(0,200) uNormal(0,200)
β 0 \beta_0 β0intercept parameter25
β 1 \beta_1 β1slope parameter3
n n nsimple size10
N N Nfrequency of simpling10000
  • Note:
  1. There is almost no limit to the value of X X X in OLS estimates, and in fact, we allow both X X X and Y Y Y to be R . V . R.V. R.V.
  2. R . V . R.V. R.V. Y Y Y is generated by the following formula: Y = β 0 + β 1 X + u Y=\beta_0+\beta_1X+u Y=β0+β1X+u

Results

y ^ i = β ^ 0 + β ^ 1 x i   ,      i = 1 , ⋯   , n \hat y_i=\hat\beta_0+\hat\beta_1x_i\ , \ \ \ \ i=1,\cdots,n y^i=β^0+β^1xi ,    i=1,,n

EstimatorMean ErrorStandard Deviation of Error
β ^ 0 \hat\beta_0 β^00.6815081.85889
β ^ 1 \hat\beta_1 β^1-0.009121.31418
  • Mean Abs. Error: 232.94118
  • Mean Sq. Error: 40849.77162

Graphs

  1. Graph1: Distribution of β ^ 0 \hat\beta_0 β^0
  2. Graph2: DIstribution of β ^ 1 \hat\beta_1 β^1 (hint: There are some problems with the vertical data but they don’t affect the distribution.)
from IPython.display import Image
Image(filename='Graphs1.png')

在这里插入图片描述

from IPython.display import Image
Image(filename='Graphs2.png')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9mF7qXJi-1590808438914)(output_5_0.png)]

Coding

import numpy as np
import pandas as pd
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import statsmodels.api as sma
from scipy.stats import norm

# Params Setting
b0 = 25
b1 = 3
n = 10
N = 10000
x = np.random.normal(5, 50, n)
b0_set = np.zeros(N)
b1_set = np.zeros(N)
yab_set = np.zeros(N)
ysq_set = np.zeros(N)

# MC Simulation
i = 0
while i < N:
    u = np.random.normal(0, 200, n)
    y = b0 + b1 * x + u
    model = sma.OLS(y,sma.add_constant(x))
    results = model.fit()
    b0hat = results.params[0]
    b1hat = results.params[1]
    b0_set[i] = b0hat
    b1_set[i] = b1hat
    yhat = b0hat + b1hat * x
    yab_set[i] = np.abs(y,yhat).mean()
    ysq_set[i] = np.sum((y-yhat)**2) / n
    i +=1

# Results
dict_result = {}
dict_result['Mean Error'] = [b0_set.mean()-b0, b1_set.mean()-b1]
dict_result['Std. Error'] = [b0_set.std(), b1_set.std()]
df = pd.DataFrame(dict_result, index=['beta0_hat', 'beta1_hat'])
print(df)
print("Mean Abs. Error: {}".format(yab_set.mean()))
print("Mean Sq. Error: {}".format(ysq_set.mean()))

# Graphs
plt.figure(figsize=(25,15))
plt.style.use('seaborn')

# Graph1: Distribution of estimator beta0_hat
plt.subplot(221)
plt.title('Graph1: Distribution of estimator beta0_hat',fontsize=25, c='black')
plt.grid(True)
plt.xlabel('Value of beta0_hat',fontsize=15,c='black')
plt.ylabel('Probability',fontsize=15,c='black')
m0,bins0,patches0 = plt.hist(b0_set, bins=50, density=True, color='blue', edgecolor='black', linewidth=1, alpha=0.7)
B0_set = norm.pdf(bins0, b0_set.mean(), b0_set.std())
plt.plot(bins0,B0_set,'r--')

# Graph2: Distribution of estimator beta1_hat
plt.subplot(222)
plt.title('Graph2: Distribution of estimator beta1_hat',fontsize=25, c='black')
plt.grid(True)
plt.xlabel('Value of beta1_hat',fontsize=15,c='black')
plt.ylabel('Probability',fontsize=15,c='black')
m1,bins1,patches1 = plt.hist(b1_set, bins=40, density=True, color='r', edgecolor='black', linewidth=1, alpha=0.5)
B1_set = norm.pdf(bins1, b1_set.mean(), b1_set.std())
plt.plot(bins1,B1_set,'b-.')
plt.savefig('Graphs1&2.png', bbox_inches='tight')
plt.show()

原创文章,转载请注意出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值