Skip to content

Commit

Permalink
unbreak website attempt 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jvivian committed Mar 9, 2024
1 parent 82fe31a commit 7c718ef
Show file tree
Hide file tree
Showing 49 changed files with 4,425 additions and 37 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
_freeze/
_docs/
_proc/

Expand Down
11 changes: 11 additions & 0 deletions _freeze/posts/dr-dfm/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "18d0ebdf42d8d4552b0bc778da758409",
"result": {
"markdown": "---\ntitle: \"Covid-19 Data-Rich Dynamic Factor Model\"\nsubtitle: \"Overview, Python Package, and Interactive Analysis\"\nauthor: \"John Vivian, Aaron Cooke, Josh Fitzgerald\"\ndate: \"2024-03-09\"\ncategories: ['Math', 'Modeling', 'Python']\nimage: \"jv_dfm.jpg\"\nformat:\n html: \n output-file: \"test.html\"\n revealjs:\n include-in-header:\n text: |\n <style>\n .center-xy {\n margin: 0;\n position: absolute;\n top: 50%;\n left: 50%;\n -ms-transform: translateY(-50%), translateX(-50%);\n transform: translateY(-50%), translateX(-50%);\n }\n </style>\neditor:\n render-on-save: true\ncode:\n echo: false\nformat-links: [revealjs]\n---\n\n## Understanding the Economic Impact of COVID-19 Through Data\n\n<br>\n\nAs the world grapples with the ongoing effects of the COVID-19 pandemic, it is necessary to leverage advanced analytical tools to understand its economic impacts. Our project leverages Dynamic Factor Models (DFMs) to uncover hidden patterns and relationships in large amounts of economic data generated during this period. This presentation will introduce you to the core concepts of DFMs, the specific challenges posed by COVID-19 data, and the Python package we designed to address these challenges.\n\n\n# Dynamic Factor Models\n\n## Dynamic Factor Models: A Primer\n\n<br>\n\nDynamic Factor Models are powerful statistical tools that help us make sense of complex, interconnected data. By identifying latent factors that influence observed variables over time, DFMs can reveal the underlying trends and dynamics of economic systems. This approach is particularly valuable in the context of COVID-19, where traditional models may struggle to account for rapidly changing conditions.\n\n## The Model at a Glance\n\nConsider the basic Dynamic Factor Model equation:\n\n$$y_t = \\Lambda f_t + \\epsilon_t$$\n\n<br>\n\nHere's what each symbol represents:\n\n- $y_t$: The observed variables at time $t$.\n- $\\Lambda$: The loading matrix, showing how each latent factor influences observed variables.\n- $f_t$: The latent factors, representing underlying trends.\n- $\\epsilon_t$: The error term, accounting for discrepancies between model predictions and observed data.\n\n\n## Visualizing the Model\n\n\n:::{.columns}\n::::{.column width=\"70%\"}\n<br>\n\nThe latent factors ($f_t$) influence the observed variables ($y_t$) through the loading matrix ($\\Lambda$), and the error term ($\\epsilon_t$) is associated with the observed variables.\n\n<br>\n\nThe loading matrix is a bridge that connects the latent factors, which are unobservable, to the observed variables, providing a mathematical representation of how the latent factors influence the observed data\n::::\n::::{.column width=\"30%\"}\n![](./model-viz.png)\n::::\n:::\n\n## Latent Factors and Observed Variables {.smaller}\n> Relationship between latent factors and observed variables via loading matrix\n\n::: {.cell execution_count=1}\n``` {.python .cell-code}\n# echo: false\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Set seed for reproducibility\nnp.random.seed(42)\n\n# Generate dummy data\nnum_observed_variables = 4\nnum_time_points = 100\nloading_matrix = np.array([[0.5, 0.3, 0.8, 0.2],\n [0.7, 0.2, 0.5, 0.1]])\n\nlatent_factors = np.random.randn(num_time_points, 2)\nobserved_variables = np.dot(latent_factors, loading_matrix) + np.random.randn(num_time_points, num_observed_variables)\n\n# Plotting\nplt.figure(figsize=(10, 6))\n\n# Plot latent factors\nplt.subplot(2, 1, 1)\nplt.plot(latent_factors[:, 0], label='Latent Factor 1', linestyle='--')\nplt.plot(latent_factors[:, 1], label='Latent Factor 2', linestyle='--')\nplt.title('Latent Factors Over Time')\nplt.legend()\n\n# Plot observed variables\nplt.subplot(2, 1, 2)\nfor i in range(num_observed_variables):\n plt.plot(observed_variables[:, i], label=f'Observed Variable {i+1}')\nplt.title('Observed Variables Over Time')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-html/cell-2-output-1.png){width=950 height=565}\n:::\n:::\n\n\n## Extending the Model with Time Dynamics\n\nBy incorporating autoregressive components, we can capture the evolving nature of economic relationships during the pandemic. This advanced model formulation allows for a more accurate representation of the data, enabling better forecasts and insights.\n\n<br>\n\n$$\n\\begin{split}\\begin{align}\ny_t & = \\Lambda f_t + B x_t + u_t \\\\\nf_t & = A_1 f_{t-1} + \\dots + A_p f_{t-p} + \\eta_t \\qquad \\eta_t \\sim N(0, I)\\\\\nu_t & = C_1 u_{t-1} + \\dots + C_q u_{t-q} + \\varepsilon_t \\qquad \\varepsilon_t \\sim N(0, \\Sigma)\n\\end{align}\\end{split}\n$$\n\n## Extending the Model with Time Dynamics {.smaller}\n\n$$\n\\begin{split}\\begin{align}\ny_t & = \\Lambda f_t + B x_t + u_t \\\\\nf_t & = A_1 f_{t-1} + \\dots + A_p f_{t-p} + \\eta_t \\qquad \\eta_t \\sim N(0, I)\\\\\nu_t & = C_1 u_{t-1} + \\dots + C_q u_{t-q} + \\varepsilon_t \\qquad \\varepsilon_t \\sim N(0, \\Sigma)\n\\end{align}\\end{split}\n$$\n\n<br>\n\nWhere $y_t$ is observed, $f_t$ are unobserved latent factors, $x_t$ are optional (unused for our case) exogenous variables, and the dynamic evolution of latent factors is expressed using the transition matrix $A$ with $\\eta_t$ representing new information or random shocks. $u_t$ is the error or \"idiosyncratic\" process\n\n. . .\n\n<br>\n\nThis model is then cast into state space form and the unobserved factors estimated via the Kalman filter. The likelihood can be evaluated as a byproduct of the filtering recursions with maximum likelihood estimation used to estimate the parameters.\n\n\n## Extending the Model with Time Dynamics {.smaller}\n$$f_t = A f_{t-1} + \\eta_t$$\n\n$A$: Transition matrix<br>\n$\\eta_t$: Innovation term\n\n<br>\n\n:::{.incremental}\n- The transition matrix, often denoted as $A$, is a square matrix that governs the temporal evolution of the latent factors\n- Each element of the matrix represents the influence of one latent factor at the current time on the corresponding latent factor at the next time point\n- The elements of the transition matrix $A$ determine how each latent factor at the previous time point contributes to the latent factors at the current time point\n- Values in the diagonal of $A$ represent the persistence of each latent factor over time\n- Off-diagonal elements indicate the influence of one latent factor on another\n:::\n\n## Interpreting Transition Matrices {.smaller}\n\nExamining the first transition matrix\n\n::: {.cell execution_count=2}\n``` {.python .cell-code}\n# echo: false\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Set seed for reproducibility\nnp.random.seed(42)\n\n# Generate two different transition matrices\ntransition_matrix_1 = np.array([[0.8, 0.2],\n [0.3, 0.7]])\n\ntransition_matrix_2 = np.array([[0.5, 0.5],\n [0.6, 0.4]])\n\n# Create a figure with subplots\nfig, axs = plt.subplots(1, 2, figsize=(10, 4))\n\n# Plot heatmap for Transition Matrix 1\nsns.heatmap(transition_matrix_1, annot=True, cmap=\"Reds\", linewidths=.5, ax=axs[0])\naxs[0].set_title('Transition Matrix 1')\n\n# Plot heatmap for Transition Matrix 2\nsns.heatmap(transition_matrix_2, annot=True, cmap=\"Reds\", linewidths=.5, ax=axs[1])\naxs[1].set_title('Transition Matrix 2')\n\n# Adjust layout\nplt.tight_layout()\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-html/cell-3-output-1.png){width=936 height=373}\n:::\n:::\n\n\n- The diagonal elements (0.8 and 0.7) are relatively high, indicating a strong persistence of each latent factor over time.\n- The off-diagonal elements (0.2 and 0.3) suggest moderate influence of one latent factor on the other, allowing for some interaction between the two factors.\n- Summary: latent factors have a tendency to persist, with some interdependence.\n\n## Interpreting Transition Matrices {.smaller}\n\nExamining the second transition matrix\n\n::: {.cell execution_count=3}\n``` {.python .cell-code}\n# echo: false\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Set seed for reproducibility\nnp.random.seed(42)\n\n# Generate two different transition matrices\ntransition_matrix_1 = np.array([[0.8, 0.2],\n [0.3, 0.7]])\n\ntransition_matrix_2 = np.array([[0.5, 0.5],\n [0.6, 0.4]])\n\n# Create a figure with subplots\nfig, axs = plt.subplots(1, 2, figsize=(10, 4))\n\n# Plot heatmap for Transition Matrix 1\nsns.heatmap(transition_matrix_1, annot=True, cmap=\"Reds\", linewidths=.5, ax=axs[0])\naxs[0].set_title('Transition Matrix 1')\n\n# Plot heatmap for Transition Matrix 2\nsns.heatmap(transition_matrix_2, annot=True, cmap=\"Reds\", linewidths=.5, ax=axs[1])\naxs[1].set_title('Transition Matrix 2')\n\n# Adjust layout\nplt.tight_layout()\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-html/cell-4-output-1.png){width=936 height=373}\n:::\n:::\n\n\n- The diagonal elements (0.5 and 0.4) are lower compared to Transition Matrix 1, suggesting less persistence of each latent factor over time.\n- The off-diagonal elements (0.5 and 0.6) indicate a relatively stronger influence of one latent factor on the other compared to Transition Matrix 1.\n- Summary: latent factors are less likely to persist and may be influenced more by each other, allowing for a more dynamic and responsive behavior.\n\n## Factor Constraints: Enhancing Model Interpretability\n<br>\n\nBy applying constraints to the model parameters, we can improve interpretability and reduce complexity while incorporating prior domain knowledge about variable relationships.\n\n<br><br>\n\nFor example, setting certain elements of the loading matrix to zero might suggest that specific observed variables are not influenced by particular latent factors.\n\n## Factor Constraints {.smaller}\n> Factor loading constraint example\n\n| Dep. variable | Global.1 | Pandemic | Employment | Consumption | Inflation |\n|-----------------|----------|----------|------------|-------------|-----------|\n| Supply_1 | X | | | | |\n| Supply_7 | X | | | | |\n| Monetary_5 | X | | | | |\n| Monetary_9 | X | | | | |\n| Supply_2 | X | | X | | |\n| Supply_3 | X | | X | | |\n| Demand_7 | X | | X | | |\n| Demand_3 | X | | | X | |\n| Demand_5 | X | | | X | |\n| Monetary_2 | X | | | | X |\n| Monetary_1 | X | | | | X |\n| Pandemic_2 | X | X | | | |\n| Pandemic_9 | X | X | | | |\n\n\n# Our Python Package: Modeling and Analysis\n\n## Implementation {.smaller}\n<br>\n\nWe are developing a Python package that simplifies the process of applying DFMs to COVID-19 economic data. The package includes features such as:\n\n<br>\n\n- Poetry for dependency management\n- CI with GitHub Actions\n- Pre-commit hooks with pre-commit\n- Code quality with black & ruff\n- Testing and coverage with pytest and codecov\n- Documentation with MkDocs\n- Compatibility testing for multiple versions of Python with Tox\n- Containerization with Docker\n\n## Dashboard {.smaller}\n<br>\nOur package contains a simplified interface for running parameterized DFM models\n\n:::{.column-page}\n![](runner.png)\n:::\n\n## Dashboard - Data Explorer {.smaller .scrollable}\n<br>\nOur package includes a comprehensive dashboard with features for data exploration, factor analysis, and comparative model testing. Here's a sneak peek at what you can do:\n\n\n:::{.column-page}\n![](data_explorer.png)\n:::\n\n## Dashboard - Factor Analysis {.smaller}\n<br>\nDive deep into the relationships between latent factors and observed variables. Understand how economic trends evolve over time.\n\n:::{.column-page}\n![](factor_analysis.png)\n:::\n\n## Dashboard - Comparative Analysis {.smaller .scrollable}\nTest and compare different model configurations to identify the most accurate representations of the data.\n\n:::{.column-page}\n![](comparative_analysis.png)\n:::\n\n\n# Future Work\n\n<br>\n\nOur next steps involve incorporating the insights gained from DFMs into [Synthetic Control Model](https://github.com/OscarEngelbrektson/SyntheticControlMethods) to further refine our understanding of COVID-19's economic impact by exploring counter-factual statements. We are garnering feedback on our work and are hoping to submit for publication within the year!\n\n",
"supporting": [
"index_files/figure-html"
],
"filters": [],
"includes": {}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions _freeze/site_libs/revealjs/plugin/highlight/highlight.esm.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions _freeze/site_libs/revealjs/plugin/highlight/highlight.js

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions _freeze/site_libs/revealjs/plugin/highlight/monokai.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Monokai style - ported by Luigi Maselli - http://grigio.org
*/

.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
background: #272822;
color: #ddd;
}

.hljs-tag,
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-strong,
.hljs-name {
color: #f92672;
}

.hljs-code {
color: #66d9ef;
}

.hljs-class .hljs-title {
color: white;
}

.hljs-attribute,
.hljs-symbol,
.hljs-regexp,
.hljs-link {
color: #bf79db;
}

.hljs-string,
.hljs-bullet,
.hljs-subst,
.hljs-title,
.hljs-section,
.hljs-emphasis,
.hljs-type,
.hljs-built_in,
.hljs-builtin-name,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-addition,
.hljs-variable,
.hljs-template-tag,
.hljs-template-variable {
color: #a6e22e;
}

.hljs-comment,
.hljs-quote,
.hljs-deletion,
.hljs-meta {
color: #75715e;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-doctag,
.hljs-title,
.hljs-section,
.hljs-type,
.hljs-selector-id {
font-weight: bold;
}
Loading

0 comments on commit 7c718ef

Please sign in to comment.