Skip to content

praveen-codes1/MAVAR

Repository files navigation

Dynamic Causal Inference in Finance using Meta-RL

1. Project Overview

This project implements a novel data science approach combining Causal Inference and Meta-Reinforcement Learning (Meta-RL) to analyze highly non-stationary time series data.

The core goal is to move beyond simple prediction and achieve dynamic causal inference: measuring how the causal influence between market factors changes over time due to shifting market regimes (non-stationarity).

Component Role Novelty
Causal Model Vector Autoregression (VAR(2)) Used to calculate the time-lagged causal scores (Granger Causality).
Learning Algorithm Model-Agnostic Meta-Learning (MAML) Meta-learns the optimal starting point ($\Theta^*$) for the VAR(2) model, enabling rapid adaptation to new causal structures in minutes, not hours.
Key Output Time-Varying Causal Coefficients Provides empirical evidence of how market rules evolve across different historical periods.

2. Dataset and Variables

We use real-world daily financial data for Apple Inc. (AAPL) spanning over four decades (1980–2022). The raw data is transformed into three key time series variables ($\mathbf{Y}_t$) to establish a robust causal system ($k=3$).

Variable Symbol Source/Calculation Interpretation
Returns $Y$ (R) Log Return of Adjusted Close Outcome: The percentage price change we seek to explain.
Volume $X_1$ (V) Log of Trading Volume Cause 1: Measures market activity/liquidity.
Volatility $X_2$ (Vol) Log(High/Low) Range Cause 2: Measures market risk/uncertainty.

3. Project Approach: VAR(2) and MAML

A. The Challenge: Non-Stationarity

Financial markets are non-stationary; the effect of volume on price during a pre-internet market (1980s) is fundamentally different from a high-frequency trading market (2020s). A traditional model would fail by averaging these effects.

B. The Solution: MAML Task Structure

The entire 40-year time series is segmented into sequential Tasks ($T_1, T_2, \dots$). Each task is treated as a separate, unique causal environment.

  1. Inner Loop (Adaptation): The VAR(2) model quickly adjusts its causal coefficients ($\phi$) using a small Support Set of data from the current task, reflecting the current market rule.
  2. Outer Loop (Meta-Learning): The MAML algorithm optimizes the model's initialization ($\Theta^*$) across all tasks, ensuring the model is always initialized to the most adaptable starting point for any future market shift.

4. Causal Coefficients and Inference

The core finding is contained in the Causal Coefficient Matrix ($\mathbf{\Phi}_1$), which is the output of the trained model.

A. Initial Causal Matrix ($\Theta^*$)

This matrix represents the Meta-Learned Master Strategy—the average week-to-week influence one variable has on another across the entire dataset.

Coefficient Value (Example) Interpretation (Granger Causality)
$\mathbf{\Phi}_{1}[\text{R, V}]$ +0.377 STRONG POSITIVE CAUSAL SCORE: On average, higher trading volume last week significantly contributes to higher returns this week.
$\mathbf{\Phi}_{1}[\text{R, R}]$ -0.228 NEGATIVE EFFECT: Past price increases lead to slight downward pressure (mean-reversion) this week.

B. Dynamic Inference (The Novelty)

The causal_track_df.csv file contains the dynamic coefficients. By plotting these, we show the causal score for the $\text{Volume} \to \text{Returns}$ link is not constant across all time, but fluctuates (e.g., from $+1.0$ to $-0.1$) proving that the market's fundamental rules are unstable and adaptive.


5. Running the Project

A. Setup

  1. Create Environment: Ensure you have Python and PyCharm/VS Code.
  2. Install Dependencies: Run the following command using the requirements.txt file:
    pip install -r requirements.txt

B. Execution

  1. Data Placement: Place your AAPL.csv file in the main project directory.
  2. Training: Run the training script to optimize the MAML model and generate results. A fixed seed is used for reproducibility.
    python maml_trainer.py
  3. Visualization: Launch the Streamlit dashboard to view the dynamic results.
    streamlit run app_dashboard.py

6. Final Results and Applications

The project successfully delivered a working MAML-VAR(2) framework that achieves stable meta-learning convergence on non-stationary financial data.

Final Deliverables

  • theta_star.pt: Saved optimized initial parameters.
  • causal_track_df.csv: Time series data of the evolving causal coefficients.
  • Streamlit Dashboard: Interactive visualization of the dynamic causal scores, proving the existence of non-stationary causal regimes.

Other Applications

The Meta-RL Causal Inference framework developed here is highly generalizable and can be applied to other complex, evolving systems:

  • Public Health: Tracking the time-varying effect of vaccination rates on disease transmission as the virus mutates.
  • Climate Science: Inferring the evolving causal relationship between greenhouse gas concentrations and extreme weather events.
  • Policy Economics: Measuring how the effectiveness of a fiscal stimulus policy changes as consumers and markets adapt over time.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages