Microsoft Word – _SMM283 CW_ A_Euler
1 | P a g e
Introduction to Python
SMM283
Lecturer: Dr. Adrian Euler
GENERAL INSTRUCTIONS TO STUDENTS
– Students should provide the Python solution in the case study.
– Students may use only the Python IDLE (i.e. Python 2.7 or above) on a PC.
– Students should upload the following on Moodle by the deadline:
• Python file (*.py) – one file only.
• Excel file (*.xls) – one file only
SPECIAL INSTRUCTIONS TO STUDENTS
– Students should save Python program file, excel file, and the word document
in a StudentID named folder, for upload to Moodle
– Students are expected to create structural programs only, without any Python
graphics, object oriented or GUI features.
2 | P a g e
CASE STUDY
You have just received a positive response from Merrill Lynch on a recent graduate ‘Financial
Analyst’ role you had applied for and you are invited to a job interview. The job specification
includes, among other things, knowledge of Python at introductory level, but with the prospect of
been further trained within the organisation. If successful you will work in a recently re-organised
asset pricing department and if you were to be successful, you’d work as part of a team dealing
with financial analysis and asset pricing that also involves computational work. Your prospective
employer has prepared a trial test for you as part of the interview, entirely based on introductory
Python programming.
You’d be expected to be tested on programming skills in Python, where a specific task is given
that must be coded within a limited amount of time to a problem specification. You are expected
to code the solution with user-defined functions, but not with graphics, object orientation, or
graphical user interface(GUI). Make use of good Python-style comments, variable declaration,
initialization, data processing, control flow statements, function cohesion, and coupling. You are
also expected to demonstrate that you understand both the syntax, function design, and have
good and timely programming skills in Python.
PROBLEM SPECIFICATION
Consider a 2-fund separation problem. Write a Python program that allows the user to choose
how to enter the (i) expected rate of return, (ii) volatility for each of the two funds, (ii) the
correlation coefficient, (iv) the risk aversion coefficient, and (v) a risk free rate of return; 1. read
the data from an Excel file, (2), enter them through program prompts i.e. use input(..) function
to read from the keyboard.
i.e. ~~~~DATA ENTER CHOICES~~~~~
1. Read from keyboard
2. Read from Excel file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the 2-fund portfolio and for the purpose of computing the return and risk of the portfolio, you
would need the capital weights. The capital weight w1 is set initially to 0.00. The second weight
is always w2 = 1-w1. It should use a loop to increment w1 by a small value (i.e. 0.0001) and
compute w2 such that the sum of capital weights is always 1.00. The maximum value of w1 is
1.0. The values should be stored in one-dimensional arrays or lists w1 = [], w2 = [].
i.e. w1 =[] w2 = []
0.0000 1.0000
0.0001 0.9999
0.0002 0.9998
………. ……….
0.5000 0.5000
………. ……….
1.0000 0.0000
The program should compute the expected return, volatility, quadratic utility, and sharp ratio of
the 2-fund portfolio using the following corresponding expressions:
3 | P a g e
Portfolio expected rate of return: 𝐸(𝑟𝑝) = 𝑤1𝐸(𝑟1) + 𝑤2𝐸(𝑟2)
Portfolio volatility:
𝜎𝑝 = √𝑤2𝜎2 + 𝑤2𝜎2 + 2𝑤1𝑤2𝜎1𝜎2𝜌1,2
1 1 2 2
Portfolio quadratic utility in expectation: 𝐸(𝑢𝑝) = 𝐸(𝑟𝑝) − 1 𝐴𝜎2
2 𝑃
(*) A is the risk aversion coefficient
Sharp Ratio:
𝐸(𝑟𝑝) − 𝐸(𝑟𝑓)
𝑆𝑃 =
𝜎𝑝
For each weight set, program should compute portfolio expected rate of return, volatility,
quadratic utility, and Sharp ratio. The computed portfolio returns and volatilities should be stored
in arrays rp = [], and vp = [], respectively. Program should then find the portfolio with the
highest utility and highest sharp ratio.
Update the program so that user has control over how the capital weights are generated,
prompting use to choose whether to compute capital weights using the Huang and Litzenberger
(HL) method, whereby w = g + rp * h. The g and h vector values can be generated by
applying HL through the following formulae:
𝑔1 =
𝐸(𝑟2)
𝐸(𝑟2) − 𝐸(𝑟1)
, 𝑎𝑛𝑑 𝑔2 = 1 − 𝑔1
ℎ1 =
1
𝐸(𝑟1) − 𝐸(𝑟2)
, 𝑎𝑛𝑑 ℎ2 = −ℎ1
Capital weights can be generated using w1 = g1 + rp * h1 and w2 = g2 + rp * h2, where
rp values are those computed earlier and stored in the array rp. Pogram then should compute
the portfolio standard deviation using the formula given above. Store them in the array efvp =
[]. Note that according to HL theory, these are mean-variance efficient frontier portfolios.
The program should print on the screen and Excel, the capital weight values within 4 decimal
points and in a field with a reasonable character width, as well as the portfolio expected return,
volatility, utility, and sharp ratio within a reasonable character width and 4 decimal places. The
values should be sorted from the lowest portfolio return to the highest. It should print out the
maximum utility and sharp ratio, clearly indicating the corresponding portfolio with its return and
volatility attributes. The input must be robustly validated and the output should be properly
formatted.
You may help to first try to code at the program level or inside a user defined main function and
then delegate some code to dedicated user-defined functions of your choice, but not less than 4
user defined functions.
You have flexibility on how many user-defined functions you may create and their specification
(not less than four). They must be designed using good cohesion and coupling programming
principles. The finished product must contain at least four well designed user-defined functions.
This is the only program file you are expected to submit.
You must produce a running program saved in a Python file named “studentID.py” and an output
Excel file “StudentIDPortfolioProblem.xls”. You may manually plot the portfolio mean-variance
efficient frontier, CML, and identify the market portfolio, based on the program output data in the
pc
高亮
pc
高亮
pc
高亮
pc
高亮
pc
高亮
pc
高亮
pc
高亮
4 | P a g e
excel file. This would allow you to check with literature that the shape of the graph is as described
in literature. You may wish to note the maximum utility and sharp ratio in the graph. You do not
need to produce any written work on the theorized financial portfolio elements. Focus is on the
programming skills.
The program must run in order to be marked.
(100 marks)