MATLAB The aim of this assignment is to write a function to generate stock prices over time, based on therandom walk algorithm, which is
.Stock (t+1) = Stock(t) + Delta, where Delta= (-1 or +1),and is given by a random draw.Consider the following code and output(check out what the function diff()does in MATLAB, and see how that is shown on the second graph). Also, subplot()allows you to create a number of graphs, and hold on retains previous graph output so that two variables can be shown on the graph.
clear;
rng(100);
Trials = 365;
Starting = 100;
Max=2000;
Min=30;
Min_Change = 1;
Max_Change = 10;
p1 = stock(Trials,Starting,Max,Min,Min_Change,Max_Change);
subplot(2,1,1);
plot(p1);
hold on;
subplot(2,1,2);
plot(diff(p1));
hold on;
p2 = stock(Trials,Starting,Max,Min,Min_Change,Max_Change);
subplot(2,1,1);
plot(p2);
hold on;
subplot(2,1,2);
plot(diff(p2));
Write the function (m file) stock, that takes in the following parameters.
The number of trials (e.g. number of successive days for stock price)
•The starting stock price•
The maximum stock price (if a change forces the stock above the max then the stock price should not change)
•The minimum stock price (if a change forces the stock below the min then the stock price should not change)
•The minimum change between two days (min should be 1)
•The maximum change between two days
To test your solution, the following values of p1 and p2 should be generated (first 20 shown here).Note that both stock prices take the same parameters, start at the same point, yet generate very different results–this is due to the random number generation.
>> p1(1:20)'ans =100 103 94 92 101 95 98 96 86 88 91 81 85 81 78 79 86 82 73 74>> p2(1:20)'ans =100 108 118 111 103 98 103 109 113 117 121 116 119 121 122 116 112 122 130 134
usage of the function diff() in MATLAB
The diff function gives the difference between consecutive values for a vector. That is if a vector V of size n is passed in the function as diff(V), it gives another vector size (n-1) with values as [V(2) - V(1), V(3) - V(2), ..... , V(n) - V(n-1)].
For the MATLAB function stock, all the explanation is in the code comments. Hope this helps!
Code:
% given unedited code ***************************************
clear;
rng(100);
Trials = 365;
Starting = 100;
Max=2000;
Min=30;
Min_Change = 1;
Max_Change = 10;
p1 = stock(Trials,Starting,Max,Min,Min_Change,Max_Change);
subplot(2,1,1);
plot(p1);
hold on;
subplot(2,1,2);
plot(diff(p1));
hold on;
p2 = stock(Trials,Starting,Max,Min,Min_Change,Max_Change);
subplot(2,1,1);
plot(p2);
hold on;
subplot(2,1,2);
plot(diff(p2));
% *********************************************************
% required function
function p = stock(Trials,Starting,Max,Min,Min_Change,Max_Change)
% create an array of size Trials + 1
% 1 extra for initial price
p = zeros(1, Trials + 1);
p(1) = Starting;
% loop for number of Trials
for i=1:Trials
% generate a random number between Min_Change and Max_Change
change = mod(rand(), (Max_Change - Min_Change + 1)) + Min_Change;
% generate sign for change ( addition / subtraction)
if(rand() <= 0.5)
sign = -1;
else
sign = 1;
end
% now get the supposed to be price for next day
sprice = p(i) + change*sign;
% check if sprice is within range
% do not change if not within range
if(sprice >= Min && sprice <= Max)
p(i+1) = sprice;
else
p(i+1) = p(i);
end
end
end
Sample run:
Graph
Values of p1 and p2
Code screenshots:
MATLAB The aim of this assignment is to write a function to generate stock prices over...