Question: 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
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 resultsthis 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
