Computer Science Data Manipulation and Summary Statistics Practice Problems

Midterm-Style Practice ProblemsThe problems below cover the three topics on the midterm.
1.
2.
3.
Data manipulation and summary statistics
Functions that process financial data
Simulations
If I succeed in using lockdown browser to administer the first midterm (which is the most likely
to happen in MFIN227001 and MFIN227002), then these questions are harder, on average, than
the questions that you can expect to see on the actual midterm. Regardless, they are good
practice.
import random
import copy
import math
import statistics
Data Manipulation and Summary Statistics
Question 1a. Create new list of lists lol3 which combines existing list of lists lol1 and lol2 so
that each ‘row’ of lol3 is of the form [‘date’,return from lol1,return from lol2], where the date is
a string with ‘-‘ as a delimiter and the returns are floats.
# Creates data and prints the two lists
random.seed(42)
dates = [“09-28-2023″,”09-29-2023″,”09-30-2023″,”09-31-2023”,\
“10-01-2023″,”10-05-2023″,”10-06-2023”]
lol1 = [[d.replace(‘-‘,’/’),random.uniform(-0.10,0.10)] \
for d in random.sample(dates,7)]
lol2 = [[d,str(random.uniform(-0.10,0.10))] for d in
random.sample(dates,7)]
for num in range(len(lol1)):
print(lol1[num],lol2[num])
# your code goes here
Question 1b. Calculate the mean and standard deviation of the eight returns from September
and store them as q1b_mean and q1b_stdev.
# Your code goes here
Question 2a. l2 is a list of lists that contains 10 rows. Each row is a list that includes the name of
a mutual fund and ten net returns. For each mutual fund, exactly one net return is missing
(coded as -99). Create a new list of lists q2a where each row is a list that includes the fund
name, the average return, and the standard deviation of returns, where both statistics are based
on the nine non-missing returns. q2a does not need to include a list of variable names.
# Creates `l2` for your use
random.seed(8675309)
l2 = [[‘Name’] + list(range(2013,2023,1))]
for num, letter in enumerate(‘abcdefghij’.upper()):
returns = [random.gauss((num+1)/100,(num+1)/50) for num in
range(10)]
returns[random.randint(0,9)] = -99
l2.append([‘Fund’+letter]+returns)
print(l2[0])
print(l2[1])
l3 = copy.deepcopy(l2)
# your code goes here
Question 2b. Using l2 again, create a new list of lists q2b where each row is a list containing the
fund name and the calendar year for which the return is missing. It is okay (and easiest) to write
code that assumes every fund will have exactly one missing return.
# Your code goes here
FUNCTIONS
Question 3. Write a function geometric(list_of_returns) that takes a list of (non-missing) returns
and returns the geometric average return:
r g eo m et r i c =( ( 1+r 1 ) ( 1+r 2 ) … ( 1+r N ) )
1/ N
−1
Notice that we are compounding all N returns and then taking the N t h root and subtracting
one.
Use your function to calculate and print the geometric average return for each fund in l3 (which
is a version of l2 that deletes the missing returns and omits the row of variable names).
l3 = []
for element in l2[1:]:
try:
element.remove(-99)
except:
pass
l3.append(element)
# your code goes here
Question 4. Define the function rank_sd(lol) which takes a list of lists as its input, where each
row contains the annual returns for a single investment, and ranks the investments from 1 to N
where the fund with rank 1 has the lowest standard deviation of annual returns and the fund
with rank N has the highest standard deviation of annual returns. The output should be a list of
lists where each “row” is a list containing the fund name, its rank based on the standard
deviation, and the standard deviation. Your function should allow for the possibility that some
investments have more annual returns than others. You should run your function on l4, which is
defined below.
# Creates return data for Fund1 – Fund7 for use in your function
random.seed(1776)
l4 = []
for num, name in enumerate(random.sample(range(1,8),7)):
returns = [random.gauss(0.10,0.02*(num+1)) for i in range(num+2)]
l4.append([‘Fund’+str(name)]+returns)
print(l4[0])
print(l4[1])
# Your code goes here
rank_sd(l4)
SIMULATIONS
Question 5. You are investing \$500,000 for one year. You have two options. First, you can
invest in an index fund that will charge you \$500 upfront (allowing you to invest \$499,500) and
earn an annual return that is normally distributed with mean of 10% and standard deviation of
20%. Second, you can pay a \$25,000 fee for a structured product (allowing you to invest \
$475,000) and earn an annual return that is normally distributed with mean of 10% and a
standard deviation of 20% except that any returns less than -5% are converted into -5%. (In
other words, you should simulate annual returns using a mean of 10% and standard deviation of
25% but replace any simulated returns less than -5% with -5%.) Assume that your utility
function is given by the square root of your portfolio value at the end of one year. Use a
simulation with 10,000 iterations for each option to determine which option generates the
higher average utility. Use an if-else statement to print the better portfolio for you based on a
comparison of the average utilities.
Finance question: In this problem, given this specific utility function, was it worth $24,500 to
purchase insurance against large negative returns?
# Your code goes here
Question 6: Use nine (closely related) simulations to estimate the average utility associated with
nine one-year investment opportunities. For each of three possible annual net returns, given by
list m, you should consider each of three possible standard deviations, given by list s. Each of the
nine simulations should be based on 5,000 iterations and should assume that annual returns are
normally distributed. As always, please replace any simulated return that is less than -100%
with -100%.
m = [0.06, 0.08, 0.10]
s = [0.10, 0.15, 0.20]
Finally, assume that the investor would invest \$250,000 today and that you can measure the
investor’s utility one year from today as the natural logarithm of (\$10,000 plus the value of their
portfolio one year from today).
Print the average utility associated with each investment opportunity on its own line, along with
the corresponding mean and standard deviation.
Finance question: How does average utility respond to an increase in the average return? How
does it respond to an increase in the standard deviation? Why?
# Your code goes here

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

Order a unique copy of this paper

600 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
Top Academic Writers Ready to Help
with Your Research Proposal

Order your essay today and save 25% with the discount code GREEN