聯系方式

您當前位置:首頁 >> Python編程Python編程

日期:2019-06-17 10:58

Introduction to Python

SMM283

GENERAL INSTRUCTIONS TO STUDENTS

- Students should provide Python solutions to both part I & II 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) files from both part I & II, and

Excel file created by the program in part I.

SPECIAL INSTRUCTIONS TO STUDENTS

- Students should save all files in a student_id named folder, for upload to

Moodle after you complete your activity of debugging and upgrading the

program.

- Students are expected to create structural programs only, without any Python

graphics, object oriented or GUI features.

2

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 two-part trial test for you

as part of the interview, entirely based on introductory Python programming.

In the first part, you are served a Python program riddled with programming “bugs” and

code cohesion issues. You are given a limited time to fix it, but also perform an update to

it based on an additional programming specification, thus demonstrate that you

understand the syntax of Python and truly have good and timely debugging skills.

In the second part, you’d be expected to be tested on procedural 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 entirely with procedural

programming concepts, without involving user-defined functions, graphics, object

orientation, or graphical user interface(GUI). Make use of good Python-style comments,

variable declaration, initialization, data processing, and control flow statements.

PART I

Debugging and update a Python program. Create a folder “cw_student_id”, where your

actual student id should be part of your folder name. You should then create a file

“Part1V1.py” file using the Python IDLE (Python 2.7 or above), where the program given

is entirely re-created and successfully repaired of its “bugs”. You should also update it so

that the code is more compact and alternative features are used; for the same task and

output results, use less loops, use alternative loops, replace the max() function with an ifelse

statement , use alternative formatting capabilities, and remove any unneeded

comments or code; you must create a second file “Part1V2.py”, The programs must run

in order to be marked. The output of the corrected programs are the same and shown at

the end of this question.A C program that computes the payoff of

1. a long put with exercise k1, ~~~~~~~~~~~~

~~~~~~~~~~2. a short call with exercise k2, where k2 > k1~~~~~~~~~

~~~~~~~~~~3. An option spread based on (1), (2) where k1 < k2~~~~~~~~~~

3

~~~~~~~~~~The premiums are ignored.~~~~~~~~~~~~~~

******************************************************************************

~~~~~~~~~Author: A Euler ~~~~~~~~~~

~~~~~~~~~School: Cass Business School~~~~~~~~~~~

~~~~~~~~~Module: Introduction to Python~~~~~~~~~~~

'''

import sys

import math

import string

SIZE = 17

f = open(optionSpread.xls, 'w'); # open (create) the file optionSpread.xls with

write permission # generate the asset spot prices from 0.0 t0 160.00*/

while (i < SIZE)

stockprice..append(spot) # use append to add elements on the array

#print("|%12.2lf|" % (spot))

spot +=== 10.0

i += 1'''

print("%d"%i)

for i in range(SIZE):

print('|%11.2lf|' % (stockprice[i]))

print("%d\n\n"%i)

'''

#long put with exercise k1:

# payout =MAX(k1-stockprice, 0)+stockprice - stockprice = max(k1, stockprice)-stockprice

for i range(SIZE):

4

payout == max(0, k1-stockprice[i])

longputk1,Append(payout)

#print("|%12.2lf|" % (longputk1[i]))

#print("%s"% "\n\n\n")

# short calls with exercise k2:

# payout = -MAX(stockprice-k2, 0)

# = MIN(k2-stockprice,0)+stockprice - stockprice= MIN(k2,stockprice) - \

stockprice

for i in range(SIZE):

payout = - max(stockprice[i] - k2, 0)

shortcallk2.appent[payout)

#print("|%12.2lf|" % (shortcallk2[i]))

# Spread payoff (premiums are ignored):

# pyaout = MAX(k-s1, 0) - MAX(s-k2, 0)

for i in range(SIZE):

payout = longputk1(i] + Shortcallk2[i]

spreadpo.append(payout)

#print("|%12.2lf|" % (spreadpo[i]))

#----------------------------------------------------------------------------------------------

str1 = 'No#'

str2 == 'Stock Price'

str3 = 'Long Put(k1)'

str4 = 'Short Call(k2)

str5 = 'Spread payout'

#----------------------------------------------------------------------------------------------

# print formatted data in the monitor

print("%14s\t%14s\t%14s\t%14s\t%14s" % (str1, str2, str3, str4, str5))

print(' ------------------------------------------------------------------------------ ')

for i in range(SIZE):

print("%14d%14.2lf%14.2lf%14.2lf%14.2lf" % (i+1; stockprice[i], \

longputk1[i], shortcallk2[i], spreadpo[i]))

# print formatted data in the the file

f.write("%s\t%s\t%1s\t%s\t%s\n" % (str1, str2, str3, str4, str5)]

print(' ------------------------------------------------------------------------------ ')

for i IN range(SIZE):

f..write(str(i+1) + 't' + str(stockprice[i]) + 't'+ str(longputk1[i]) \

+'\t'+ str(shortcallk2[i])+ '\t'+ str(spreadpo[i])

+'\n')

#----------------------------------------------------------------------------------------------

f.close() # closes the file

#------------------------------------------------------------------------

The output of the program without bugs:

5

The “optionSpread.xls” file created and data printed as follows:

Once the optionSpread.xls file is created by the program and data printed on it, you can,

then manually plot the option spread payoff against the spot price and the result should be as

follows:

6

(50 marks)

PART II

Consider a two-fund separation problem. Write a procedural Python program (all

coded at the program level or inside a user-defined main function) that prompts

the user to enter (1) expected rates of return, (2) volatilities for the two funds, (3)

the correlation coefficient of the funds, (4) the risk aversion coefficient. The input

must be validated in the correct range and type with exception handling in order to

have a robust input handling Python program.

In the two-fund portfolio and for the purpose of computing the return and risk of the

portfolio you would need to establish the capital weight of the first fund and set the

weight of the other fund as one minus the weight of the first. To that end your

Python program should generates 100 random values, representing stochastic

choices for the capital allocation problem. The random weights are then stored in

a one-dimensional array or list called w1 = [], where the values of w2 = [] should

be calculated and checked such that the total of each weigh- pair is constrained to

1.00.

The program should compute the expected return, volatility, and quadratic utility of

the two-fund portfolio using the following corresponding expressions:

7

For each of the 100 weight sets generated previously and in a random manner of

portfolios formed of these two investments, the program should compute the

portfolio expected rate of return, volatility and utility 100 times (for each of the

random weight sets). The program should then find the portfolio with the highest

utility (maximum value).

The program should print out the random capital weight values within 2 decimal

points and in a field with width 10, as well as the portfolio expected return, volatility

and utility with a width of 5 and 2 decimal places. Further on it should print out the

maximum utility, clearly indicating the corresponding portfolio with its return and

volatility attributes.

Note that this should all be coded at the program level or inside a user defined

main function and you are not expected to delegate any of the code to other

dedicated user-defined functions. The input must be robustly validated and the

output should be properly formatted.

You must produce a running program saved in a Python file named “Part2.py”. The

program must run in order to be marked.

(50 marks)


版權所有:編程輔導網 2018 All Rights Reserved 聯系方式:QQ:99515681 電子信箱:[email protected]
免責聲明:本站部分內容從網絡整理而來,只供參考!如有版權問題可聯系本站刪除。

25选5一等奖多少钱