Showing posts with label financial engineering. Show all posts
Showing posts with label financial engineering. Show all posts

Thursday

Causal Modeling

 

                                                             generated by meta ai

Yes. In fact, causal modelling is one of the most advanced topics in quantitative finance and is becoming increasingly important because traditional ML models (LSTM, XGBoost, Transformers) often learn correlations, whereas causal models aim to discover why prices move.

Since I am pursuing an MSc in Financial Engineering at WQU, I'll teach it at that level, covering theory, mathematics, and Python implementations.

Learning Roadmap

We'll build from scratch.

  1. Correlation vs Causation

  2. Structural Causal Models (SCM)

  3. Directed Acyclic Graphs (DAGs)

  4. Causal Discovery Algorithms

  5. Do-Calculus (Pearl)

  6. Counterfactual Prediction

  7. Causal Forecasting

  8. Applying causal models to stock prediction

  9. Building an end-to-end project


Step 1 — Correlation vs Causation

Suppose we have

Oil PriceAirline Stock

They are negatively correlated.

But does oil directly affect airlines?

Yes.

Higher fuel cost
→ higher operating expenses
→ lower profit
→ lower stock price

This is a causal relationship.

Now another example.

Ice Cream SalesShark Attacks

Highly correlated.

Does ice cream cause sharks?

No.

Common cause:

Summer
→ more people swim
→ more shark attacks

Summer
→ more ice cream sales

This is a confounder.

Traditional ML usually cannot distinguish these.


Step 2 — Financial Example

Imagine predicting Apple stock.

Variables

Interest Rate

Inflation

USD Index

Nasdaq Index

Apple Earnings

Apple Stock

The causal graph looks like

Interest Rate
        \
         \
          \
Inflation ---> USD
     \
      \
       \
     Nasdaq ----\
                  \
                   \
              Apple Earnings
                     \
                      \
                   Apple Stock

Notice

Interest rate does NOT directly cause Apple stock.

It changes

Interest Rate

Bond Yield

Investor Preference

Nasdaq

Apple

That matters.


Step 3 — Structural Causal Model (SCM)

Every node has an equation.

Example

Inflation = Noise

InterestRate = 0.8 × Inflation + Noise

USD = 0.5 × InterestRate + Noise

Nasdaq = -0.7 × InterestRate + Noise

Apple =

0.6 × Nasdaq

+0.4 × Earnings

+Noise

Unlike regression,

each equation represents a mechanism.


Python Example

Generate synthetic causal data.

import numpy as np
import pandas as pd

np.random.seed(42)

N = 1000

inflation = np.random.normal(2,0.5,N)

interest = 0.8*inflation + np.random.normal(0,0.2,N)

usd = 0.5*interest + np.random.normal(0,0.3,N)

nasdaq = -0.7*interest + np.random.normal(0,0.5,N)

earnings = np.random.normal(5,1,N)

apple = (
    0.6*nasdaq
    +0.4*earnings
    +np.random.normal(0,0.5,N)
)

df = pd.DataFrame({
    "Inflation":inflation,
    "Interest":interest,
    "USD":usd,
    "Nasdaq":nasdaq,
    "Earnings":earnings,
    "Apple":apple
})

print(df.head())

Notice that we explicitly generated the causal relationships.


Step 4 — Draw the DAG

Using NetworkX

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()

G.add_edges_from([
    ("Inflation","Interest"),
    ("Interest","USD"),
    ("Interest","Nasdaq"),
    ("Nasdaq","Apple"),
    ("Earnings","Apple")
])

nx.draw(G,
        with_labels=True,
        node_size=3000,
        arrows=True)

plt.show()

Produces

Inflation

↓

Interest

↙      ↘

USD   Nasdaq

          ↓

      Apple
↑

Earnings

Step 5 — Why This Is Better Than Regression

Suppose Fed raises rates.

Regression says

Past data:

Rate ↑

Apple ↓

Therefore predict Apple ↓

But suppose

Apple reports record earnings.

Causal model says

Interest ↑

↓

Nasdaq ↓

↓

Apple ↓

BUT

Earnings ↑↑

↓

Apple ↑

The model understands competing causes.

This is much closer to how human analysts reason.


Step 6 — Counterfactual Prediction

Question:

"What would Apple have done if the Fed had NOT increased rates?"

Regression cannot answer.

Causal model can.

interest = 5.5

# Intervention

interest = 2.0

# Recompute downstream variables

usd = 0.5*interest

nasdaq = -0.7*interest

apple = 0.6*nasdaq + 0.4*earnings

This is called an intervention:

do(Interest=2%)

instead of merely observing Interest = 2%.


Step 7 — Real Stock Prediction

For NIFTY prediction, a richer causal graph might be:

Fed Rate
     ↓
US Dollar
     ↓
Crude Oil
     ↓
Indian Inflation
     ↓
RBI Rate
     ↓
Bank Nifty
     ↓
NIFTY

GDP

↓

Corporate Earnings

↓

NIFTY

VIX

↓

NIFTY

FII Flow

↓

NIFTY

A causal model uses these relationships rather than feeding all variables into a black-box predictor.


Step 8 — Libraries

Popular Python libraries include:

  • DoWhy — causal inference and effect estimation

  • CausalNex — Bayesian networks and causal graphs

  • pgmpy — Bayesian networks (already familiar from your coursework)

  • econml — heterogeneous treatment effects

  • lingam — causal discovery for linear non-Gaussian models

  • causal-learn — PC, GES, FCI, NOTEARS and other discovery algorithms

  • networkx — graph visualization

Example with DoWhy:

from dowhy import CausalModel

model = CausalModel(
    data=df,
    treatment="Interest",
    outcome="Apple",
    common_causes=["Inflation"]
)

identified_estimand = model.identify_effect()

estimate = model.estimate_effect(
    identified_estimand,
    method_name="backdoor.linear_regression"
)

print(estimate.value)

Final Project (MSc-Level)

A strong capstone could combine:

  1. Data collection

    • Yahoo Finance

    • FRED (interest rates)

    • RBI

    • FII/DII flows

    • VIX

    • Crude oil

    • USD/INR

    • CPI

  2. Causal discovery

    • PC Algorithm

    • NOTEARS

    • LiNGAM

  3. Bayesian Network

    • Build the DAG

    • Learn conditional probability tables or continuous conditional distributions

  4. Causal forecasting

    • Estimate intervention effects

    • Perform counterfactual analysis

    • Forecast index or stock prices under different macroeconomic scenarios

  5. Explainability

    • Show which causal paths contributed most to the prediction

    • Compare against LSTM, XGBoost, and Transformer models

This kind of project aligns well with your financial engineering background because it integrates probabilistic graphical models, macroeconomic reasoning, and interpretable AI rather than relying solely on black-box prediction.

Causal Modeling

                                                               generated by meta ai Yes. In fact, causal  modelling  is one of the most adva...