Computer Science Question

I have attached the demo exam and the solution below. Please review it and be prepared. The exam will start at 5:30 EST (Boston,USA) time and I will message you here 10 mins before the exam.

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

Midterm 1 (Demo)
If this were the real midterm, it would contain three questions
1.
2.
3.
Using list comprehension (or a for loop) to transform an existing list of lists
Creating a function that processes final data. For example, it could ask you to calculate a
present value, future value, NPV, compound return, average return, standard deviation,
etc.
Using a simple simulation to estimate the probability of a subset of possible outcomes
import math
import random
import statistics
def last(s):
return s[-1]
Example Question 1. Please use list comprehension or for loop to create a new list that
transforms the return for each mutual fund into its rank (where the highest return receives the
highest rank). Since the list of lists ‘mf’ reports returns for eight mutual funds, the ranks should
be 1, 2, 3, …, 8. The new list of list ‘mf2’ should be of the form [‘fund name’,rank]. It does not
need to be in the same order as ‘mf’.
Hint: You may find last() helpful when sorting ‘mf’.
random.seed(10)
mf = [[‘Fund’+letter,random.gauss(0.10,0.10)] for letter in
‘ABCDEFGH’]
for fund in mf:
print(fund)
# your code goes here
Example Question 2. Please define a function that calculates Sharpe ratios named sr(lol). In its
original form, the Sharpe ratio was defined as the (average return on an investment minus the
risk-free rate) divided by the standard deviation of the annual returns on the investment. Your
function should calculate this statistic for each of the stocks in a list of lists named ‘stocks’ under
the assumption that the risk-free rate is 3%. You will find it helpful to use statistics.mean() and
statistics.stdev().
‘Stocks’ contains five lists, each of which contains a stock ticker followed by annual returns over
the past five years. The output should be a new list of lists with five ‘rows’ where each ‘row’ is of
the form [ticker,Sharpe ratio].
random.seed(100)
stocks = []
for stock,mean in [(‘ABC’,0.08),(‘XYZ’,0.05),(‘QQQ’,0.12),(‘IBM’,0.2),
(‘SEI’,0.15)]:
stocks.append([stock] + [random.gauss(mean,0.10) for num in
range(5)])
for row in stocks:
print(row)
# your code goes here
Example Question 3. Assume that your portfolio consists of two stocks. Specifically, you
allocated 800,000 USD to Tesla and 1,200,000 USD to Apple. Assume that Tesla has an
expected annual return of 12% with a standard deviation of 20% and Apple has an expected
annual return of 9% with a standard deviation of 15%. Use a simulation with 10,000 iterations to
estimate the probability that your portfolio has an annual net return between 0% and 5% over
the next year.


Please replace any simulated returns for Tesla or Apple that are less than -100% with 100%.
Please use a print statement to report the result of your simulation, where the
probability of 10% is printed as 10.00% (i.e., with two decimal places).
# your code goes here
Midterm 1 (Demo)
If this were the real midterm, it would contain three questions
1.
2.
3.
Using list comprehension (or a for loop) to transform an existing list of lists
Creating a function that processes final data. For example, it could ask you to calculate a
present value, future value, NPV, compound return, average return, standard deviation,
etc.
Using a simple simulation to estimate the probability of a subset of possible outcomes
import math
import random
import statistics
def last(s):
return s[-1]
Example Question 1. Please use list comprehension or for loop to create a new list that
transforms the return for each mutual fund into its rank (where the highest return receives the
highest rank). Since the list of lists ‘mf’ reports returns for eight mutual funds, the ranks should
be 1, 2, 3, …, 8. The new list of list ‘mf2’ should be of the form [‘fund name’,rank]. It does not
need to be in the same order as ‘mf’.
random.seed(10)
mf = [[‘Fund’+letter,random.gauss(0.10,0.10)] for letter in
‘ABCDEFGH’]
for fund in mf:
print(fund)
[‘FundA’, 0.00462829919366628]
[‘FundB’, 0.05409058431613147]
[‘FundC’, 0.04007505277909144]
[‘FundD’, 0.06798575989435207]
[‘FundE’, 0.17217183074100623]
[‘FundF’, -0.0717264917767424]
[‘FundG’, 0.06631461682081363]
[‘FundH’, 0.05144251776160891]
mf2 = sorted(mf, key=last)
mf2
[[‘FundF’, -0.0717264917767424],
[‘FundA’, 0.00462829919366628],
[‘FundC’, 0.04007505277909144],
[‘FundH’, 0.05144251776160891],
[‘FundB’, 0.05409058431613147],
[‘FundG’, 0.06631461682081363],
[‘FundD’, 0.06798575989435207],
[‘FundE’, 0.17217183074100623]]
mf3 = [[mf2[num][0],num+1] for num in range(len(mf2))]
mf3
[[‘FundF’, 1],
[‘FundA’, 2],
[‘FundC’, 3],
[‘FundH’, 4],
[‘FundB’, 5],
[‘FundG’, 6],
[‘FundD’, 7],
[‘FundE’, 8]]
Example Question 2. Please define a function that calculates Sharpe ratios named sr(lol). In its
original form, the Sharpe ratio was defined as the (average return on an investment minus the
risk-free rate) divided by the standard deviation of the annual returns on the investment. Your
function should calculate this statistic for each of the stocks in a list of lists named ‘stocks’ under
the assumption that the risk-free rate is 3%. You will find it helpful to use statistics.mean() and
statistics.stdev().
‘Stocks’ contains five lists, each of which contains a stock ticker followed by annual returns over
the past five years. The output should be a new list of lists with five ‘rows’ where each ‘row’ is of
the form [ticker,Sharpe ratio].
random.seed(100)
stocks = []
for stock,mean in [(‘ABC’,0.08),(‘XYZ’,0.05),(‘QQQ’,0.12),(‘IBM’,0.2),
(‘SEI’,0.15)]:
stocks.append([stock] + [random.gauss(mean,0.10) for num in
range(5)])
for row in stocks:
print(row)
[‘ABC’, 0.14715533269422837, 0.1673319667879366, 0.1003616548392885, 0.0750349212724641, 0.067940872473325]
[‘XYZ’, -0.05592757448773025, 0.08814369665471086, 0.06734290437167734, 0.1466371824603381, 0.1032483431220606]
[‘QQQ’, 0.34204199055906503, 0.1889012838080295, 0.20543644913362236,
0.09049524849024004, 0.057664944559563634]
[‘IBM’, 0.35917369009274847, 0.21792047470562367, 0.2600863394252517,
0.23474319006562094, 0.2855371857345397]
[‘SEI’, -0.030291827295241186, -0.03331129482658354,
0.2109110868755436, 0.39250144955073907, -0.05223357550927624]
def sr(lol):
output = []
for row in lol:
ticker = row[0]
ret
= row[1:]
numer = statistics.mean(ret) – 0.03
denom = statistics.stdev(ret)
sr
= numer/denom
output.append([ticker,sr])
return output
sr(stocks)
[[‘ABC’, 0.5380373953538318],
[‘XYZ’, 0.13223870577963495],
[‘QQQ’, 1.3151293986094565],
[‘IBM’, 4.366329920174881],
[‘SEI’, 0.34215015090816164]]
Example Question 3. Assume that your portfolio consists of two stocks. Specifically, you
allocated 800,000 USD to Tesla and 1,200,000 USD to Apple. Assume that Tesla has an
expected annual return of 12% with a standard deviation of 20% and Apple has an expected
annual return of 9% with a standard deviation of 15%. Use a simulation with 10,000 iterations to
estimate the probability that your portfolio has an annual net return between 0% and 5% over
the next year.


Please replace any simulated returns for Tesla or Apple that are less than -100% with 100%.
Please use a print statement to report the result of your simulation, where the
probability of 10% is printed as 10.00% (i.e., with two decimal places).
net_ret = []
for num in range(10000):
tsla = 800000*(1+max(random.gauss(0.12,0.20),-1))
aapl = 1200000*(1+max(random.gauss(0.09,0.15),-1))
net_ret.append((tsla+aapl)/2000000 – 1)
min(net_ret), statistics.mean(net_ret), max(net_ret)
(-0.33376004044462415, 0.10275619593012913, 0.5725308582678093)
prob = [1 if num>=0.00 and num

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