Time-Varying Parameter PVAR (TVP-PVAR) with AR(1) and PyMC - "tuple" object error

Hi everyone,
I’m following up on two previous inquires on TVP-PVAR with Stochastic Volatility ,and h Help Needed: Kalman Filter with PyMC and pytensor.tensor.KalmanFilter Erros

I’m writing to seek assistance with an error I’m encountering while implementing a TVP-PVAR model with PyMC version 5 for my research (unpublished).

Model Description:

My model aims to capture the dynamic relationships between multiple time series variables using a TVP-PVAR approach. Additionally, I’m incorporating AR(1) terms for each variable to account for persistence and a more complex volatility model

Model Structure Description:

My model leverages a state-space framework, a common approach for TVP-PVAR models. This framework involves two key equations:

Observation Equation: This equation relates the observed data (denoted as y_t for time t) to a time-varying state vector (alpha_t) and an error term (varepsilon_t). It captures how the observed data is generated by the underlying dynamics in the model.

State Evolution Equation: This equation describes how the state vector (alpha_t) evolves over time. It often involves the previous state vector (alpha_(t-1)) and potentially a random component (eta_t) to account for the time-varying nature of the parameters.

Time-Varying Parameters and Stochastic Volatility:

A key feature of my model is that some parameters within the state vector (alpha_t) can vary over time. This allows the model to capture the dynamic relationships between economic variables.

Additionally, the model incorporates stochastic volatility, meaning the variance of the error term (varepsilon_t) is not constant but can fluctuate over time.

Code Snippet (Error section):

`Pythondef process_country(country, filtered_data, vars):
# … (rest of my function code, excluding sensitive details)

T_country, K = Y_country.shape  # Extracting dimensions from the tensor

try:
    with pm.Model() as model:
        # ... (relevant model definitions, excluding specifics)

        # Observation equation (potential source of error)
        Y_country_reshaped = Y_country.unsqueeze(0)
        beta_t_transposed = beta_t.permute(0, 2, 1)

        mu = alpha_t + torch.bmm(Y_country_reshaped, beta_t_transposed).squeeze(0)

        # Ensure observed is passed as a NumPy array
        y_t = pm.MvNormal('y_t', mu=mu, chol=chol, observed=Y_country.numpy())

        # ... (rest of the model code)

except Exception as e:
    print(f"Error processing {country}: {str(e)}")

Error Message:

  • Processing country: United Kingdom
    • PyTorch tensor shape for United Kingdom: torch.Size([, ]) # Replaced dimensions with placeholders
    • T_country: , K: # Replaced dimensions with placeholders
    • Error processing, United Kingdom: ‘tuple’ object has no attribute ‘ndim’
  • Processing country: United States (Similar error with “tuple” object)

Minimal Code Snippet (for Context):
import pymc as pm

class BayesianModel:

def __init__(self, data, vars):
    # data processing and initialization


def process_country(country, filtered_data, vars):
# ... (rest of your function code, excluding sensitive details)

T_country, K = Y_country.shape  # Extracting dimensions from the tensor

try:
    with pm.Model() as model:
        # ... (relevant model definitions, excluding specifics)

        # Observation equation (potential source of error)
        Y_country_reshaped = Y_country.unsqueeze(0)
        beta_t_transposed = beta_t.permute(0, 2, 1)

        mu = alpha_t + torch.bmm(Y_country_reshaped, beta_t_transposed).squeeze(0)

        # Ensure observed is passed as a NumPy array
        y_t = pm.MvNormal('y_t', mu=mu, chol=chol, observed=Y_country.numpy())

        # ... (rest of the model code)

except Exception as e:
    print(f"Error processing {country}: {str(e)}")

Rest of the code for processing countries and analysis)

Request:
I’m using PyTensor throughout my code, and I suspect this error might be related to the usage of tensors where an integer value is expected.

I’d appreciate any insights you can offer on the “tuple” object error and how to potentially resolve it. Additionally, any guidance on best practices for implementing TVP-PVAR models with AR(1) and PyMC , particularly regarding efficient handling of potentially large datasets, would be valuable.

Thank you for your time and assistance.

Sincerely,

Dimitri