Question 1.
Please use Numpy to generate the following ndarray.
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
1 3 5 7 9 11 13 15 17 19
2 4 6 8 10 12 14 16 18 20
1 1 2 3 5 8 13 21 34 55
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0 0
Tip: you may use the numpy.vstack function
Question 2.
Import the provided stock data into a DataFrame and use Matplotlib to plot the daily K bar with the
dataframe.
Tips: For each K bar, only the open, high, low, close price need to be considered in this question.
Question 3.
Please use Numpy to generate random numbers and use Monte Carlo method to calculate π
(3.1415926)
Tip: please read the appendix to understand the Monte Carlo method to calculate π.
Appendix:
We start the familiar example of finding the area of a circle. Figure 1 below shows a circle with
radius inscribed within a square. The area of the circle is , and the area of the
square is . The ratio of the area of the circle to the area of the square is
Figure 1.
If we could compute ratio, then we could multiple it by four to obtain the value . One particularly
simple way to do this is to pick lattice points in the square and count how many of them lie inside the
circle, see Figure 2. Suppose for example that the points are
selected, then there are 812 points inside the circle and 212 points outside the circle and the percentage
of points inside the circle is . Then the area of the circle is
approximated with the following calculation
Figure 2.
Monte Carlo Method for
Monte Carlo methods can be thought of as statistical simulation methods that utilize a sequences of
random numbers to perform the simulation. The name “Monte Carlo” was coined by Nicholas
Constantine Metropolis (1915-1999) and inspired by Stanslaw Ulam (1909-1986), because of the
similarity of statistical simulation to games of chance, and because Monte Carlo is a center for gambling
and games of chance. In a typical process one compute the number of points in a set A that lies inside
box R. The ratio of the number of points that fall inside A to the total number of points tried is equal to
the ratio of the two areas (or volume in 3 dimensions). The accuracy of the ratio depends on the
number of points used, with more points leading to a more accurate value.
A simple Monte Carlo simulation to approximate the value of could involve randomly selecting
points in the unit square and determining the ratio , where is number of points
that satisfy . In a typical simulation of sample size there were points satisfying
, shown in Figure 3. Using this data, we obtain
and
http://scienceworld.wolfram.com/biography/Metropolis.html
http://scienceworld.wolfram.com/biography/Metropolis.html
http://www-groups.dcs.st-and.ac.uk/~history/Mathematicians/Ulam.html
Figure 3.
Every time a Monte Carlo simulation is made using the same sample size it will come up with a
slightly different value. The values converge very slowly of the order . This property is a
consequence of the Central Limit Theorem.