0% found this document useful (0 votes)
1 views5 pages

Pi Complete

The document details a Monte Carlo simulation method for estimating the mathematical constant π by generating random points in a square and calculating the proportion that falls within an inscribed unit circle. It outlines the methodology, including generating random samples, checking points inside the circle, counting them, and estimating π iteratively over multiple iterations. The results indicate convergence towards the true value of π, demonstrating the effectiveness of probabilistic methods in numerical approximation.

Uploaded by

sejaldubey125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Pi Complete

The document details a Monte Carlo simulation method for estimating the mathematical constant π by generating random points in a square and calculating the proportion that falls within an inscribed unit circle. It outlines the methodology, including generating random samples, checking points inside the circle, counting them, and estimating π iteratively over multiple iterations. The results indicate convergence towards the true value of π, demonstrating the effectiveness of probabilistic methods in numerical approximation.

Uploaded by

sejaldubey125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

HW_2 Question 2

Estimation of �

Rupesh, Shivam, Sejal, Saurav, Prakhar

2025-01-25

Pi Estimation Report

Estimation of � Using Monte Carlo Simulation

Introduction
Monte Carlo simulation is a probabilistic method that uses random sampling to estimate
numerical results. In this, we estimate the mathematical constant � by simulating random
points in a square and calculating the proportion of points that fall inside a unit circle inscribed
within the square.

Methodology

1. Generate Random Samples

• Random coordinates are generated uniformly in the range (-1, 1).


• Each point represents a random location in the square of side length 2.

n = 1e4 # Total number of points


x = runif(n, min = -1, max = 1) # Generate random x-coordinates
y = runif(n, min = -1, max = 1) # Generate random y-coordinates

2. Check Points Inside the Circle

• A point lies inside the circle if its distance from the origin is less than or equal to 1.
• This condition is checked for all generated points.

1
inside_circle = (x^2 + y^2 <= 1) # Logical vector indicating points inside the circle

3. Count Points Inside the Circle

• The total number of points inside the circle is calculated by summing the logical values.

count = sum(inside_circle) # Count points inside the circle

4. Estimate �

• The value of � is estimated using the formula:

pi_estimate = 4 * count / n
print(pi_estimate) # Print the estimated value of �

[1] 3.1384

5. Iterative Refinement

• To analyze convergence, � is estimated iteratively over multiple iterations, each with a


new set of random points.

pi_values = function(n) {
x = runif(n, min = -1, max = 1)
y = runif(n, min = -1, max = 1)
count = sum(x^2 + y^2 <= 1)
return(4 * count / n)
}

n_iterations = 1000
pi_arr = sapply(1:n_iterations, pi_values)
pi_arr_sample = sample(pi_arr, 100)
pi_arr_sample

[1] 3.096774 3.392000 3.215686 3.092398 3.242902 3.329670 3.142222 3.224806


[9] 3.132075 3.089655 3.153061 3.164420 3.404580 3.064327 3.086614 3.134021
[17] 3.187970 3.428571 3.018072 3.197568 3.188275 3.153110 3.285714 3.207503
[25] 3.299475 3.094188 3.034301 3.029536 3.056180 3.175676 3.205993 2.630137
[33] 3.160622 3.242718 3.104762 2.723404 3.192444 3.213251 3.285714 3.162162

2
[41] 2.995633 3.077748 3.119048 3.286031 3.186885 3.116641 3.111111 3.198257
[49] 3.167048 3.065896 2.933333 3.322581 3.107981 2.968553 3.152888 3.130435
[57] 2.666667 3.600000 3.140351 3.090909 3.206544 3.019763 3.140741 3.222482
[65] 3.456790 3.134986 3.163359 3.341176 3.163043 3.042945 3.293777 3.112500
[73] 3.109948 3.099851 3.183217 3.232000 3.087719 3.101370 3.108943 3.164557
[81] 3.483871 3.106439 3.158576 3.177143 3.097030 3.136000 3.118227 3.253142
[89] 3.105691 3.150115 3.182796 3.156812 2.961326 2.787879 3.246377 3.187141
[97] 3.125000 3.143552 3.303797 3.099415

6. Visualization

• A plot is created to show the estimated values of � over the iterations.

# Prepare data for plotting


pi_df = data.frame(X = seq_along(pi_arr), Y = pi_arr)

# Plot using ggplot2


library(ggplot2)
pi_plot = ggplot(pi_df, aes(X, Y)) +
geom_line(color = "blue") +
geom_point(shape = 21, fill = "#FF6347", size = 1) +
geom_hline(yintercept = pi, linetype = "dashed", color = "red") +
labs(title = "Estimation of �", x = "Iterations", y = "Estimated Value of �") +
coord_cartesian(ylim = c(3, 3.5), xlim = c(0, n_iterations)) +
theme_minimal()

# Display results
print(mean(pi_arr)) # Average of all estimates

[1] 3.135534

print(pi_plot) # Display the plot

3
Estimation of p
3.5

3.4
Estimated Value of p

3.3

3.2

3.1

3.0
0 250 500 750 1000
Iterations

Results

1. Convergence

• As the number of iterations increases, the estimated values of � converge to the true value
(� = 3.14159).

2. Stability

• The average of the estimates over 1000 iterations provides a robust approximation of �.

3. Visualization

• The plot demonstrates the convergence of the estimates towards the true value of �, with
a dashed line marking the reference value.

4
Conclusion

Monte Carlo simulation effectively estimates � by leveraging random sampling. While initial
estimates may vary, iterative refinements demonstrate convergence to the true value of �. This
approach showcases the power of probabilistic methods in numerical approximation.

You might also like