ARG?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

ARG?

Bazman76
Hi there,

The code below was posted at the following address:

http://www.quantcode.com/modules/mydownloads/singlefile.php?lid=495

Just wondered what significance the "1;" has (I am compltetly new to octave)

Also what does the following section do?

badidx=find(abs(arg(S))>0);
nBad_values=size(badidx,1) %should ideally be zero
goodidx=find(abs(arg(S))==0);

S=S(goodidx);

Also what does the arg function do? (more generally is there a resource where I can type in an octave function name and find out what it does?)

Thanks

Baz



1;


clear all;

global alpha;
global beta;
global rho;
global nu;
global T;
global S;
global r;


%----------Input parameters ----------------
S0=100; %Spot price
r=0.05; %risk free rate
K=98; %strike
alpha = 0.852; %alpha parameter
beta=.5; %beta parameter set from backbone or aesthetics
rho = 0.5; %rho parameter
nu = .42; %variance of volatility paramater
T=1; %maturity price
nsimulations=100000; %no of MC simulations (will get doubled for anithetic)
nT=50; %no. of time step points
%------------------------

function ret=GetSABRsigma(F,X)
        global alpha;
        global beta;
        global rho;
        global nu;
        global T;

        z=(nu/alpha)*((F.*X).^(0.5*(1-beta))).*log(F./X);
        zhi=log(  (  ((1-2*rho*z+z.*z).^0.5) + z - rho)/(1-rho) );
        numer1= ( ((1-beta)^2)/24 )*((alpha*alpha)./((F.*X).^(1-beta)));
        numer2=.25*rho*beta*nu*alpha./((F*X).^((1-beta)/2));
        numer3=((2-3*rho*rho)/24)*nu*nu;
        numer=alpha*(1+(numer1+numer2+numer3)*T).*z;
        denom1=((1-beta)^2/24)*(log(F./X)).^2;
        denom2=(((1-beta)^4)/1920)*((log(F./X)).^4);
        denom=((F.*X).^((1-beta)/2)).*(1+denom1+denom2).*zhi;
        ret=numer./denom;
        idx=find(X==F);
        if idx>0,
                %use eqn(20) to avoid NaN problem
                numer1=(((1-beta)^2)/24)*alpha*alpha/(F^(2-2*beta));
                numer2=.25*rho*beta*nu*alpha/(F^(1-beta));
                numer3=((2-3*rho*rho)/24)*nu*nu;
                VolAtm=alpha*(1+(numer1+numer2+numer3)*T)/(F^(1-beta))
                ret(idx)=VolAtm;
        end
    endfunction

%form a correlated pair of random variables for the 2 factors
rndmat1=randn(nsimulations,nT);
rndmat1=[rndmat1; -rndmat1];
rndmat2=randn(nsimulations,nT);
rndmat2=[rndmat2; -rndmat2];
rndmat2=rho*rndmat1+(1-rho*rho)^0.5*rndmat2;
nsimulations=nsimulations*2;

dt=T/(nT-1);
F0=S0*exp(r*T);

rdt=dt^0.5;
F=ones(nsimulations,1)*F0;
S=ones(nsimulations,1)*S0;
alphavec=ones(nsimulations,1)*alpha;
for i=1:nT-1,
        %dF=(r-0.5*sig*sig)*dt + sig*rdt*rndmat(:,i);
        %dS=(r-0.5*sig*sig)*dt + sig*rdt*rndmat(:,i);
        %F=F.*exp(dF);
        %S=S.*exp(dS);
        S = S + r*S*dt + alphavec.* exp(r*(beta-1)*T).*S.^beta*rdt.*rndmat1(:,i);
        %alphavec = alphavec + nu*alphavec*rdt.*rndmat2(:,i);
        alphavec = alphavec.*exp(nu*rdt.*rndmat2(:,i));
end

badidx=find(abs(arg(S))>0);
nBad_values=size(badidx,1) %should ideally be zero
goodidx=find(abs(arg(S))==0);

S=S(goodidx);

payoff=max(S-K,0);
mccallprice=exp(-r*T)*mean(payoff)

% Calculate implied volatility using Hagan's analytical formula
sig=GetSABRsigma(F0,K)
Dest=exp(-r*T);
d1=(log(F0/K) + 0.5*sig*sig*T)/(sig*T^0.5);
d2=(log(F0/K) - 0.5*sig*sig*T)/(sig*T^0.5);
V_BScall=Dest*(F0*normcdf(d1)-K*normcdf(d2)) %analytical price
Reply | Threaded
Open this post in threaded view
|

Re: ARG?

bpabbott
Administrator
On Jun 21, 2011, at 6:02 AM, Bazman76 wrote:

> Hi there,
>
> The code below was posted at the following address:
>
> http://www.quantcode.com/modules/mydownloads/singlefile.php?lid=495
>
> Just wondered what significance the "1;" has (I am compltetly new to octave)
>
> Also what does the following section do?
>
> badidx=find(abs(arg(S))>0);
> nBad_values=size(badidx,1) %should ideally be zero
> goodidx=find(abs(arg(S))==0);
>
> S=S(goodidx);
>
> Also what does the arg function do? (more generally is there a resource
> where I can type in an octave function name and find out what it does?)
>
> Thanks
>
> Baz

The "1;" has no significance.

badidx is a vector in the indices of S where its phase is greater than zero.

nBad is the number of indices of S where its phase is greater than zero.

goodidx is a vector containing the indices of S where its phase is equal to zero.

Ben

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: ARG?

Jordi Gutiérrez Hermoso-2
On 21 June 2011 08:26, Ben Abbott <[hidden email]> wrote:
> On Jun 21, 2011, at 6:02 AM, Bazman76 wrote:
>
>> Hi there,
>>
>> The code below was posted at the following address:
>>
>> http://www.quantcode.com/modules/mydownloads/singlefile.php?lid=495
>>
>> Just wondered what significance the "1;" has (I am compltetly new to octave)

> The "1;" has no significance.

In this particular example, it's cargo cult. Otherwise, it's a silly
hack for distinguishing function files from scripts. The way the
Matlab/Octave language works is that if the first statement is a
function definition, then that file is treated as a function file. The
1; is a trick to not make the first statement a function definition,
hence make the file is a script file. Here it's unnecessary because
the statement in that file immediately following the 1; is not a
function definition.

>> Also what does the arg function do?

It's the argument (angle) of a complex number

>> (more generally is there a resource where I can type in an octave
>> function name and find out what it does?)

Yes, Octave is such a resource. Type "help foo" to figure out what foo
does.

You can also use this:

     http://octave.sourceforge.net/functions_by_alpha.php

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: ARG?

Bazman76
Thanks to both of you.

I thought arg might be equivalent to phase but its use seems strange that it is used here?

The author seems to be using

badidx=find(abs(arg(S))>0);

to test if S is complex at any point in the array.

But if that is the case why not use isreal()?

And I don't see how S could be complex from the code above?

The only square root used is rdt which is stricly positive?
Can randn produce complex numbers?? (surely not)

Baz

Reply | Threaded
Open this post in threaded view
|

Re: ARG?

James Sherman

The author seems to be using

badidx=find(abs(arg(S))>0);

to test if S is complex at any point in the array.

But if that is the case why not use isreal()?

While I am not a mind reader, one possibility is that he'd have to use something like 
arrayfun(@isreal, x)
since isreal is not an elementwise operation.  I'm not intimately familiar with isreal to know if there are any corner cases that this would prove problematic.
 
And I don't see how S could be complex from the code above?

S = S + r*S*dt + alphavec.* exp(r*(beta-1)*T).*S.^beta*rdt.*rndmat1(:,i);

Raises a number to the power beta, which has magnitude less than one, thus the result would be complex if an element of S is negative.

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: ARG?

Bazman76
Thank you James you answers are very helpful :-)