Workshop
SMM283-INTRODUCTION TO PYTHON Dr. A. Euler
1 | P a g e
WEEK 3 & 4
[FORMATIVE HOME WORK]
TASK SET 1
(a)
Write a user-defined function with absolute(num) as a header. It should receive any
signed numbers(positive of negative) and should return the absolute value of that number.
(b)
Write a user-defined function with the mean(x=[], l)as its header. It should
receive an array of real numbers and the length of the array , computes the
average and return it.
TASK SET 2
(a)
Write a user-defined function with the var(x = [], l) as its header. It should
receive an array of real numbers and the length of the array. Computes the average and
returns it. Experiment by combining the var function coded in this exercise with
function mean of question 3 in one program.
(b)
Write a user-defined function w(x = [], l) that receives an array of real numbers
(i.e. probabilities or weights), checks weather they all add up to 100% and returns one
or zero.
In portfolio theory this is a basic capital spread condition and in that context it would
n
check that all weights add to 1. The mathematical formula is
n
wi 1
i1
for weights or
p i 1
i1
for probabilities.
TASK SET 3 (*)
SMM283-INTRODUCTION TO PYTHON Dr. A. Euler
2 | P a g e
n
(a)
Write a user-defined function sumproduct(a=[], n, b = [], m) that receives
two arrays of real numbers and the lengths of the arrays . It should compute the
cumulative effect of the product between the numbers.
In portfolio theory this function, when receiving an array of capital weights and an
array of expected asset returns, would compute the portfolio expected return in
accordance with the formula
Ep wi Ei .
i1
As a challenge extend its use to compute the portfolio variance based on the formula
var
j1
n
wj wi
i1
i,j .
You could do this through an additional function i.e. mmult(wt, mmult(v,
w)).
(b)
Re-write the Black-Scholes option pricing program so that the bscall and bsput are
combined in one function bsoption (iopt, s, k, r, q, sig, tau). Further on
the combine the d1(..) and d2(..) functions in one function d(iopt, ….).
Could the function n(..) be re-written in a more compact function? Give it a try. The
iopt variable takes the values +1 and -1, thus make sure the integer input is validated.
n