Python 计量经济学作业(个股和股指分析)

import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
import yfinance as yf

stocks="KRZ.IR", "^ISEQ"
data = yf.download(stocks, start="2020-01-09", end="2022-01-09")
data = data[['Adj Close']]
data.columns = ["KRZ", "ISEQ"]
data.head()

returns = data.pct_change()
returns = returns.dropna()
returns.columns = ['rKRZ', "rISEQ"]
returns

# OLS regression find the relationship between the daily return of KRZ.IR and ISEQ
formula = "rKRZ ~ rISEQ"
results = smf.ols(formula, returns).fit()
print(results.summary())

# Null hypothesis test
# H 0 : β 1 = 1
# H 1 : β 1 ≠ 1
formula = "rKRZ ~ rISEQ"
hypotheses = "rISEQ = 1"
results = smf.ols(formula, returns).fit()
f_test = results.f_test(hypotheses)
print(f_test)

# Multiple hypothesis tests
# H 0 : β 0 = 1, β 1 =1
# H 1 : β 0 ≠ 1, β 1 ≠ 1
formula = "rKRZ ~ rISEQ"
hypotheses = "rISEQ = Intercept =1"
results = smf.ols(formula, returns).fit()
f_test = results.f_test(hypotheses)
print(f_test)

plt.figure(1)
plt.plot(results.resid)
plt.xlabel('Date')
plt.ylabel('Residuals')
plt.grid(True)
plt.show()

你可能感兴趣的:(Python,作业,python)