Plotting confidence intervals-lines in one graph with means inside

I need to plot 12 confidence intervals in one graph using MATLAB and each with mean marked inside. Any ideas how I can do it?
I am using a code below for computing confidence intervals:
S=10; E=9; sigma=0.1; r=0.06; T=1;
Dt=1e-3; N=T/Dt; M=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)]
You can find an example of what I need in the following picture:

 Respuesta aceptada

As stylistic aside, in Matlab "use the vectors, Luke!" :)
Replace the loop with the vectorized equivalent; NB: the "dot" operator '.*' for element-wise multiplication and the M,1 argument to randn to generate the sample vector in one call.
Sfinal = S*exp((r-0.5*sigma^2)*T+sigma*sqrt(T).*randn(M,1));
V=exp(-r*T).*max(Sfinal-E,0);
For plot, not certain just how your code is to relate to the sample plot but look at
doc errorbar % for error bars on a data plot
then using
hold all
plot([x1 xend],[meanVal meanVal],'--')
will add the mean value dashed line.

4 comentarios

Thanks, but still I do not understand how I can plot a vertical line for the specific interval.
errorbar does that for you, given the error bands. The question is, what are the '*' points on the plots; some selected values of the V vector, maybe? If I run the code, the error band is so small it just doesn't show up on the plot since M is so large 1/M is a very small number.
errorbar(X,Y,ones(size(X))*diff(conf)/2,'*')
set(gca,'xscale','log','yscale','log')
will give an errorbar plot of X,Y with errorbars at data points X,Y shown as '*' on log axes. Use whatever it is that X, Y are supposed to be that these conf limits are to be applied to...
Yes, the '*' is a mean of each confidence interval.
So, is the result what you expected? I'm still not positive...I made a stab at it from your code but it looks nothing like the sample figure as far as the data itself...
subplot(2,1,1)
errorbar(ix,V(ix),ones(size(V(ix)))*diff(conf)/2,'*')
set(gca,'xscale','log','yscale','log')
xlim([10 1E6]), ylim([1 2.5])
subplot(2,1,2)
errorbar(ix,V(ix),ones(size(V(ix)))*diff(conf)/2,'*')
set(gca,'xscale','log','yscale','log')
xlim([10 1E6])
ylim([1 1.1])
results in
where had to blow up the y axis tremendously for the error bar magnitude to be large enough to be visible.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 3 de Jun. de 2017

Comentada:

dpb
el 4 de Jun. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by