Shape Mismatch Issue in TVP-PVAR Model Implementation

10 views
Skip to first unread message

D. K

unread,
May 20, 2024, 6:01:08 AMMay 20
to pystatsmodels
 

Hello,

 I am reaching out to seek your expertise regarding a persistent issue I have encountered while implementing a Time-Varying Parameter Panel Vector Autoregression (TVP-PVAR) model. I have managed to write a such code and model. Despite several attempts to resolve the problem, I continue to face a shape mismatch error.

**Problem Description:**

The error message I receive is as follows:
```
Error at country 0, time 1: Dimension mismatch: array 'cov' is of shape (4, 4), but 'mean' is a vector of length 304.
Likelihood could not be calculated due to an error.

```
This occurs when using the `multivariate_normal.pdf` function from the `scipy.stats` module. The function expects the `mean` parameter to be a vector with a length equal to the number of rows (or columns) of the covariance matrix `R`.

**Relevant Code Section:**

Here is the part of the code where the issue arises:

```python
def likelihood(data, alphas, Phi, Q, R):
    n_countries, n_periods, n_vars = data.shape
    p = Phi.shape[2]
    total_obs = n_countries * n_periods * n_vars

    likelihood = 1
    for i in range(n_countries):
        for t in range(p, n_periods):
            y_it = data[i, t].ravel()
            y_hat = alphas[i] + np.sum([np.dot(data[i, t-j], Phi[i, j-1]) for j in range(1, p+1)], axis=0)
            y_hat_flat = y_hat.ravel()  # Flatten y_hat for compatibility

            try:
                likelihood *= multivariate_normal.pdf(y_it, mean=y_hat_flat, cov=R)
            except ValueError as e:
                print(f"Error at country {i}, time {t}: {e}")
                return None

    return likelihood
```

Despite ensuring that `y_it` and `y_hat` are flattened to 1D vectors, the mismatch persists. The current definition of `R` is as follows:
```python
R = np.eye(n_vars)  # Observation equation covariance matrix
```
Here, `R` is defined as the covariance matrix for the observation equation. Given that `n_vars` is 4, `R` is a 4x4 identity matrix.

**Steps Taken to Resolve the Issue:**

1. **Flattened `y_it` and `y_hat`:** Reshaped both `y_it` and `y_hat` to 1D vectors using `.ravel()`.
2. **Defined Compatible Shape for `R`:** Attempted to ensure `R` had a compatible shape with the length of `y_hat.ravel()`. Originally set `R` to `np.eye(n_vars)`, but considering the length of `y_hat.ravel()` is 304, a larger identity matrix was also tested.
3. **Print Statements for Debugging:** Added print statements to verify shapes of `y_it`, `y_hat`, and `R`.

Despite these efforts, the shape mismatch error persists. I suspect there might be a fundamental issue with the way `R` is being defined or used in the context of the likelihood calculation. I need to have the likelihood calculation explicitly calculated in the code.

**Request for Assistance:**

I have several blocks of variables for the model. For this example, I use 4 variables, reaching at a model of 28 variables. All variables are endogenous.

Could you please provide guidance on how to correctly define and use the covariance matrix `R` in this context? Any insights or suggestions on resolving the shape mismatch would be greatly appreciated.

Thank you for your time and assistance.

Best regards,

David K.
Reply all
Reply to author
Forward
0 new messages