Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Newbie question
Replies: 5   Last Post: Aug 16, 2007 2:54 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Paul Abbott

Posts: 1,437
Registered: 12/7/04
Re: Newbie question
Posted: Feb 14, 2005 1:00 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

In article <cukb9l$lns$1@smc.vnet.net>, Anonymous wrote:

> I use the Mathematica package below. The function that I call is
> HestonVanilla. This function has 13 arguments that I need to provide. I want
> to change the programm, so that I do not have to give "lambda", "rf", "cp"
> and "G" each time I use the function. Instead I want that these arguments
> are always set to specific values, namely:
>
> lambda = 0
> rf =0
> cp = 1
> G = 0
>
> The reason for that is that, when I call the function from Excel I can only
> have 9 arguments.


You can shorten the argument list using constructs like

HestonVanilla[k_,o_,sigma_,rho_,r_,v_,S_,K_,tau_] :=
HestonVanilla[k,o,sigma,rho,r,0,v,S,K,tau,1,0}]

A better alternative is to change the syntax of HestonVanilla,
collecting together related parameters (using either [], {}, or a
mixture of both) so as to structurally partition the argument list.

For example, you could collect together the arguments as follows

HestonVanilla[k_,o_,{sigma_,rho_,lambda_:0},{r,rf_:0},v_,{S_,K_},
{tau_,cp_:1,G_:0}]

or, a more sensible collection may involve something like

HestonVanilla[k_,o_,{sigma_,rho_,lambda_:0}][{r,rf_:0},v_,{S_,K_},
{tau_,cp_:1,G_:0}]

In each case, the optional parameters have to be at the end of each list.

> It would be great if someone could help me with that, since I need it
> urgently for my diploma thesis. Thank you!


Looking at the code you supply, I think that, for efficiency, a total
re-write would be a good idea. For example, using numerical codes for
the Greeks is not optimal (can you remember which is which without
looking at the list?). Also, code fragments such as

d = Table [
Sqrt[(I*rsf - b[[j]])^2 -
sigma^2*(2*I*u[[j]]*fi - fi^2)],
{j,2}];

can be replaced by

Sqrt[(I rsf - b)^2 - sigma^2 (2 I u fi - fi^2)]

because Sqrt and Power are Listable. Similarly for g, cl, dl, and f in
AuxFunc. The code for f would probably benefit the use of ComplexExpand.

Cheers,
Paul


> BeginPackage["Options`HestonVanilla`"]
>
> (*****************************************************************************
> *******
> author: Uwe Wystup, wystup@mathfinance.de
> date : November 1999
> ******************************************************************************
> *******)
> AuxFunc::usage = "computing the ingredients for HestonVanilla"
> HestonVanilla::usage = "Steven Heston's Stochastic Volatility Model\n
> to price European put and call options\n
> HestonVanilla[k,o,sigma,rho,lambda,r,rf,v,S,K,tau,cp,G]\n
> The input parameters are:\n
> k: MeanReversion\n
> o: LongRunVariance\n
> sigma: VolaVolatility\n
> rho: Correlation\n
> lambda: VolaRiskPremium\n
> r: domestic RiskFreeRate\n
> rf: foreign RiskFreeRate\n
> v: CurrentVariance\n
> S: AssetPrice\n
> K: ExercisePrice\n
> tau: ExpirationTime\n
> cp : 1 for call, -1 for put\n
> G: Greek\n
> 0 : value\n
> 1 : spot delta\n
> 2 : spot gamma\n
> 3 : theta (in years)\n
> 4 : vega (wrt v)\n
> 5 : domestic rho\n
> 6 : foreign rho\n
> 7 : vomma (wrt v)\n
> 21: dual delta (wrt K)\n
> 22: dual gamma (wrt K)"
>
>
> Begin["`Private`"]
>
> AuxFunc[k_,o_,sigma_,rho_,lambda_,
> r_,rf_,v_,S_,K_,tau_,fi_] :=
> Block[{u,a,b,x,rsf,d,g,cl,dl,f},
> u = {0.5,-0.5};
> a = k*o;
> b = {k+lambda-rho*sigma, k+lambda};
> x = Log[S];
> rsf = rho*sigma*fi;
>
> d = Table [
> Sqrt[(I*rsf - b[[j]])^2 -
> sigma^2*(2*I*u[[j]]*fi - fi^2)],
> {j,2}];
>
> g = Table [
> (b[[j]] - I*rsf + d[[j]]) /
> (b[[j]] - I*rsf - d[[j]]),
> {j,2}];
>
> cl = Table [
> I*(r-rf)*fi*tau +
> (a/sigma^2)*((b[[j]]-I*rsf+d[[j]])*tau -
> 2*Log[(1-g[[j]]*Exp[d[[j]]*tau])/(1-g[[j]])]),
> {j,2}];
>
> dl = Table [
> ((b[[j]] - I*rsf + d[[j]])/sigma^2) *
> ((1-Exp[d[[j]]*tau])/
> (1-g[[j]]*Exp[d[[j]]*tau])),
> {j,2}];
>
> f = Table [
> Exp[cl[[j]] + dl[[j]]*v + I*fi*x], {j,2}];
>
> Table [{
> Re[(Exp[-I*fi*Log[K]] * f[[j]])/(I*fi)],
> Re[(Exp[-I*fi*Log[K]] * f[[j]])],
> Re[(dl[[j]]*Exp[-I*fi*Log[K]] * f[[j]])/(I*fi)],
> Re[(dl[[j]]*dl[[j]]*Exp[-I*fi*Log[K]] * f[[j]])/(I*fi)],
> d[[j]],
> f[[j]]
> },{j,2}]];
>
>
> HestonVanilla[k_,o_,sigma_,rho_,lambda_,
> r_,rf_,v_,S_,K_,tau_,cp_,G_] :=
> Switch[G,
> 0, (*value*)
> Block[{p,j},
> p = Table[
> 0.5+(1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,1]], {fi,0.,100}],
> {j,2}];
> S*Exp[-Log[1+rf]*tau]*(p[[1]]-(1-cp)/2)
> - K*Exp[-Log[1+r]*tau]*(p[[2]]-(1-cp)/2)],
> 1, (*delta*)
> Block[{p,j},
> p = Table[
> 0.5+(1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,1]], {fi,0.,100}],
> {j,1}];
> Exp[-Log[1+rf]*tau]*(p[[1]]-(1-cp)/2)],
> 2, (*gamma*)
> Block[{dp,j},
> dp=Table[
> (1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,2]], {fi,0.,100}],
> {j,1}];
> Exp[-Log[1+rf]*tau]/S*dp[[1]]],
> 3, (*theta*)
> 50000*
> (HestonVanilla[k,o,sigma,rho,lambda,r,rf,v,S,K,tau-0.00001,cp,0]
> -HestonVanilla[k,o,sigma,rho,lambda,r,rf,v,S,K,tau+0.00001,cp,0]),
> 4, (*vega*)
> Block[{dpv,j},
> dpv=Table[
> (1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,3]], {fi,0.,100}],
> {j,2}];
> S*Exp[-Log[1+rf]*tau]*dpv[[1]]
> - K*Exp[-Log[1+r]*tau]*dpv[[2]]],
> 5, (*rho*)
> Block[{p,j},
> p = Table[
> 0.5+(1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,1]], {fi,0.,100}],
> {j,2,2}];
> K*tau*Exp[-Log[1+r]*tau]*(p[[1]]-(1-cp)/2)/(1+r)],
> 6, (*rhof*)
> Block[{p,j},
> p = Table[
> 0.5+(1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,1]], {fi,0.,100}],
> {j,1}];
> S*tau*Exp[-Log[1+rf]*tau]*((1-cp)/2-p[[1]])/(1+rf)],
> 7, (*vomma*)
> Block[{d2pv,j},
> d2pv=Table[
> (1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,4]], {fi,0.,100}],
> {j,2}];
> S*Exp[-Log[1+rf]*tau]*d2pv[[1]]
> - K*Exp[-Log[1+r]*tau]*d2pv[[2]]],
> 21, (*dual delta*)
> Block[{p,j},
> p = Table[
> 0.5+(1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,1]], {fi,0.,100}],
> {j,2,2}];
> Exp[-Log[1+r]*tau]*((1-cp)/2-p[[1]])],
> 22, (*dual gamma*)
> Block[{dp,j},
> dp=Table[
> (1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,2]], {fi,0.,100}],
> {j,2,2}];
> Exp[-Log[1+r]*tau]/K*dp[[1]]],
> 101, (* Real part *)
> Block[{dp,j},
> dp=Table[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> 2][[j,1]],
> {j,2}];
> dp[[2]]],
> 111, (* d *)
> Block[{dp,j},
> dp=Table[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> 2][[j,5]],
> {j,2}];
> dp[[2]]],
> 112, (*probability factors*)
> Block[{p,j},
> p = Table[
> 0.5+(1/N[Pi])*NIntegrate[
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,
> fi][[j,1]], {fi,0.,100}],
> {j,2}];
> p[[1]]],
> 30, (*the Heston Integrand*)
> AuxFunc[k,o,sigma,rho,lambda,Log[1+r],Log[1+rf],v,S,K,tau,cp][[1,1]],
> _,0];
>
> End[]
>
> EndPackage[]
>
>


--
Paul Abbott Phone: +61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailto:paul@physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.