Appendix B: Software
275
xlabel(’x_f’); ylabel(’Pricing kernel p_{BS}’)
legend(’uniform MC’,’analytical’); set(gca,’FontSize’,16)
In order to evaluate p BS with the Metropolis-Hasting algorithm generating the paths,
we only have to replace the section where the paths are defined. The following code
snippet first defines the number of sample paths to prepare and defines the anonymous
function h() from which the random numbers will be drawn. Then it defines the
starting point x0 and the β for the metropolis algorithm, runs the algorithm for 1000
burn-in iterations, and finally generates the required number of random numbers to
define the paths. Since the random numbers fill a one-dimensional array, we have to
reshape() the output in such a way that each column defines one path.
%....define the paths
Npath=100000;
% number of sample paths
h=@(x)exp(-x.ˆ2/(2*(2*sigma)ˆ2));
% for Metropolis-Hastings
x0=0.01; beta=3*sqrt(sigmaˆ2*dt);
y=metropolis3(h,beta,1000,x0);
% 1000 iteration burn-in
x=metropolis3(h,beta,Npath*N,y(1000));
x=reshape(x,[N,Npath]); % x=permute(x,[2,1]);
%....
Likewise, we can replace section, where the paths are generated by the following
code snippet, which prepares paths that resemble a random walk with rms step size
2σ/
√
N and then uses the built-in cumsum() function to cumulatively add up the
individual steps in order to obtain the meandering paths that are later used to evaluate
the path integral. As already pointed out in the main text, is the step size artificially
increased in order to create paths that cover a wider range and thereby more accurately
determine the tails of the pricing kernel. Adding paths that probe far-away regions
does not affect the accuracy negatively, because their contribution to the path integral
is exponentially suppressed by the much increased action S BS on these paths.
%....define the paths
Npath=10000;
% sample paths
x=2*randn(N,Npath)*sigma/sqrt(N);
x=cumsum(x,1);
%....
Again, we encourage the reader to explore the scripts and vary parameters.
B.7 Macroeconomic Models
The following script solow.m is used to prepare Fig. 11.1. After defining the parameters of the model and allocating space for the variables, we initialize the variables.
275
xlabel(’x_f’); ylabel(’Pricing kernel p_{BS}’)
legend(’uniform MC’,’analytical’); set(gca,’FontSize’,16)
In order to evaluate p BS with the Metropolis-Hasting algorithm generating the paths,
we only have to replace the section where the paths are defined. The following code
snippet first defines the number of sample paths to prepare and defines the anonymous
function h() from which the random numbers will be drawn. Then it defines the
starting point x0 and the β for the metropolis algorithm, runs the algorithm for 1000
burn-in iterations, and finally generates the required number of random numbers to
define the paths. Since the random numbers fill a one-dimensional array, we have to
reshape() the output in such a way that each column defines one path.
%....define the paths
Npath=100000;
% number of sample paths
h=@(x)exp(-x.ˆ2/(2*(2*sigma)ˆ2));
% for Metropolis-Hastings
x0=0.01; beta=3*sqrt(sigmaˆ2*dt);
y=metropolis3(h,beta,1000,x0);
% 1000 iteration burn-in
x=metropolis3(h,beta,Npath*N,y(1000));
x=reshape(x,[N,Npath]); % x=permute(x,[2,1]);
%....
Likewise, we can replace section, where the paths are generated by the following
code snippet, which prepares paths that resemble a random walk with rms step size
2σ/
√
N and then uses the built-in cumsum() function to cumulatively add up the
individual steps in order to obtain the meandering paths that are later used to evaluate
the path integral. As already pointed out in the main text, is the step size artificially
increased in order to create paths that cover a wider range and thereby more accurately
determine the tails of the pricing kernel. Adding paths that probe far-away regions
does not affect the accuracy negatively, because their contribution to the path integral
is exponentially suppressed by the much increased action S BS on these paths.
%....define the paths
Npath=10000;
% sample paths
x=2*randn(N,Npath)*sigma/sqrt(N);
x=cumsum(x,1);
%....
Again, we encourage the reader to explore the scripts and vary parameters.
B.7 Macroeconomic Models
The following script solow.m is used to prepare Fig. 11.1. After defining the parameters of the model and allocating space for the variables, we initialize the variables.
