本文整理汇总了Python中statsmodels.tsa.vector_ar.var_model.VAR.fit方法的典型用法代码示例。如果您正苦于以下问题:Python VAR.fit方法的具体用法?Python VAR.fit怎么用?Python VAR.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类statsmodels.tsa.vector_ar.var_model.VAR
的用法示例。
在下文中一共展示了VAR.fit方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_results_statsmodels
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
def load_results_statsmodels(dataset):
results_per_deterministic_terms = dict.fromkeys(dt_s_list)
for dt_s_tup in dt_s_list:
endog = data[dataset]
exog = generate_exog_from_season(dt_s_tup[1], len(endog))
model = VAR(endog, exog)
results_per_deterministic_terms[dt_s_tup] = model.fit(
maxlags=4, trend=dt_s_tup[0], method="ols")
return results_per_deterministic_terms
示例2: TestVARResultsLutkepohl
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
class TestVARResultsLutkepohl(object):
"""
Verify calculations using results from Lutkepohl's book
"""
def __init__(self):
self.p = 2
if not have_pandas():
return
sdata, dates = get_lutkepohl_data("e1")
names = sdata.dtype.names
data = data_util.struct_to_ndarray(sdata)
adj_data = np.diff(np.log(data), axis=0)
# est = VAR(adj_data, p=2, dates=dates[1:], names=names)
self.model = VAR(adj_data[:-16], dates=dates[1:-16], names=names, freq="Q")
self.res = self.model.fit(maxlags=self.p)
self.irf = self.res.irf(10)
self.lut = E1_Results()
def test_approx_mse(self):
if not have_pandas():
raise nose.SkipTest
# 3.5.18, p. 99
mse2 = np.array([[25.12, 0.580, 1.300], [0.580, 1.581, 0.586], [1.300, 0.586, 1.009]]) * 1e-4
assert_almost_equal(mse2, self.res.forecast_cov(3)[1], DECIMAL_3)
def test_irf_stderr(self):
if not have_pandas():
raise nose.SkipTest
irf_stderr = self.irf.stderr(orth=False)
for i in range(1, 1 + len(self.lut.irf_stderr)):
assert_almost_equal(np.round(irf_stderr[i], 3), self.lut.irf_stderr[i - 1])
def test_cum_irf_stderr(self):
if not have_pandas():
raise nose.SkipTest
stderr = self.irf.cum_effect_stderr(orth=False)
for i in range(1, 1 + len(self.lut.cum_irf_stderr)):
assert_almost_equal(np.round(stderr[i], 3), self.lut.cum_irf_stderr[i - 1])
def test_lr_effect_stderr(self):
if not have_pandas():
raise nose.SkipTest
stderr = self.irf.lr_effect_stderr(orth=False)
orth_stderr = self.irf.lr_effect_stderr(orth=True)
assert_almost_equal(np.round(stderr, 3), self.lut.lr_stderr)
示例3: test_var_constant
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
def test_var_constant():
# see 2043
import datetime
from pandas import DataFrame, DatetimeIndex
series = np.array([[2., 2.], [1, 2.], [1, 2.], [1, 2.], [1., 2.]])
data = DataFrame(series)
d = datetime.datetime.now()
delta = datetime.timedelta(days=1)
index = []
for i in range(data.shape[0]):
index.append(d)
d += delta
data.index = DatetimeIndex(index)
model = VAR(data)
with pytest.raises(ValueError):
model.fit(1)
示例4: vars_test
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
def vars_test():
dt = get_dataframe()
name_list = ["date", "tBalance_all", "total_purchase", "total_redeem", "total_diff"]
# print(dt["total_purchase"])
time = dt["date"]
mdata = dt[["tBalance_all", "total_purchase", "total_redeem"]]
mdata.index = pandas.DatetimeIndex(time)
data = np.log(mdata).diff().dropna()
model = VAR(data)
results = model.fit(2)
results.summary()
results.plot()
示例5: test_var_constant
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
def test_var_constant():
# see 2043
import datetime
from pandas import DataFrame, DatetimeIndex
series = np.array([[2., 2.], [1, 2.], [1, 2.], [1, 2.], [1., 2.]])
data = DataFrame(series)
d = datetime.datetime.now()
delta = datetime.timedelta(days=1)
index = []
for i in range(data.shape[0]):
index.append(d)
d += delta
data.index = DatetimeIndex(index)
#with pytest.warns(ValueWarning): #does not silence warning in test output
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=ValueWarning)
model = VAR(data)
with pytest.raises(ValueError):
model.fit(1)
示例6: test2
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
def test2():
mdata = statsmodels.datasets.macrodata.load_pandas().data
dates = mdata[["year", "quarter"]].astype(int).astype(str)
quarterly = dates["year"] + "Q" + dates["quarter"]
mdata = mdata[["realgdp", "realcons", "realinv"]]
mdata.index = pandas.DatetimeIndex(quarterly)
data = np.log(mdata).diff().dropna()
model = VAR(data)
results = model.fit(2)
results.summary()
results = model.fit(maxlags=50, ic="aic")
# print(results.summary())
lag_order = results.k_ar
print results.forecast(data.values[-lag_order:], 30)
# print(results)
# print model.select_order(15)
# results.plot()
# results.plot_acorr()
pass
示例7: test_constructor
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
def test_constructor(self):
# make sure this works with no names
ndarr = self.data.view((float, 3))
model = VAR(ndarr)
res = model.fit(self.p)
示例8: dates_from_str
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
import pandas as pd
import numpy as np
import statsmodels.api as sm
import pylab
from statsmodels.tsa.base.datetools import dates_from_str
from statsmodels.tsa.vector_ar.var_model import VAR
mdata = sm.datasets.macrodata.load_pandas().data
dates = mdata[['year', 'quarter']].astype(int).astype(str)
quarterly = dates["year"] + "Q" + dates["quarter"]
quarterly = dates_from_str(quarterly)
mdata = mdata[['realgdp','realcons','realinv']]
mdata.index = pd.DatetimeIndex(quarterly)
data = np.log(mdata).diff().dropna() # log difference
# make a VAR model
model = VAR(data)
results = model.fit(2)
print results.summary()
results.plot()
results.plot_acorr() #autocorrelation
model.select_order(15)
results = model.fit(maxlags=15, ic='aic')
irf = results.irf(10)
irf.plot(orth=True) #Orthogonalization
pylab.show()
示例9: VAR
# 需要导入模块: from statsmodels.tsa.vector_ar.var_model import VAR [as 别名]
# 或者: from statsmodels.tsa.vector_ar.var_model.VAR import fit [as 别名]
#calc beta's alpha's
#do forecast of returns, correlation. Use to Weight
rets.iloc[:,0:10].plot()
###DETOUR TO VAR FORECASTING
from statsmodels.tsa.vector_ar.var_model import VAR, VARResults, VARProcess
import statsmodels
statsmodels.version.version
#Check for NA's in data - have to reduce number of series used as full 30
#gave singular matrix
v1 = VAR(rets_train[series_red], freq='D')
v1.select_order(maxlags=30)
results = v1.fit(5) #From fitted
# results.summary()
results.plot()
# results.plot_acorr()
# plt.show()
#Make forecast for 3months
test_index = rets_test.index
fc_range = pd.date_range(start=test_index[0], periods=2, freq='3M')
fc_periods = len(rets_test[fc_range[0]:fc_range[1]])
lag_order = results.k_ar
fc = results.forecast(rets_train[series_red].values,fc_periods)
fc.shape
fc[:,-1]
df_fc = pd.DataFrame(fc,index=rets.index[0:fc_periods],columns=rets_train[series_red])
df_fc.plot()