Home > Python > Application in Statistical Mechanics

Ch. 21.7 Monte Carlo Method

Chapters Index
Program 1

Monte Carlo Method

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Estimation of \( \pi \) using the Monte-Carlo Method:

Theoretical Background & Computational Mechanics

Generating Random Points and Estimating \( \pi \)

inside = sum(u(-1, 1, n)**2 + u(-1, 1, n)**2 + u(-1, 1, n)**2 <= 1)

This line performs:

  • u(-1, 1, n) generates n random numbers uniformly distributed between -1 and 1. The expression u(-1, 1, n)**2 computes the square of each of these random numbers.
  • The process is repeated three times to generate three sets of random numbers, which represent the coordinates \( (x, y, z) \) in a 3D space.
  • The code calculates the condition:
  • $$ x^2 + y^2 + z^2 \leq 1 $$

    This condition checks whether each of the generated points lies inside a unit sphere (which is defined by the equation \( x^2 + y^2 + z^2 \leq 1 \)).

  • sum(...) counts how many of these points fall inside the unit sphere. The result is stored in the variable inside.

Estimating \( \pi \):

pi = 6.0 * inside / n

estimation of \( \pi \) using the formula:

The volume of the unit sphere is \( \frac{4}{3}\pi \cdot1^3 \), while the volume of the cube defined by the range \( [-1, 1] \) is \( 2^3 = 8 \).

$$ \frac{V_{sphere}}{V_{cube}} = \frac{\frac{4}{3}\pi}{8}$$

$$ \pi \approx \frac{6 \cdot (\text{number of points inside the sphere})}{\text{total number of points}} $$

Program 2

Monte Carlo Method

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

\[ \int_{a}^{b} x^2 \sin(x/2) \, dx \] using MC method

Program 3

Monte Carlo Method

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

\[ \int_{a}^{b} x^3 \sin(x) \, dx \] using MC method

Program 4

Monte Carlo Method

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

\[ \int_{a}^{b} \exp(-2 \cdot |x - 5|) \, dx \] using MCI Importance sampling method

Theoretical Principles & Computational Physics Guide

Monte Carlo Method: Statistical Physics & Stochastic Modeling

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Stochastic Processes: Simulates random physical phenomena including Brownian diffusion, radioactive decay, and thermal fluctuations.
Ensemble Averages: Computes macroscopic thermodynamic observables as statistical ensemble averages over microscopic configurations.
Monte Carlo Convergence: Statistical error scales inversely with the square root of trial count: $\sigma \propto 1/\sqrt{N}$.

2. Mathematical Formulation & Algorithmic Pipeline

Random walk diffusive spreading and Monte Carlo error scaling:

$$ \langle x(t) \rangle = 0, \quad \langle x^2(t) \rangle = 2 D t, \quad \text{Statistical Uncertainty: } \sigma_N = \frac{\sigma}{\sqrt{N}} $$

3. Numerical Mechanics & Implementation Logic

  • Random Number Seeding: Initialize pseudo-random generators with fixed seeds to ensure reproducible simulation results.
  • Ensemble Sampling: Execute $M$ independent trials of $N$ steps each to build accurate probability distributions.
  • Histogramming: Bin particle final positions to verify convergence toward Gaussian/Poisson distributions.

4. Relevant Computational Practice Exercises

Exercise 1
Simulate $M = 1000$ independent random walk trajectories and confirm that root-mean-square displacement scales as $\sqrt{t}$.
Exercise 2
Apply Monte Carlo integration to estimate $\pi$ using hit-or-miss circle sampling and plot error vs trial count $N$.
Exercise 3
Model radioactive nuclear decay for $N_0 = 10^4$ nuclei and compare against the continuous decay law $N(t) = N_0 e^{-\lambda t}$.
Standard Literature & References:
  • F. Reif, "Fundamentals of Statistical and Thermal Physics", Waveland Press (2009).
  • Mark Newman, "Computational Physics", University of Michigan (2013).