DEV Community

Cover image for Monte Carlo simulation engine for Carbon and Climate-economic risk. Its is a model for how cities can visualize their climate futures.
Erick Mwangi Muguchia
Erick Mwangi Muguchia

Posted on

Monte Carlo simulation engine for Carbon and Climate-economic risk. Its is a model for how cities can visualize their climate futures.

DEV Weekend Challenge: Earth Day

This is a submission for Weekend Challenge: Earth Day Edition

What I Built

Instead of building a basic personal carbon footprint tracker, I wanted to tackle the macro problem: convincing governments to act. This project directly honors Earth Day by providing a localized, mathematical engine to prove that climate action is an economic necessity.
This project models climate change as a risk problem, not just an environmental issue. By simulating emissions and their economic consequences, it reframes sustainability as something governments and institutions cannot afford to ignore.

For a city like Nairobi, the question becomes clear: reducing emissions isn’t just good for the planet, it stabilizes the future economy. That’s the argument this tool is built to prove.

Demo

The concept applies actuarial risk math (Monte Carlo simulations) to climate science. Instead of just proving that emissions drop, it proves that economic volatility and extreme risk drop. Piping that hard math directly into an AI (Google Gemini) to instantly generate human-readable policy briefs bridges the gap between data scientists and politicians.

The Math **models.py**: Inside this file, the code literally flips a weighted coin every single year for 20 years to add random surprises (stochastic shocks). Sometimes emissions randomly spike, sometimes they crash.

The Engine **simulator.py**: It plays out these 20 years, 10,000 separate times. It runs it once doing nothing ("Baseline") and once where the city cuts emissions ("Intervention").

The Proof **visualization.py**: It takes all 10,000 parallel universes and draws a funnel (the graphs). It proves that climate action doesn't just reduce carbon, it drastically reduces risk and volatility. The resulting graph visually proves that by acting now, we stop the worst-case, most expensive catastrophic futures from happening.

The AI **policy_brief.py**: Raw statistical variance is boring to politicians. So you take the billions of dollars saved in those 10,000 universes and feed them into Google Gemini, asking it to write an inspiring, plain-English speech for the local city council based solely on your math.

This project is intentionally built as a lightweight CLI tool. It runs entirely on a CPU with no external dependencies, making it usable in low-resource environment.
Plus I automated the demo with run_demo.sh file.

This clip shows the simulator running — baseline emissions rise while intervention drives them toward zero.

Limitation & Context

This simulator is designed as a conceptual actuarial engine, not a direct measurement of Nairobi’s emissions. The numbers are stylized to demonstrate how intervention changes risk trajectories, using Nairobi as a case study.

Several external factors are not yet modeled, including population growth, energy mix, transport electrification, and economic shocks. These would all influence real‑world emissions. For now, the model focuses on the risk dynamics: baseline emissions rise with widening uncertainty, while intervention collapses both emissions and variance.

Code

You can find the code on github:

GitHub logo ricsmwangi / carbon-risk-simulator

Carbon Risk Simulator is a lightweight, actuarial-inspired simulation tool that models climate change as a risk problem rather than just an environmental trend. Instead of producing a single forecast, the system uses Monte Carlo simulation to generate thousands of possible future emission paths.

Nairobi Carbon Risk Simulator

A lightweight, pure-Python Monte Carlo simulation engine designed to project carbon emissions pathways and their associated economic damages. Built specifically for Earth Day risk assessments and localized climate action policy.

What it is

Unlike linear carbon calculators, the ** Nairobi Carbon Risk Simulator** leverages actuarial mathematics to model deep uncertainty. It applies stochastic random shocks (normal and lognormal distributions) across 10,000+ simulated futures to measure not just how many emissions might be reduced, but how much volatility and economic risk is eliminated by taking climate action.

Features

  • Ultra-Lightweight CLI: Stripped of heavy dataframe dependencies dataframes. Relies entirely on numpy for blazing fast, vectorized Monte Carlo matrices and native Python csv for exports.
  • Stochastic Risk Modeling: Compare "Business as Usual" against intervention scenarios across 20-year horizons.
  • Data Visualization: Autogenerates high-resolution probabilistic funnels (5th-95th percentiles) and final-outcome histograms using matplotlib.
  • AI Policy Translation: Integrates Google Gemini to…

How I Built It

I approached this project from an actuarial mindset — focusing on risk and uncertainty instead of just prediction.

At the core, I used Monte Carlo simulation to model thousands of possible future emission paths over time. Instead of a single outcome, the system generates a range of scenarios, showing not only expected emissions but also variability and extreme cases.

I built the simulation engine in Python using NumPy for fast computations. The model is parameter-based, so values like emission rates, reduction percentages, and economic impact can easily be adjusted.

To make the results more meaningful, I added scenario comparison:

  • a baseline (no changes)
  • and a reduced-emission scenario (e.g. 20%)

Our base model targets a 20% reduction, but the Monte Carlo engine can instantly simulate any alternative policy percentage, like 50% or 80%, for sensitivity analysis.

This allows the system to show how policy decisions affect both emissions and economic risk over time.

Random seeds are fixed so results can be replicated exactly.

I also used GitHub Copilot to help expand and structure the idea. It assisted with refining the simulation logic and organizing the code, while I focused on the modeling approach and assumptions.

Finally, I kept everything as a simple CLI tool. This keeps the project lightweight, fast, and able to run on any machine without special requirements.

Prize Categories

I am submitting this project under two categories:

Best Use of GitHub Copilot
Copilot was essential in scaffolding the actuarial engine and Monte Carlo simulation logic. It accelerated development by suggesting reproducible code structures, statistical functions, and console formatting for the Earth Day–themed demo output. Copilot helped keep the workflow modular and clear, ensuring the simulator could scale to multiple scenarios.

Key emphasis:

  • Guided the modular design of the simulator, from models.py to policy_brief.py, ensuring reproducibility and clarity.
  • Polished console outputs, turning raw math into a clean demo flow.
  • Accelerated iteration speed, letting me focus on actuarial modeling instead of boilerplate code.

Best Use of Google Gemini

Gemini transformed raw simulation results into formal policy briefs. By piping CSV summaries and scenario outcomes into Gemini, the simulator automatically generated council‑ready narratives that reframed actuarial math into persuasive, human‑readable language for decision makers. This bridged the gap between technical rigor and policy communication.

Key emphasis:

  • Reframed statistical variance collapse into persuasive council speeches.
  • Bridged actuarial math with civic communication, making the simulator resonate beyond the data.
  • Aligned perfectly with the Earth Day theme by translating technical outputs into accessible calls for urgent climate action.

Top comments (0)