Hi all,
I want to implement a state space model.
However, it does not seem to work correctly as shown in the following code.
I would be thankful if you could help me with this question.
import os
import random
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import arviz as az
from copy import copy
from scipy import stats
import pymc as pm
import pytensor
import pytensor.tensor as pt
# Initialize random number generator
RANDOM_SEED = 8927
rng = np.random.default_rng(RANDOM_SEED)
az.style.use("arviz-darkgrid")
# create data
num_data = 142
t = np.arange(0, num_data)
y = np.sin(t) + np.random.normal(size=num_data) * 0.1
# ar2 model
with pm.Model() as ar2_model:
alpha_1 = pm.Normal("alpha_1", mu=0.0, sigma=1.0)
alpha_2 = pm.Normal("alpha_2", mu=0.0, sigma=1.0)
const = pm.Normal("const", mu=0.0, sigma=1.0)
sigma = pm.HalfNormal("sigma", sigma=1.0)
def transition(y_obs, y_prev, y_prev_prev, alpha_1, alpha_2, const, sigma):
m_t = const + alpha_1 * y_prev + alpha_2 * y_prev_prev
#y_t = m_t
y_t = pm.Normal("y_t", mu=m_t, sigma=sigma, observed=y_obs)
return y_t, y_prev
y_pt = pt.as_tensor_variable(y)
n_steps = num_data - 2
inits = [y_pt[1], y_pt[0]]
# Pytensor scan is a looping function
result, updates = pytensor.scan(
fn=transition, # function
outputs_info=inits, # initial conditions
sequences=y_pt[2:],
non_sequences=[alpha_1, alpha_2, const, sigma], # parameters
n_steps=n_steps, # number of loops
)
with ar2_model:
trace_scan = pm.sample(tune=500, draws=500)
error
MissingInputError: Input 1 () of the graph (indices start from 0), used to compute Mul(alpha_2, ), was not provided and not given a value. Use the PyTensor flag exception_verbosity='high', for more information on this error.
Backtrace when that variable is created:
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/ipykernel/zmqshell.py", line 549, in run_cell
return super().run_cell(*args, **kwargs)
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3048, in run_cell
result = self._run_cell(
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3103, in _run_cell
result = runner(coro)
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3308, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3490, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "/Users/yyamaguchi/Desktop/programming/05_PyMC/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3550, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/var/folders/57/xr8vd64j2_qd3ckpnwhjpw_h0000gn/T/ipykernel_4769/3370695671.py", line 23, in
result, updates = pytensor.scan(