Skip to content

Conversation

andreacate
Copy link
Contributor

@andreacate andreacate commented Mar 31, 2025

Dynamical Factor Models (DFM) Implementation

This PR provides a first draft implementation of Dynamical Factor Models as part of my application proposal for the PyMC GSoC 2025 project. A draft of my application report can be found at this link.

Overview

  • Added DFM.py with initial functionality

Current Status

This implementation is a work in progress and I welcome any feedback

Next Steps

  • Vectorize the construction of the transition and selection matrices (possibly by reordering state variables).
  • Add support for measurement error.

@zaxtax
Copy link
Contributor

zaxtax commented Apr 1, 2025

Looks interesting! Just say when you think it's ready for review

@fonnesbeck
Copy link
Member

cc @jessegrabowski

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@andreacate
Copy link
Contributor Author

Thanks for the feedback!

I'm still exploring the best approach for implementing Dynamic Factor Models.
I've added a simple custom DFM model in a Jupyter notebook, which I plan to use as a prototype and testing tool while developing the main BayesianDynamicFactor class.

@andreacate andreacate force-pushed the DFM_draft_implementation branch 2 times, most recently from 21560db to a459a1a Compare July 25, 2025 10:44
@jessegrabowski
Copy link
Member

Some tests are failing due to missing constants. You might have lost some changes in the reset/rebasing process

@andreacate andreacate force-pushed the DFM_draft_implementation branch from 1c04f65 to bc3fcf2 Compare July 25, 2025 13:51
Copy link
Member

@jessegrabowski jessegrabowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments. I didn't look over the tests because they still seem like WIP, but seem to be on the right track!

@andreacate andreacate force-pushed the DFM_draft_implementation branch 3 times, most recently from 7846f15 to e15cdd3 Compare July 29, 2025 07:59
@andreacate andreacate force-pushed the DFM_draft_implementation branch from e15cdd3 to 3b8bfe4 Compare August 8, 2025 12:36
@andreacate andreacate force-pushed the DFM_draft_implementation branch from 6496f38 to 615960b Compare August 15, 2025 21:20
Copy link
Member

@jessegrabowski jessegrabowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a deeper pass on everything except the build_symbolic_graph method. I need to spend more time on that because it's gotten quite complex.

I'll finish ASAP.

@jessegrabowski jessegrabowski force-pushed the DFM_draft_implementation branch from 8a03197 to ca1a86e Compare August 26, 2025 05:20
pt.zeros((self.k_endog, self.k_endog * (self.error_order - 1)), dtype=floatX)
)
if len(matrix_parts) == 1:
design_matrix = factor_loadings * 1.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do?

self.ssm["state_cov", :, :] = factor_cov

# Observation covariance matrix (H)
if self.error_order > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems backwards? The first case looks like it assumes error_order == 0.

This comment is based on error_sigma appearing in the else branch

sigma_obs = self.make_and_register_variable(
"sigma_obs", shape=(self.k_endog,), dtype=floatX
)
total_obs_var = error_sigma**2 + sigma_obs**2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure error_sigma should appear here? It's already appearing in Q, so is this double-counting?

andreacate and others added 5 commits August 26, 2025 15:50
@andreacate andreacate marked this pull request as ready for review August 27, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants