Monte Carlo simulation helps us understand uncertain situations by trying many random examples. It shows possible outcomes and their chances.
Monte Carlo simulation basics in NumPy
import numpy as np # Step 1: Generate many random samples samples = np.random.random(size=N) # Step 2: Apply a function or condition to samples results = some_function(samples) # Step 3: Calculate statistics like mean or probability estimate = np.mean(results)
np.random.random(size=N) creates N random numbers between 0 and 1.
You can replace some_function with any calculation or test you want to simulate.
import numpy as np N = 10000 samples = np.random.random(size=N) estimate = np.mean(samples) print(estimate)
import numpy as np N = 100000 samples_x = np.random.random(size=N) samples_y = np.random.random(size=N) inside_circle = (samples_x**2 + samples_y**2) < 1 pi_estimate = 4 * np.mean(inside_circle) print(pi_estimate)
This program uses Monte Carlo simulation to estimate π. It randomly picks points in a square and counts how many fall inside a quarter circle. The ratio helps estimate π.
import numpy as np # Number of random trials N = 100000 # Generate random points in the square [0,1] x [0,1] x = np.random.random(size=N) y = np.random.random(size=N) # Check if points fall inside the quarter circle of radius 1 inside_circle = (x**2 + y**2) <= 1 # Estimate pi using the ratio of points inside the circle pi_estimate = 4 * np.mean(inside_circle) print(f"Estimated value of pi: {pi_estimate:.5f}")
More trials (larger N) give better estimates but take more time.
Monte Carlo simulation works well when exact math is hard or impossible.
Randomness means results will be slightly different each time you run the code.
Monte Carlo simulation uses random sampling to estimate results.
It is useful for problems with uncertainty or complex math.
Using numpy makes it easy to generate many random samples quickly.
Practice
Solution
Step 1: Understand Monte Carlo simulation basics
Monte Carlo simulation uses random sampling to estimate results when exact calculation is hard.Step 2: Identify the main purpose
It helps estimate outcomes by simulating many random scenarios.Final Answer:
To estimate results using random sampling -> Option CQuick Check:
Monte Carlo = random sampling estimation [OK]
- Confusing Monte Carlo with data sorting
- Thinking it cleans data
- Assuming it creates charts
Solution
Step 1: Identify numpy functions for randomness
numpy.random.rand() generates random numbers between 0 and 1, useful for sampling.Step 2: Compare with other options
numpy.array() creates arrays, numpy.mean() calculates average, numpy.sort() sorts data; none generate random samples.Final Answer:
numpy.random.rand() -> Option AQuick Check:
Random samples = numpy.random.rand() [OK]
- Choosing numpy.array() instead of random function
- Confusing mean or sort with random sampling
- Using functions that don't generate randomness
import numpy as np np.random.seed(0) samples = np.random.rand(5) mean_val = np.mean(samples) print(round(mean_val, 2))
Solution
Step 1: Generate 5 random samples with seed 0
Using np.random.seed(0) fixes randomness. np.random.rand(5) produces [0.5488135, 0.71518937, 0.60276338, 0.54488318, 0.4236548].Step 2: Calculate mean and round
Sum = 2.83530423, mean = 0.567060846, round(mean_val, 2) = 0.57.Final Answer:
0.57 -> Option BQuick Check:
Mean of samples = 0.57 [OK]
- Not setting seed causing different outputs
- Rounding incorrectly
- Calculating sum or mean wrongly
import numpy as np samples = np.random.rand(1000) result = sum(samples) / 100 print(result)
Solution
Step 1: Check calculation of average
sum(samples) sums 1000 values, but dividing by 100 underestimates average.Step 2: Correct divisor
To get average, divide sum by 1000 (number of samples), not 100.Final Answer:
Dividing by 100 instead of number of samples -> Option DQuick Check:
Average = sum / number of samples [OK]
- Dividing by wrong number
- Confusing random functions
- Ignoring seed importance
Solution
Step 1: Generate 2D points inside 1x1 square
np.random.rand(10000, 2) creates 10000 points with x,y in [0,1].Step 2: Calculate distance from center (0.5,0.5)
Distance formula sqrt((x-0.5)^2 + (y-0.5)^2) applied to each point.Step 3: Count points inside circle and estimate probability
Count points with distance ≤ 0.5, divide by total points for probability.Final Answer:
np.random.rand(10000, 2) with column indexing for coordinates -> Option AQuick Check:
2D points + distance check = probability [OK]
- Using 1D points instead of 2D
- Wrong shape for points array
- Using randint instead of rand for floats
Ready to prove it?
Pass the test and this pattern moves unknown
