0% found this document useful (0 votes)
8 views3 pages

Document 15

x
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)
8 views3 pages

Document 15

x
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/ 3

CODE EXPLANATION PYTHON

Question 1 :

This Python code estimates the value of pi using Monte Carlo simulation. Here's how it works:

1. The code imports the `random` and `math` modules.

2. The variable `N` is set to 1000000, indicating the number of random points to generate.

3. The variable `count` is initialized to 0, and will be used to keep track of how many random points
fall inside a circle inscribed in a square.

4. The code enters a `for` loop that runs `N` times. On each iteration, it generates two random
numbers `x` and `y` using the `random.uniform(0,1)` function. These numbers are between 0 and 1
and represent the (x, y) coordinates of a point in the square.

5. The distance of this point from the origin is calculated using the `math.sqrt(x**2 + y**2)`
function. If this distance is less than 1, then the point is inside the inscribed circle.

6. If the point is inside the circle, the `count` variable is incremented by 1.

7. After the loop has finished, the approximation of pi is calculated using the formula `pi_estimate =
4 * count / N`. This formula uses the ratio of the number of points inside the circle to the total
number of points to estimate the area of the circle, which is proportional to pi. The factor of 4 comes
from the fact that the circle is inscribed in a square whose side length is 2, so its area is 4.

8. The approximation of pi is printed to the console using the `print` function.

The resulting value printed to the console is an approximation of pi based on the Monte Carlo
simulation. The accuracy of the approximation depends on the number of points generated, with a
larger number of points resulting in a more accurate estimate.

Question 2 :

This Python code defines a function `n(k)` which returns 10 to the power of `k`, and a function
`estimate_pi(k)` which generates `N = n(k)` random points and estimates pi by counting the number
of points that fall within a unit circle and dividing by the total number of points.

The code then generates a list of `k` values to use for the pi estimation and uses a list
comprehension to calculate the corresponding `pi` estimates using `estimate_pi(k)`.

Finally, the code plots the true value of pi (imported from the math module), as well as the
approximate pi estimates for each `k` value using `matplotlib.pyplot.plot`. The x-axis is the value of
`k`, and the y-axis is the value of pi. The plot shows how the accuracy of the pi estimate improves as
`k` increases, with the approximate values of pi converging towards the true value of pi.

Question 3
This code defines a function named `gaussian_dis` that takes a single integer argument `n`. The
function generates `n` pairs of random numbers that are distributed according to the 2D Gaussian
(normal) distribution.

Here's how the function works:

1. First, the function uses the `random` function from the `numpy.random` module (which is
imported with the alias `npr`) to generate two arrays of `n` random numbers between 0 and 1. These
arrays are stored in the variables `U_1` and `U_2`. (u_1 and u_2 are independant)

2. The function then applies some mathematical transformations to these random numbers to
generate pairs of random numbers with a Gaussian distribution.

Specifically, the function uses the inverse transform method, which involves taking two uniformly
distributed random numbers, transforming them using a set of mathematical functions, and
obtaining a pair of normally distributed random numbers.

3. The mathematical transformations used in this function involve the following steps:

a. The function calculates the square root of the negative logarithm of `U_1`, which gives a value
distributed according to a chi-squared distribution with 2 degrees of freedom.

b. The function generates a random angle `teta` between 0 and 2π using `U_2`.

c. The function calculates the `X` and `Y` coordinates of a point with a distance of `R` from the
origin and an angle of `teta` using the trigonometric functions `cos` and `sin`.

4. Finally, the function returns a tuple of two arrays `X` and `Y`, each containing `n` random numbers
that are normally distributed with a mean of 0 and a standard deviation of 1.

Note that this implementation relies on the `numpy` module, as well as the `numpy.random`
submodule, so you need to have these packages installed and imported before you can use this
function.
Question 4:
This code generates `X` and `Y` coordinates for a set of 10 million points that are normally
distributed using the `gaussian_dis` function, and then creates a two-panel histogram plot of the
resulting data.

Here's how the code works:

1. The `X` and `Y` variables are assigned the values returned by the `gaussian_dis` function when
called with the argument `int(10e6)`. `int(10e6)` evaluates to 10,000,000, so this generates 10
million normally distributed random numbers for both `X` and `Y`.

2. The `np.linspace` function from the `numpy` module is used to generate an array `T` of 100
equally spaced values between -4 and 4. This array is used to plot the normal distribution curves.

3. The `plt.subplot` function from the `matplotlib.pyplot` module is used to create a two-panel plot
with two subplots, one above the other.

4. In the first subplot (at the top), a histogram of the `X` values is plotted using `plt.hist` function
with the `density` parameter set to `True` and the number of bins set to 30. The `density=True`
option scales the histogram so that the total area of the bars sums to 1, effectively converting it into
a probability density function. (Normalize because surface area equals to 1)

(bins fonction does this, In Python, the term "bins" usually refers to the number of intervals or
partitions that are used to group data in a histogram.)

5. A normal distribution curve is also plotted on top of the histogram using the `plt.plot` function,
with `T` on the x-axis and the corresponding normal distribution values (calculated using the formula
`1/np.sqrt(2*np.pi)*np.exp(-T**2/2)`) on the y-axis. This curve is shown in blue color.

6. In the second subplot (at the bottom), a histogram of the `Y` values is plotted using `plt.hist`
function with the `density` parameter set to `True` and the number of bins set to 30.

7. Another normal distribution curve is plotted on top of the second histogram using the `plt.plot`
function, with `T` on the x-axis and the corresponding normal distribution values on the y-axis. This
curve is also shown in blue color.

The resulting plot shows the normally distributed `X` and `Y` values as histograms along with the
theoretical normal distribution curves in blue. This plot can be used to visualize the distribution of
the random numbers and compare it to the theoretical normal distribution.

You might also like