0

Documentation:

https://nfoursid.readthedocs.io/en/latest/

#housekeeping
#_________________________________________________________________________

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from nfoursid.kalman import Kalman
from nfoursid.nfoursid import NFourSID
from nfoursid.state_space import StateSpace
import time
import datetime
import math
import scipy as sp
from pandas_datareader import data as pdr
from IPython.display import display, Latex
from statsmodels.graphics.tsaplots import plot_acf
import yfinance as yfin




#this Time Series should be used as input
#_________________________________________________________________________

import yfinance as yfin
yfin.pdr_override()
spy = pdr.get_data_yahoo('AAPL',start='2022-08-23',end='2022-10-24')
spy['Log Return'] = np.log(spy['Adj Close']/spy['Adj Close'].shift(1))
AAPL=pd.DataFrame((spy['Log Return']))




#this is from the documentation and actually works
#_________________________________________________________________________

pd.set_option('display.max_columns', None)

# reproducable results
np.random.seed(0)  

# create a training-set by simulating a state-space model with this many datapoints
NUM_TRAINING_DATAPOINTS = 1000  

# same for the test-set
NUM_TEST_DATAPOINTS = 20  

INPUT_DIM = 3
OUTPUT_DIM = 2

# actual order of the state-space model in the training- and test-set
INTERNAL_STATE_DIM = 4  

NOISE_AMPLITUDE = .1  # add noise to the training- and test-set
FIGSIZE = 8

# define system matrices for the state-space model of the training- 
# and test-set
A = np.array([
    [1,  .01,    0,   0],
    [0,    1,  .01,   0],
    [0,    0,    1, .02],
    [0, -.01,    0,   1],
]) / 1.01
B = np.array([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
    [0, 1, 1],
]
) / 3
C = np.array([
    [1, 0, 1,  1],
    [0, 0, 1, -1],
])
D = np.array([
    [1, 0, 1],
    [0, 1, 0]
]) / 10

state_space = StateSpace(A, B, C, D)
for _ in range(NUM_TRAINING_DATAPOINTS):
    input_state = np.random.standard_normal((INPUT_DIM, 1))
    noise = np.random.standard_normal((OUTPUT_DIM, 1)) * NOISE_AMPLITUDE

    state_space.step(input_state, noise)

nfoursid = NFourSID(
    
    # the state-space model can summarize inputs and outputs as a dataframe
    state_space.to_dataframe(),
    
    output_columns=state_space.y_column_names,
    input_columns=state_space.u_column_names,
    num_block_rows=10
)
nfoursid.subspace_identification()




#further methods
#_________________________________________________________________________

fig, ax = plt.subplots(figsize=figsize)
nfoursid.plot_eigenvalues(ax)
fig.tight_layout()

#interpret order from plot (sprungstelle), still run order->inf
ORDER_OF_MODEL_TO_FIT = 4
state_space_identified, covariance_matrix = nfoursid.system_identification(
    rank=ORDER_OF_MODEL_TO_FIT
)

#Ausgabe der Modellvorhersagen
nfoursid.to_dataframe()

#Vorhersage gegen Beobachtung
figsize = (1.3 * FIGSIZE, FIGSIZE)
fig = plt.figure(figsize=figsize)

# the state-space model can plot its inputs and outputs
state_space.plot_input_output(fig)  

fig.tight_layout()

Pasting AAPL in method nfoursid:

TypeError: NFourSID.init() missing 1 required positional argument: 'dataframe'

Pasting AAPL in method state_space:

ValueError: Dimensions of u (43, 1) are inconsistent. Expected (3, 1). and TypeError: 'DataFrame' object is not callable

0 Answers0