In: Computer Science
Python Problem
Suppose you invest $50,000 today into some kind of investment
fund. Assume
that investments in this fund grow by some rate that is not
entirely predictable.
However, we believe growth of the fund to be normally distributed
with an av-
erage growth of 7% and standard deviation of 5%. The following
chart displays
the believed relative likelihood of various growth amounts for this
fund.
As we can see above, a wide range of growth rates are possible, but
the bulk of
them are clustered around 7% growth (0.07).
Simulating a single possible outcome for the fund is not nearly as
valuable
as simulating many potential outcomes for the fund. Therefore, we
wish to do
100,000 simulations of the possible values after 30 years.
To do so, we could use the following code (see the .py le
referenced at the
beginning of this question).
import numpy
listOfOutcomes = []
for i in range(100000):
currentValue = 50000
for year in range(30):
growth = numpy.random.normal(0.07,0.05)
currentValue *= (1+growth)
finalValue = currentValue
listOfOutcomes.append(finalValue)
At the end of the simulation, the variable listOfOutcomes has a
length of
100,000 and records the nal value of the fund at the end of each of
the 100,000
simulations. The variable listOfOutcomes is of the list data
type.
Part (a): utilize the sort() method of the list data type to sort
the outcomes.
Part (b): what was the lowest value observed in your simulation?
(Hint: because
we sorted the list, the list is sorted from least to greatest. The
rst item of the list
(i.e the smallest value outcome) can be viewed by using
print(listOfOutcomes[0])).
Part (c): what was the maximum value observed in your simulation?
(Think
back to Question 1 and the use of negative indices to reference
elements near
the end of a list.)
Part (d): compute the 90th percentile outcome. Compute the 10th
percentile
outcome. (Hint: you should be able to view the 70th percentile
outcome, for
example, by using print(listOfOutcomes[69999]). This prints the
70,000th
entry in a sorted list of length 100,000, so this is the 70th
percentile value.)
Part (e): compute the average (mean) value of the investment in the
100,000
simulations. (Hint: using sum(listOfOutcomes) and dividing by the
number
of trials might be helpful.) Is this more or less than the median
value? (Recall
the median is the 50th percentile.)
Here is the required code:-
import numpy
listOfOutcomes = []
for i in range(100000):
currentValue = 50000
for year in range(30):
growth = numpy.random.normal(0.07,0.05)
currentValue *= (1+growth)
finalValue = currentValue
listOfOutcomes.append(finalValue)
#A) Sorting outcomes
listOfOutcomes.sort()
#B) Lowest Value
print("Lowest Value Observed: " + str(listOfOutcomes[0]))
#C) Highest Value
print("Highest Value Observed: " + str(listOfOutcomes[-1]))
#D) Computing 90th and 10th Percentile
print("90th Percentile Outcome: " +
str(listOfOutcomes[89999]))
print("10th Percentile Outcome: " + str(listOfOutcomes[9999]))
#E) Computing Mean and compairing it with Median
n = len(listOfOutcomes)
s = sum(listOfOutcomes)
mean = s/n
print("Mean: " + str(mean))
median = listOfOutcomes[49999]
print("Meadian: " + str(median))
if mean>median:
print("Mean is more than Median.")
else:
print("Mean is less than Median.")
The code would produce the following output:-