How can I repeat a function i times for each i-th value of a vector?

How can I repeat a function i times for each i-th value of a vector? [def_int_Altadis]=defintAltadis(cdsAltadis, zerotermstruc, deffreq, spreadfreq, RR) where cdsAltadis and zeroterm are vectors 8x1 deffreq,spreadfreq,RR are scalar numbers. The result is a vector 8x1 that changes as RR changes. I do not want to repeat the function by myself i times changing each time the value of RR, but I want Matlab to pick up the values from a vector, let's say vecRR. Here's the old code:
if true
%
function [def_int_Altadis]=defintAltadis(cdsAltadis, zerotermstruc, deffreq, spreadfreq, RR)
%function to bootstrap the default intensities (lambda) under the assumption of piecewise constant default intensities
%inputs: cdsAltadis: term structure of cds of Altadis having maturities (in months) in the first column and CDS spreads (in bps) in the second column
% zerotermstruc: term structure of risk free zero coupon bond rates having maturities (in months) in the first column and zero rates in the second
% column
% deffreq: frequency (in months) at which default can occur (e.g. 1=once a month)
% spreadfreq: frequency (in months) at which spreads are paid
% RR: recovery rate (e.g. 0.4)
Nspreads=length(cdsAltadis(:,1)); def_int_Altadis=zeros(Nspreads,1);
for i=1:Nspreads T=cdsAltadis(i,1); mT=T/deffreq; nT=T/spreadfreq; def_intensity=fzero(@(x) myfun(x, mT, nT, deffreq, spreadfreq, RR, zerotermstruc, cdsAltadis, def_int_Altadis, i), 0.07); def_int_Altadis(i,1)=def_intensity; end def_int_Altadis; plot(cdsAltadis(:,1),def_int_Altadis, 'b+:'), title('Altadis')
function diffspread=myfun(x, mT, nT, deffreq, spreadfreq, RR, zerotermstruc, cdsAltadis, def_int_Altadis, i) %function to minimize: difference between market spread and fair spread for m=1:mT tm=m*deffreq; indx1=min(find(zerotermstruc(:,1)>=tm)); zerodf(m,1)=exp(-zerotermstruc(indx1,2)*tm/12); indx2=min(find(cdsAltadis(:,1)>=tm)); if indx2==1 probsur=exp(-x*tm/12); else probsur=exp(-x*(tm/12-cdsAltadis((indx2-1),1)/12)); indx2=indx2-1; while indx2>0 if indx2==1 probsur=probsur*exp(-def_int_Altadis(indx2)*cdsAltadis(indx2,1)/12); else probsur=probsur*exp(-def_int_Altadis(indx2)*(cdsAltadis(indx2,1)-cdsAltadis((indx2-1),1))/12); end indx2=indx2-1; end end probsurv(m,1)=probsur; if m==1 defprobvec(m,1)=1-probsur; else defprobvec(m,1)=probsurv(m-1,1)-probsurv(m,1); end end numerator=(1-RR)*sum(zerodf.*defprobvec); %numerator of fair CDS spread calculation
for n=1:nT
tn=n*spreadfreq;
indx1=min(find(zerotermstruc(:,1)>=tn));
zerodf2(n,1)=exp(-zerotermstruc(indx1,2)*tn/12);
indx2=min(find(cdsAltadis(:,1)>=tn));
if indx2==1
probsur=exp(-x*tn/12);
else
probsur=exp(-x*(tn/12-cdsAltadis((indx2-1),1)/12));
indx2=indx2-1;
while indx2>0
if indx2==1
probsur=probsur*exp(-def_int_Altadis(indx2)*cdsAltadis(indx2,1)/12);
else
probsur=probsur*exp(-def_int_Altadis(indx2)*(cdsAltadis(indx2,1)-cdsAltadis((indx2-1),1))/12);
end
indx2=indx2-1;
end
end
probsurv2(n,1)=probsur;
if n==1
probvec(n,1)=probsur+0.5*(1-probsur);
else
probvec(n,1)=probsurv2(n,1)+0.5*(probsurv2(n-1,1)-probsurv2(n,1));
end
end
denominator=(spreadfreq/12)*sum(zerodf2.*probvec); %denominator (RPV01)
diffspread=(cdsAltadis(i,2)/10000)-numerator/denominator;
end

 Risposta accettata

dpb
dpb il 15 Mag 2014
Modificato: dpb il 15 Mag 2014
combined_result=arrayfun( ...
@(rr) defintAltadis(cdsAltadis, zerotermstruc, deffreq, spreadfreq, rr), ...
vecRR, 'uniformoutput',false);
Warning--air code, untested.
Or, just use a loop.

Categorie

Scopri di più su Get Started with MATLAB in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by