6

I'd like to declare a function that will be part of the equation that I then want to plot. The equation that I want to plot is defined as

\frac{e^{-rT}N'(d_{2})}{S\sigma\sqrt{T}}

The function to be declared is the probability density function

N'\left (x  \right )=\frac{1}{\sqrt{2\pi}}e^{-x^2/2}

where x should equal to d_2, and d_2 must also be declared by

d_{2}=\frac{ln(S/K)+(r-\frac{\sigma^2}{2})T}{\sigma\sqrt{T}})

If possible I'd like to be able to define K, r and sigma in advance, so that they can flow into above calculation as fixed values. For the values of S (x axis) and T (y axis) I want to take a different domains, e.g. for S a range 95 to 105, and for T a range 0.01 to 0.3. The overall result should be a surfaceplot where the results of the intial equation are on the z axis. Thanks a million, I hope so much that somebody can help me!

ADDITION: the code that was provided by @percusse works very well but when I try to modify it following the logic of its structure I get an error. Here is the modified code:

\documentclass{article}
\usepackage{pgfplots}
\begin{document} 

\begin{tikzpicture}[
declare function={ Nprime(\x)                 = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2))); 
                   d1(\x,\y,\KK,\RR,\SIG)     = (ln(\x/\KK)+(\RR+(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   d2(\x,\y,\KK,\RR,\SIG)     = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   myfun(\x,\y,\KK,\RR,\SIG)  = - (exp(-\RR*\y)*d1*Nprime(d2(\x,\y,\KK,\RR,\SIG))/(\y*(pow(\x,2))*(pow(\SIG,2))));
                 },
]
\begin{axis}[ y domain=0.01:0.3, domain=95:105,view={25}{20}, 
]
\addplot3[surf] {myfun(x,y,100,0,0.09)};
\end{axis}
\end{tikzpicture}
\end{document} 

What am I doing wrong?

ONE MORE ADDITION: I have tried to swap the \y and \SIG values. Here is the code that does not work

\documentclass{article}
\usepackage{pgfplots}
\begin{document} 
\begin{tikzpicture}[
declare function={ Nprime(\x)                 = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2))); 
                   d2(\x,\SIG,\KK,\RR,\y)     = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   myfun(\x,\SIG,\KK,\RR,\y)  = exp(-\RR*\y)*Nprime(d2(\x,\SIG,\KK,\RR,\y))/(\x*\SIG*sqrt(\y));
                 },
]
\begin{axis}[
y domain=0.01:0.09, domain=95:105, view={25}{20}, 
]
\addplot3[surf] {myfun(x,SIG,100,0,0.04)};
\end{axis}
\end{tikzpicture}
\end{document} 

What's wrong with it? Thanks a lot!

  • 1
    I fear that your aim is definitely not possible : in general the elaborate LaTeX formulas cannot be parsed by the pgf engine. Just as an example, there is not \frac and your denominator would be writen as \S*\sig*sqrt(\T) provided that you have \def-ined the values of \S,\sig and \T (note te absence of \ before sqrt to {evaluate} de square root ! – Jhor Jul 21 '13 at 11:17
  • can you provide some values of the constants? – percusse Jul 21 '13 at 11:28
  • K = 100, r = 0, sigma = 0.09 – ajafarov Jul 21 '13 at 11:29
  • the thing is, that I have that graph in matlab and I've tried to use matlab2tikz to transform the matlab-figure to latex. matlab2tikz does it with some distrotion, i.e. it simply chops off a part of the graph and places that piece arbitrarily somewhere else – ajafarov Jul 21 '13 at 11:35
  • @ajafarov You are missing the arguments of d1 – percusse Jul 21 '13 at 14:36
13

I don't know what it should look like but from the constants you have provided I get this

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[
declare function={ Nprime(\x)                 = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2))); 
                   d2(\x,\y,\KK,\RR,\SIG)     = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   myfun(\x,\y,\KK,\RR,\SIG)  = exp(-\RR*\y)*Nprime(d2(\x,\y,\KK,\RR,\SIG))/(\x*\SIG*sqrt(\y));
                 },
]
\begin{axis}[y domain=0.01:0.3,domain=95:105,view={150}{20}]
\addplot3[surf] {myfun(x,y,100,0,0.09)};
\end{axis}
\end{tikzpicture}


\end{document}

enter image description here

  • OH LA LAAAAA that is exactly what I need! You guys are simply Latex-Gods!!! Is it possible to replace the axis in the sense, that I get the 0-0.3 domain on the right hand side, and the 96-104 at the left hand side, simply replace them? – ajafarov Jul 21 '13 at 11:41
  • @ajafarov You can rotate the 3D view angles with view option added to the axis options. Replace it with view={25}{20}. – percusse Jul 21 '13 at 11:43
  • @ajafarov My pleasure. Next time please provide some more data and a MWE if possible. Writing the code from scratch is really not fun. – percusse Jul 21 '13 at 11:46
  • when I try to slightly modify the code that you have written today in order to make it suitable for another application I get an error: PGF Math: Could not parse input '' as a floating point number, sorry. The unreadable part was near..." could you please help me? why is it the case? – ajafarov Jul 21 '13 at 14:07
  • I have edited the question and made an addition incl. code – ajafarov Jul 21 '13 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.