Skip to content

nayanekaa/causal-productivity-intelligence

Repository files navigation

🧠 Causal Productivity Intelligence System

Statistical Modelling · Bayesian Inference · Causal Inference · LLM Integration

Python Streamlit License: MIT CI Code Style: Black Research-Grounded


Most productivity tools tell you what you did. This system tells you why your output varies — using causal inference, not correlation.



Overview

This is a research-grade, end-to-end data science project that identifies the causal drivers of knowledge-worker productivity through a rigorous statistical pipeline. It goes far beyond dashboards and regression — it builds a full causal model with:

  • A Directed Acyclic Graph (DAG) specifying the causal structure, including stress, caffeine, and motivation as first-class nodes
  • Double/Debiased ML (Chernozhukov et al., 2018) to estimate causal effects without parametric model bias
  • Mediation analysis decomposing total effects into direct and indirect pathways
  • Bayesian hierarchical modelling with informative, literature-calibrated priors for full uncertainty quantification
  • Counterfactual simulation implementing Pearl's do-operator across the full structural causal model
  • A research-primed LLM layer that narrates every finding using only verified peer-reviewed concepts

Every component is anchored in published behavioral science, econometrics, and Bayesian statistics. No fabricated citations. No generic advice.


Quick Start

# Clone and install
git clone https://github.com/your-username/causal-productivity-intelligence.git
cd causal-productivity-intelligence

python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

# Run the full pipeline (approx. 27 seconds without Bayesian MCMC)
python run_all.py --skip-bayes

# Launch the interactive dashboard
cp .env.example .env          # add your ANTHROPIC_API_KEY
streamlit run app.py

Research Foundation

Every modelling decision is grounded in peer-reviewed research.

Finding Source Implementation
Sleep deprivation at 6h/night equals 2 nights total loss Van Dongen et al. (2003, SLEEP) Non-linear sleep_performance_curve()
Sleep and circadian phase interact multiplicatively Dijk & Czeisler (1995, J Neuroscience) sleep × start_hour interaction term
Deep/deliberate work requires elite cognitive resources Ericsson et al. (1993, Psych Review) task_type × sleep interaction
Post-lunch dip is a genuine circadian phenomenon Monk (2005, Occup Env Medicine) circadian_alertness() trough at 14:00
Caffeine 6h before bed reduces sleep by ~1 hour Drake et al. (2013, J Clin Sleep Med) caffeine_sleep_disruption()
Motivation mediates sleep → work performance Deci & Ryan (2000); Baard et al. (2004) Mediation analysis; motivation as DAG node
Stress elevates cortisol, impairing memory consolidation Cohen et al. (1983); Salleh (2008) Stress as hub confounder/mediator
Breaks aligned to 90-min ultradian rhythm optimise output Kleitman BRAC; Lorist et al. (2005) fatigue_curve() with break moderation
Backdoor criterion identifies adjustment sets Pearl (2009, Causality) DAG specification + adjustment sets
Neyman-orthogonal estimation avoids regularisation bias Chernozhukov et al. (2018, Econometrica) Double ML with GradientBoosting nuisance
Sensitivity to unmeasured confounders is quantifiable Rosenbaum (2002) Gamma-bounds sensitivity analysis
Posterior predictive checks validate model adequacy Gelman et al. (2013, BDA3) PPC with 100 posterior draws

Architecture

causal-productivity-intelligence/
│
├── src/
│   ├── data_generation.py     Research-grounded synthetic DGP (v2 enriched DAG)
│   ├── regression_model.py    OLS with interaction effects, HC3 SEs, AMEs
│   ├── causal_analysis.py     DAG · IPW · Double ML · Mediation · Sensitivity
│   ├── bayesian_model.py      PyMC / MH-MCMC · Posteriors · PPC
│   ├── simulation.py          Counterfactual engine (Pearl do-operator)
│   └── llm_insights.py        Research-primed LLM narration (Anthropic API)
│
├── notebooks/
│   └── exploration.ipynb      32-cell EDA · 9 sections · 14 publication-quality plots
│
├── data/
│   ├── productivity_data.csv  Generated dataset (1,200 observations, 19 variables)
│   └── outputs/               24 model output plots + posterior summaries
│
├── .github/
│   └── workflows/ci.yml       GitHub Actions: lint · test · notebook execution
│
├── app.py                     Streamlit dashboard (4 tabs, fully interactive)
├── run_all.py                 Master pipeline runner with --skip-bayes flag
├── pyproject.toml             Package metadata, Black/isort/pytest config
├── requirements.txt
├── .env.example
├── CONTRIBUTING.md
├── CHANGELOG.md
└── LICENSE

Causal DAG — v2 Enriched

[chronotype] ─────────────────────────────────────────────────────┐
      │                                                            │
day_of_week ──► stress_level ──► caffeine_intake ────────────────►│
                     │                  │                          ▼
screen_time ─────────┘          caffeine_hour ──► sleep_hours ──► productivity_score
                     │                                  │          ▲
                     └────────────────────────► motivation_score ──┤
                                                        │          │
                                              task_type ───────────┘

Identification challenges introduced by the enriched DAG:

  1. motivation_score and task_type are mediators — conditioning on them when estimating the sleep effect blocks the indirect path and underestimates the total effect
  2. [chronotype] is an unmeasured confounder — we quantify sensitivity to it via Rosenbaum Gamma-bounds
  3. caffeine_intake sits in a cross-day feedback loop — treated as a within-day observed variable with adjustment

Modelling Results

Regression — M3 Full Model

Predictor β 95% CI p
Sleep hours (centered at 7h) +1.47 [+1.10, +1.80] < 0.001
Deep work (vs shallow) +7.16 [+6.46, +7.85] < 0.001
Deep × Sleep interaction +2.31 [+1.70, +2.91] < 0.001
Log hours worked −1.11 [−1.68, −0.55] < 0.001
Screen time (normalised) −1.36 [−2.71, 0.00] 0.050

R² = 0.436, Adj. R² = 0.429, HC3 robust standard errors

Causal Inference — Double ML

Estimand θ̂ 95% CI p
Total Effect of sleep (1 hr) +1.89 pts [+1.52, +2.27] < 0.001
Direct Effect (excl. motivation) +1.66 pts [+1.32, +2.00] < 0.001
Indirect via motivation (NIE) +0.56 pts [+0.34, +0.80] < 0.001

Proportion mediated through motivation (SDT pathway): 26.6%

Bayesian Posteriors

Parameter Posterior Mean 95% CrI P(β > 0)
beta_sleep_hours +2.58 [+2.24, +2.83] 100%
beta_task_deep +4.33 [+4.03, +4.71] 100%
beta_deep×sleep +1.01 [+0.82, +1.25] 100%
beta_start_hour −2.05 [−2.37, −1.76] 0%

MH-MCMC: 15,000 iterations, burn-in = 33%, acceptance rate 36% (textbook optimal)

Counterfactual Simulation — Baseline: 6h sleep, shallow work, late coffee

Intervention Δ Productivity 90% CI
Sleep +1h → 7h +3.5 pts [+2.9, +4.0]
Sleep +2h → 8h +6.9 pts [+5.8, +8.0]
Switch to deep work +5.2 pts [+3.5, +6.9]
Move coffee to 9AM +0.0 pts (next-day effect)
Full behavioural package +17.3 pts [+15.2, +19.5]

Streamlit Dashboard

Four interactive tabs:

Tab Content
📊 Profile & Prediction Sidebar sliders → real-time prediction with predictive distribution and 3 dose-response curves
🔮 Counterfactual Planner Build custom interventions → causal lift with 90% uncertainty bands
📈 Statistical Deep Dive Data explorer · correlation matrix · enriched DAG · Bayesian posteriors
🤖 AI Research Insights Generate research-backed narration from 5 insight templates

EDA Notebook

notebooks/exploration.ipynb — 32 cells, fully executed, 9 annotated sections. Every finding is linked to the modelling decision it informs.

To open:

jupyter notebook notebooks/exploration.ipynb

Limitations

  1. Synthetic data — the DGP reflects literature but real data will contain unmeasured confounders, measurement error, and more complex feedback loops
  2. SUTVA — consecutive-day fatigue accumulation may violate the Stable Unit Treatment Value Assumption
  3. Single circadian curve — chronotype heterogeneity (larks vs owls) is partially captured by the latent [chronotype] variable but not fully modelled
  4. Rosenbaum Gamma = 1.25 — a moderate sensitivity result; real chronotype data would strengthen identification considerably
  5. LLM knowledge base — the insight layer is limited to 12 pre-loaded research concepts; do not treat outputs as clinical advice
  6. Population scope — findings are calibrated for knowledge workers; different optimal profiles likely exist for other work types

Installation Details

Python 3.10 or higher required.

python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install -r requirements.txt

# For Bayesian inference with PyMC (optional, falls back to scipy MH)
pip install pymc arviz

# For LLM insights
cp .env.example .env
# Add: ANTHROPIC_API_KEY=sk-ant-...

Running the Pipeline

# Full pipeline
python run_all.py

# Skip MCMC (faster)
python run_all.py --skip-bayes

# Individual modules
python src/data_generation.py
python src/regression_model.py
python src/causal_analysis.py
python src/bayesian_model.py
python src/simulation.py

# Dashboard
streamlit run app.py

# Tests
pytest tests/ -v

Contributing

Please read CONTRIBUTING.md first. Key areas of interest:

  • Real-world data connectors (Oura Ring, Toggl, RescueTime APIs)
  • Instrumental variable estimation (IV using weather/commute as instruments)
  • Causal Forest (Wager & Athey, 2018) for continuous CATE estimation
  • PyMC v5 hierarchical model with individual-level random effects
  • Chronotype-stratified analysis using MEQ scores

References

  • Baard et al. (2004). Journal of Applied Psychology, 89(4), 781.
  • Chernozhukov et al. (2018). Double/debiased ML. Econometrica, 86(3), 451-475.
  • Cohen, Kamarck & Mermelstein (1983). J Health and Social Behavior, 24(4), 385.
  • Deci & Ryan (2000). Self-determination theory. Psychological Inquiry, 11(4), 227-268.
  • Dijk & Czeisler (1995). Journal of Neuroscience, 15(5), 3526-3538.
  • Drake et al. (2013). Journal of Clinical Sleep Medicine, 9(11), 1195-1200.
  • Ericsson, Krampe & Tesch-Römer (1993). Psychological Review, 100(3), 363-406.
  • Gelman et al. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.
  • Kleitman (1982). Basic rest-activity cycle. Experientia, 38, 1509-1513.
  • Lieberman et al. (2002). Psychopharmacology, 164(3), 250-261.
  • Lorist et al. (2005). Neuropsychologia, 43(9), 1400-1412.
  • MacKinnon & White (1985). Journal of Econometrics, 29(3), 305-325.
  • Monk (2005). Occupational and Environmental Medicine, 62(1), 1-8.
  • Newport (2016). Deep Work. Grand Central Publishing.
  • Pearl (2009). Causality: Models, Reasoning and Inference. Cambridge University Press.
  • Preacher & Hayes (2008). Behavior Research Methods, 40(3), 879-891.
  • Rosenbaum (2002). Observational Studies (2nd ed.). Springer.
  • Rubin (1974). Journal of Educational Psychology, 66(5), 688-701.
  • Salleh (2008). AMSJ, 3(2), 9-15.
  • Van Dongen et al. (2003). SLEEP, 26(2), 117-126.
  • Walker (2017). Why We Sleep. Scribner.

License

MIT License. See LICENSE for details.


Built with statistical rigour. Interpreted with care.

Releases

No releases published

Packages

 
 
 

Contributors