Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Newbie question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54194] Re: [mg54177] Newbie question
  • From: yehuda ben-shimol <bsyehuda at gmail.com>
  • Date: Sun, 13 Feb 2005 00:21:25 -0500 (EST)
  • References: <200502120658.BAA21821@smc.vnet.net>
  • Reply-to: yehuda ben-shimol <bsyehuda at gmail.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi
Default values for function argument are implemented as
f[x_,y_,z_:2]:=...
That is, if you call it only with the first two arguments z by default
will be equal to 2.
You can add any number of arguments with default values AS LONG as
they are the right most arguments
yehuda


On Sat, 12 Feb 2005 01:58:19 -0500 (EST)
<Email address removed at author's request> 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.
> I guess this is a piece of cake for someone familar with Mathematica.
> 
> It would be great if someone could help me with that, since I need it
> urgently for my diploma thesis. Thank you!
> 
> BeginPackage["Options`HestonVanilla`"]
> 
> (*****************************************************************************
> author: Uwe Wystup, wystup at 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[]
> 
>


  • References:
  • Prev by Date: Re: Newbie question
  • Next by Date: Re: Re: Re: Re: finding package inExtraPackages`Enhancements`
  • Previous by thread: Newbie question
  • Next by thread: Re: Newbie question