Download code

Jump to: navigation, search

Back to Monte_Carlo_simulation_(Matlab)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

input.nw

  1 <<test_simplest_montecarlo0.m>>=
  2 S0      = 100
  3 mu      = .05
  4 sigma   = .12
  5 T       =   5
  6 nb_traj = 100
  7 step    = 1/255
  8 
  9 [S, t]  = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step);
 10 
 11 figure('Color',[0.9412 0.9412 0.9412 ]);
 12 plot(t,S, 'linewidth',2);
 13 axis([t(1) t(end) min(S(:)) max(S(:))]);
 14 xlabel('time');ylabel('value');
 15 title(sprintf('%d trajectories', size(S,2)));
 16 
 17 @ text
 18 
 19 <<simplest_montecarlo0.m>>=
 20 function [S, t] = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step)
 21 % SIMPLEST_MONTECARLO - the simplest Monte Carlo simulation with Matlab
 22 % use:
 23 %  S = simplest_montecarlo( sigma, T, nb_traj, S0, mu, step)
 24 % example:
 25 %  [S, t] = simplest_montecarlo( .12, 5, 50, 100, .05, 1/255);
 26 
 27 <<simplest_monovariate_montecarlo>>
 28 @ text
 29 
 30 <<simplest_monovariate_montecarlo>>=
 31 nT = ceil(T/step);
 32 W  = sigma * sqrt(step) * cumsum(randn(nT, nb_traj));
 33 c  = repmat((mu - sigma^2/2) *step * (1:nT)',1,nb_traj);
 34 S  = [repmat(S0,1,nb_traj); S0 * exp( c + W)];
 35 if nargout > 1
 36    t = [0;step * (1:nT)'];
 37 end
 38 @ text
 39 
 40 <<simplest_montecarlo.m>>=
 41 function [S, t] = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step)
 42 % SIMPLEST_MONTECARLO - the simplest Monte Carlo simulation with Matlab
 43 % use:
 44 %  S = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step)
 45 % example:
 46 %  [S, t] = simplest_montecarlo( 'simplest', .12, 5, 50, 100, .05, 1/255);
 47 
 48 switch lower(mode)
 49     
 50     case 'test-simple'
 51 
 52         <<test_simplest_montecarlo>>
 53 
 54     case 'test-multi'
 55 
 56         <<test_simplest_multiple_montecarlo>>
 57         
 58     case {'simple', 'simplest'}
 59 
 60         <<simplest_monovariate_montecarlo>>
 61 
 62     case {'multi', 'multiple'}
 63         
 64         <<simplest_multivariate_montecarlo>>
 65 end
 66 @ text
 67 
 68 <<simplest_multivariate_montecarlo>>=
 69 nT  = ceil(T/step);
 70 [s,c] = cov2corr(sigma);
 71 s   = s * sqrt(step);
 72 sigma = corr2cov(s,c);
 73 Sd  = chol(sigma);
 74 dW  = randn(nT,length(S0)*nb_traj);
 75 Sd  = repmat({Sd},1,nb_traj);
 76 Sdb = blkdiag(Sd{:});
 77 dWc = dW * Sdb;
 78 c   = repmat(1+mu*step, nT, nb_traj);
 79 S   = [repmat(S0, 1, nb_traj); repmat(S0, nT, nb_traj) .* cumprod(c + dWc)];
 80 S   = reshape(S, [nT+1, size(S0,2), nb_traj]);
 81 if nargout > 1
 82    t = [0;step * (1:nT)'];
 83 end
 84 @ text
 85 
 86 <<test_simplest_motecarlo>>=
 87 mode    = 'simplest';
 88 S0      = 100;
 89 mu      = .05;
 90 sigma   = .12;
 91 T       = 5;
 92 nb_traj = 100;
 93 step    = 1/255;
 94         
 95 [S, t]  = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step);
 96 
 97 figure('Color',[0.9412 0.9412 0.9412 ]);
 98 plot(t,S, 'linewidth',2);
 99 axis([t(1) t(end) min(S(:)) max(S(:))]);
100 xlabel('time');ylabel('value');
101 title(sprintf('%d trajectories', size(S,2)));
102 @ text
103 
104 <<test_simplest_multiple_montecarlo>>=
105 mode    = 'multiple';
106 T       = 1/255*10;
107 nb_traj = 2;
108 step    = 1/255;
109 S0      = [100, 150];
110 s       = [.12 .18];
111 sigma   = corr2cov(s, [1 .3; .3 1]);
112 mu      = [0.05 0.04];
113 T       = 5;
114 nb_traj = 100;
115 step    = 1/255;
116         
117 [S, t]  = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step);
118 
119 figure('Color',[0.9412 0.9412 0.9412 ]);
120 subplot(2,1,1);
121 plot(t,reshape(S(:,1,:),size(S,1),size(S,3)), 'linewidth',2);
122 axis([t(1) t(end) min(S(:)) max(S(:))]);
123 xlabel('time');ylabel('value');
124 title(sprintf('%d trajectories - stock 1 (\\sigma=%5.2f%%)', size(S,3), s(1)*100));
125 subplot(2,1,2);
126 plot(t,reshape(S(:,2,:),size(S,1),size(S,3)), 'linewidth',2);
127 axis([t(1) t(end) min(S(:)) max(S(:))]);
128 xlabel('time');ylabel('value');
129 title(sprintf('%d trajectories - stock 2 (\\sigma=%5.2f%%)', size(S,3), s(2)*100));
130 @ text
131 


hijacker
hijacker
hijacker
hijacker

simplest_montecarlo0.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Monte_Carlo_simulation_(Matlab)?oldid=19332
14 
15 function [S, t] = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step)
16 % SIMPLEST_MONTECARLO - the simplest Monte Carlo simulation with Matlab
17 % use:
18 %  S = simplest_montecarlo( sigma, T, nb_traj, S0, mu, step)
19 % example:
20 %  [S, t] = simplest_montecarlo( .12, 5, 50, 100, .05, 1/255);
21 
22 nT = ceil(T/step);
23 W  = sigma * sqrt(step) * cumsum(randn(nT, nb_traj));
24 c  = repmat((mu - sigma^2/2) *step * (1:nT)',1,nb_traj);
25 S  = [repmat(S0,1,nb_traj); S0 * exp( c + W)];
26 if nargout > 1
27    t = [0;step * (1:nT)'];
28 end


hijacker
hijacker
hijacker
hijacker

test_simplest_motecarlo

mode    = 'simplest';
S0      = 100;
mu      = .05;
sigma   = .12;
T       = 5;
nb_traj = 100;
step    = 1/255;
        
[S, t]  = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step);

figure('Color',[0.9412 0.9412 0.9412 ]);
plot(t,S, 'linewidth',2);
axis([t(1) t(end) min(S(:)) max(S(:))]);
xlabel('time');ylabel('value');
title(sprintf('%d trajectories', size(S,2)));


simplest_montecarlo.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Monte_Carlo_simulation_(Matlab)?oldid=19332
14 
15 function [S, t] = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step)
16 % SIMPLEST_MONTECARLO - the simplest Monte Carlo simulation with Matlab
17 % use:
18 %  S = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step)
19 % example:
20 %  [S, t] = simplest_montecarlo( 'simplest', .12, 5, 50, 100, .05, 1/255);
21 
22 switch lower(mode)
23     
24     case 'test-simple'
25 
26         
27 
28     case 'test-multi'
29 
30         mode    = 'multiple';
31 	T       = 1/255*10;
32 	nb_traj = 2;
33 	step    = 1/255;
34 	S0      = [100, 150];
35 	s       = [.12 .18];
36 	sigma   = corr2cov(s, [1 .3; .3 1]);
37 	mu      = [0.05 0.04];
38 	T       = 5;
39 	nb_traj = 100;
40 	step    = 1/255;
41 	        
42 	[S, t]  = simplest_montecarlo( mode, sigma, T, nb_traj, S0, mu, step);
43 
44 	figure('Color',[0.9412 0.9412 0.9412 ]);
45 	subplot(2,1,1);
46 	plot(t,reshape(S(:,1,:),size(S,1),size(S,3)), 'linewidth',2);
47 	axis([t(1) t(end) min(S(:)) max(S(:))]);
48 	xlabel('time');ylabel('value');
49 	title(sprintf('%d trajectories - stock 1 (\\sigma=%5.2f%%)', size(S,3), s(1)*100));
50 	subplot(2,1,2);
51 	plot(t,reshape(S(:,2,:),size(S,1),size(S,3)), 'linewidth',2);
52 	axis([t(1) t(end) min(S(:)) max(S(:))]);
53 	xlabel('time');ylabel('value');
54 	title(sprintf('%d trajectories - stock 2 (\\sigma=%5.2f%%)', size(S,3), s(2)*100));
55         
56     case {'simple', 'simplest'}
57 
58         nT = ceil(T/step);
59 	W  = sigma * sqrt(step) * cumsum(randn(nT, nb_traj));
60 	c  = repmat((mu - sigma^2/2) *step * (1:nT)',1,nb_traj);
61 	S  = [repmat(S0,1,nb_traj); S0 * exp( c + W)];
62 	if nargout > 1
63 	   t = [0;step * (1:nT)'];
64 	end
65 
66     case {'multi', 'multiple'}
67         
68         nT  = ceil(T/step);
69 	[s,c] = cov2corr(sigma);
70 	s   = s * sqrt(step);
71 	sigma = corr2cov(s,c);
72 	Sd  = chol(sigma);
73 	dW  = randn(nT,length(S0)*nb_traj);
74 	Sd  = repmat({Sd},1,nb_traj);
75 	Sdb = blkdiag(Sd{:});
76 	dWc = dW * Sdb;
77 	c   = repmat(1+mu*step, nT, nb_traj);
78 	S   = [repmat(S0, 1, nb_traj); repmat(S0, nT, nb_traj) .* cumprod(c + dWc)];
79 	S   = reshape(S, [nT+1, size(S0,2), nb_traj]);
80 	if nargout > 1
81 	   t = [0;step * (1:nT)'];
82 	end
83 end


hijacker
hijacker
hijacker
hijacker

test_simplest_montecarlo0.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Monte_Carlo_simulation_(Matlab)?oldid=19332
14 
15 S0      = 100
16 mu      = .05
17 sigma   = .12
18 T       =   5
19 nb_traj = 100
20 step    = 1/255
21 
22 [S, t]  = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step);
23 
24 figure('Color',[0.9412 0.9412 0.9412 ]);
25 plot(t,S, 'linewidth',2);
26 axis([t(1) t(end) min(S(:)) max(S(:))]);
27 xlabel('time');ylabel('value');
28 title(sprintf('%d trajectories', size(S,2)));
29 


hijacker
hijacker
hijacker
hijacker

noweb.log

1 undefined chunk name: <<test_simplest_montecarlo>>