portfolio-engine / tests /test_uc.py
engineportf's picture
Initial Deployment from Local Engine
208fbf8 verified
Raw
History Blame Contribute Delete
925 Bytes
import pytest
import numpy as np
import pandas as pd
from statsmodels.tsa.statespace.structural import UnobservedComponents
def test_unobserved_components_fit():
# Simulate monthly returns (short history: 24 observations)
np.random.seed(42)
dates = pd.date_range('2020-01-01', periods=24, freq='ME')
returns = np.random.normal(0.01, 0.04, size=24) + np.sin(np.linspace(0, 4*np.pi, 24))*0.02
series = pd.Series(returns, index=dates)
# Using 'local linear trend' and 'seasonal' with period=12
model = UnobservedComponents(series, level='local linear trend', seasonal=12)
res = model.fit(disp=False)
assert res is not None
# Check forecast
fc = res.get_forecast(steps=1)
forecast = fc.predicted_mean.iloc[0]
se = fc.se_mean.iloc[0]
# Assert values are reasonable and not NaN
assert not np.isnan(forecast)
assert not np.isnan(se)
assert se > 0