Skip to content

Estimating π using multiple numerical algorithms, with clean architecture, reproducibility, and scientific visualization.

License

Notifications You must be signed in to change notification settings

santagatiivan/montecarlo-vs-series-pi-estimation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Estimating π: A Computational Comparison of Monte Carlo and Series Expansions

This repository investigates numerical techniquues to approximate the value of π using both probabilistic (Monte Carlo) and deterministic (Leibniz and Nilakantha series) approaches. The project provides an educational and computational comparison of convergence behavior, precision, and implementation complexity of these classical algorithms.

  • Monte Carlo Simulation (stochastic approach)
  • Leibniz Series Expansion (alternating infinite series)
  • Nilakantha Series Expansion (rapidly converging series)

The implementation is designed to provide both numerical and graphical insights into the convergence behavior and relative accuracy of each method.


Mathematical Foundations

Monte Carlo Method

The Monte Carlo method utilizes random sampling to approximate π. Points are uniformly distributed within a unit square. The ratio of points falling inside the inscribed unit circle provides a statistical estimate of π:

π ≈ 4 × (points inside circle) / (total points)

Leibniz Series

A well-known infinite series derived from the Taylor expansion of arctangent:

π ≈ 4 × (1 − 1/3 + 1/5 − 1/7 + 1/9 − ...)

While theoretically valid, its convergence is extremely slow.

Nilakantha Series

An improvement over the Leibniz formula, this series converges more rapidly by adding and subtracting rational terms starting from 3:

π ≈ 3 + 4/(2·3·4) − 4/(4·5·6) + 4/(6·7·8) − ...


Objectives

  • Estimate π using numerical methods
  • Measure absolute and relative error against the true value of π
  • Visualize convergence trends across different algorithms
  • Export results in machine-readable formats (CSV)

Example Output (1,000,000 iterations)

Method π Estimate Absolute Error Relative Error (%)
Monte Carlo 3.1416000000 0.0000073466 0.00023386
Leibniz 3.1415926535 0.0000000001 0.00000000
Nilakantha 3.1415926535 0.0000000001 0.00000000

Visualization

The program generates a convergence plot showing:

  • π estimates across iterations
  • Method-specific trajectories
  • Horizontal reference line representing true π

This allows for direct visual inspection of convergence speed and stability.


How to Use

  1. Install dependencies:
pip install -r requirements.txt
  1. Run the script:
cd src
python main.py

License

This project is released under the MIT License.

About

Estimating π using multiple numerical algorithms, with clean architecture, reproducibility, and scientific visualization.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages