Download FIN 285a: Computer Simulations and Risk Assessment

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Karhunen–Loève theorem wikipedia , lookup

Law of large numbers wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Classical Hamiltonian quaternions wikipedia , lookup

Vector space wikipedia , lookup

Bra–ket notation wikipedia , lookup

Matrix calculus wikipedia , lookup

Cartesian tensor wikipedia , lookup

Basis (linear algebra) wikipedia , lookup

Transcript
FIN 285a: Computer Simulations and Risk Assessment
Fall 2004
Matlab review class
TA: Vitaliy Vandrovych
[email protected]
In order to start Matlab at the cluster’s computer you need to go to Start, then Programs, then
Applications, and finally Matlab 7.0.
In order to use some of the Blake’s functions, open them at the following link:
http://people.brandeis.edu/~blebaron/classes/fin285a/software/finboot/
Copy it and paste to the Matlab editor, and save as m-file in the work directory.
Suggested problems
1. Create the column vector a composed of numbers from 25 to -25 separated by 3 (25, 22,19, …).
What is the cumulative sum of the vector a? Define it as vector b. Find element-by-element
product of vectors a and b. Find the sum of the elements of resulting vector.
Hint: Use cumsum command.
a = (25:-3:-25)'
b = cumsum(a)
c = a.*b
d = sum(c)
Result: d = 1989
2. Calculate expected value and variance for a die (a small cube with the numbers 1 to 6 marked in
dots on the sides, used in gambling and in a wide variety of games of chance). Follow formulas
given in class.
die = 1:6
p = [1/6 1/6 1/6 1/6 1/6 1/6];
expdie = 1/6*sum(die)
% Other variant for calculating expected value
expdie = die*p'
Result: expdie = 3.5000
vardie = 1/6*sum((die - expdie).^2)
% Other variant for calculating variance
vardie1 = var(die,1) % read help var
Result: vardie = 2.9167
3.
You have the following vector: [1.3, -.7, 99, 5.7, -5, 3, 2.28, 14]. Find the index numbers for all
elements of this vector whose absolute value is greater or equal than 5. Extract these elements into
separate vector a and remaining elements into vector b. Find new vector c whose elements are
equal to the elements of vector a each raised to the power equal to the corresponding element of
vector b.
Hint: make use of commands find, abs.
h = [1.3, -.7, 99, 5.7, -5, 3, 2.28, 14]
k = find(abs(h)>= 5)
a = h(k)
b = h(find(abs(h)<5))
c = a.^b
4.
Company stock follows the following quarterly return distribution:
r -return
-0.1
0.1
0.2
0.25
0.3
p - probability 0.1
0.15
0.25
0.45
0.05
The annual returns are the compounded quarterly returns (annual return is equal to the product of
(1+r) for each quarter minus one). Draw 10000 4-element samples of quarterly returns from this
distribution; in each case calculate annual return, and then calculate mean annual return and
standard deviation of the annual returns.
Plot a histogram of the resulting annual returns (use 25 bins). Title it ‘Histogram for simulated
annual returns’.
Hint: Use prod, “for” loop, Blake’s sample and histogram functions.
r = [-0.1 0.1 0.2 0.25 0.3];
p = [0.1 0.15 0.25 0.45 0.05];
for i = 1:10000
z = sample(r,4, p);
annual_ret(i) = prod(1+z)-1;
end
mean_return = mean(annual_ret)
standdev_of_ret =std(annual_ret)
histogram(annual_ret,25)
title('Histogram for simulated annual returns')
xlabel('mean annual return')
ylabel('frequency')
5. Generate 3 by 3 matrix A of uniformly distributed random numbers. Extract the matrix whose
elements are the same as in A if they are less or equal to 0.5, or zero otherwise.
Hint: for the second part use the logical operator.
A = rand(3,3)
L = A<=0.5
B = A.*L
2
6. Create the function that takes length of the sides of rectangular as the inputs and returns its square
and perimeter as outputs. Calculate the square and the perimeter of the rectangular with sides 7
and 5.
function [Sq,Per] = rectangular(a,b)
% a and b are the length of the sides of rectangular
% Sq is the measure of the square of rectangular and Per is the perimeter of
rectangular
Sq = a*b;
Per = 2*a +2*b;
3