在Matlab中绘制置信区间
我无法通过Matlab中的函数绘制置信区间。我在下面编写了以下代码在Matlab中绘制置信区间,matlab,plot,intervals,Matlab,Plot,Intervals,我无法通过Matlab中的函数绘制置信区间。我在下面编写了以下代码 clear all; close all; %CH15 Program for Chapter 15 % % Monte Carlo for a European call randn('state',100) %%%%%%%%%%%%%%%%% Problem and method parameters %%%%%%%%%%%%%%% S = 10; E = 9; sigma = 0.1; r = 0.06;
clear all;
close all;
%CH15 Program for Chapter 15
%
% Monte Carlo for a European call
randn('state',100)
%%%%%%%%%%%%%%%%% Problem and method parameters %%%%%%%%%%%%%%%
S = 10; E = 9; sigma = 0.1; r = 0.06; T = 1;
Dt = 1e-3; N = T/Dt; M = 1e4;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on;
for M = [2^5,2^6,2^7,2^8,2^9,2^10,2^11,2^12,2^13,2^14,2^15,2^16,2^17]
V = zeros(M,1);
for i = 1:M
Sfinal = S*exp((r-0.5*sigma^2)*T+sigma*sqrt(T)*randn);
V(i) = exp(-r*T)*max(Sfinal-E,0);
end
aM = mean(V); bM = std(V);
conf = [aM - 1.96*bM/sqrt(M), aM + 1.96*bM/sqrt(M)]
%xlabel('Samples') % x-axis label
title('Monte Carlo Approximations')
ylabel('Option value approximation') % y-axis label
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
yticks([10^0.1 10^0.2 10^0.3])
axis([10^1 10^6 10^0.1 10^0.3])
yticklabels({'10^{0.1}','10^{0.2}','10^{0.3}'})
plot(M,aM,'x')
plot(M,ch08(10,9,0.06,0.1,1),'--.k')
err = ones*(size(conf));
errorbar(aM,conf(1),conf(2))
end
为了匹配下面显示的图片(出于某种原因,绘图(M,ch08(10,9,0.06,0.1,1),'--')
不显示任何内容,但我忽略了这个外观问题)
在上面的Matlab代码中,置信区间是通过
conf = [aM - 1.96*bM/sqrt(M), aM + 1.96*bM/sqrt(M)]
我当前的实现几乎与上面的图片相匹配
我不知道如何在Matlab中绘制置信区间。我查看了Google,发现推荐的方法是通过errorbar函数
我想我可以补充一下
conf = [aM - 1.96*bM/sqrt(M), aM + 1.96*bM/sqrt(M)]
在errorbar函数内,绘制第一张图片内显示的垂直置信区间线。这是可以通过调整来实现的吗
errorbar(aM,conf(1),conf(2))
以某种方式跟踪conf=[aM-1.96*bM/sqrt(M),aM+1.96*bM/sqrt(M)]
的变化
我还在Matlab代码中引用第二个脚本
function [C, Cdelta, P, Pdelta] = ch08(S,E,r,sigma,tau)
% Program for Chapter 8
% This is a MATLAB function
%
% Input arguments: S = asset price at time t
% E = Exercise price
% r = interest rate
% sigma = volatility
% tau = time to expiry (T-t)
%
% Output arguments: C = call value, Cdelta = delta value of call
% P = Put value, Pdelta = delta value of put
%
% function [C, Cdelta, P, Pdelta] = ch08(S,E,r,sigma,tau)
if tau > 0
d1 = (log(S/E) + (r + 0.5*sigma^2)*(tau))/(sigma*sqrt(tau));
d2 = d1 - sigma*sqrt(tau);
N1 = 0.5*(1+erf(d1/sqrt(2)));
N2 = 0.5*(1+erf(d2/sqrt(2)));
C = S*N1-E*exp(-r*(tau))*N2;
Cdelta = N1;
P = C + E*exp(-r*tau) - S;
Pdelta = Cdelta - 1;
else
C = max(S-E,0);
Cdelta = 0.5*(sign(S-E) + 1);
P = max(E-S,0);
Pdelta = Cdelta - 1;
end
一种解决方法是通过添加
line([M M], conf);
它绘制了一条直线
clear all;
close all;
%CH15 Program for Chapter 15
%
% Monte Carlo for a European call
randn('state',100)
%%%%%%%%%%%%%%%%% Problem and method parameters %%%%%%%%%%%%%%%
S = 10; E = 9; sigma = 0.1; r = 0.06; T = 1;
Dt = 1e-3; N = T/Dt; M = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on;
for M = [2^5,2^6,2^7,2^8,2^9,2^10,2^11,2^12,2^13,2^14,2^15,2^16,2^17]
V = zeros(M,1);
for i = 1:M
Sfinal = S*exp((r-0.5*sigma^2)*T+sigma*sqrt(T)*randn);
V(i) = exp(-r*T)*max(Sfinal-E,0);
end
aM = mean(V); bM = std(V);
conf = [aM - 1.96*bM/sqrt(M), aM + 1.96*bM/sqrt(M)]
title('Monte Carlo Approximations')
xlabel('Num samples') % x-axis label
ylabel('Option value approximation') % y-axis label
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
yticks([10^0.1 10^0.2 10^0.3])
axis([10^1 10^6 10^0.1 10^0.3])
yticklabels({'10^{0.1}','10^{0.2}','10^{0.3}'})
plot(M,aM,'x')
plot(M,ch08(10,9,0.06,0.1,1),'--.k')
line([M M], conf);
end
Matlab中似乎有一个bug,用于测试
plot(M,ch08(10,9,0.06,0.1,1),'--')
不绘制任何数据。- 通常用于此目的,但如您所知,(或)也可用于绘制置信区间线。我将重点介绍如何使用
errorbar
不绘制虚线,因为plot(M,ch08(10,9,0.06,0.1,1),'-.k')
只是一个值。对于要绘制的线,需要有多个y值(等于x值的数量),它们可以相同(就像您的情况一样)。否则,它将只是点(这在代码中发生)ch08(10,9,0.06,0.1,1)
randn('state', 100);
S=10; E=9; sigma=0.1; r=0.06; T=1; Dt=1e-3; N=T/Dt;
M = [2^5,2^6,2^7,2^8,2^9,2^10,2^11,2^12,2^13,2^14,2^15,2^16,2^17];
hold on;
for k=1:numel(M)
%No need of loop here. Generate all random values in one go
Sfinal = S*exp((r-0.5*sigma^2)*T+sigma*sqrt(T)*randn(M(k),1));
V = exp(-r*T)*max(Sfinal-E,0);
aM = mean(V); bM = std(V);
plot(M(k),aM,'x');
errorbar(M(k), aM, 1.96*bM/sqrt(M(k)));
end
chvar = repmat(ch08(10,9,0.06,0.1,1),1,numel(M)); %<----Notice this
plot(M, chvar,'--.k');
%Other figure cosmetics
%These commands shouldn't be inside the loop to avoid unnecessary computations
title('Monte Carlo Approximations');
xlabel('Samples'); % x-axis label
ylabel('Option value approximation'); % y-axis label
set(gca,'XScale', 'log','YScale', 'log');
axis([10^1 10^6 10^0.1 10^0.3]);
set(gca,'YTick',[10^0.1 10^0.2 10^0.3]);
set(gca,'YTickLabel',{'10^{0.1}','10^{0.2}','10^{0.3}'});
%Nothing is wrong with using yticks and yticklabels function but those require >=R2016b
randn('state',100);
S=10;E=9;西格玛=0.1;r=0.06;T=1;Dt=1e-3;N=T/Dt;
M=[2^5,2^6,2^7,2^8,2^9,2^10,2^11,2^12,2^13,2^14,2^15,2^16,2^17];
等等
对于k=1:numel(M)
%这里不需要循环。一次性生成所有随机值
Sfinal=S*exp((r-0.5*σ^2)*T+sigma*sqrt(T)*randn(M(k),1));
V=exp(-r*T)*最大值(Sfinal-E,0);
aM=平均值(V);bM=std(V);
图(M(k),aM,'x');
误差条(M(k),aM,1.96*bM/sqrt(M(k));
结束
chvar=repmat(ch08(10,9,0.06,0.1,1),1,numel(M));%初始实现默认为M=1e4。我不认为M=1e4会损害for循环中M的后续值。请阅读关于没有bug的内容。请删除显示为错误的部分?根据,“--”绘制了一条虚线。您没有正确使用它。函数中没有bug。你的使用中有一个错误,我的错误根本不是直觉。看来我使用的是审阅文档时使用的预期格式。不,你没有。