[R] problem applying .C function in outer product
Dirk Eddelbuettel
edd at debian.org
Thu Mar 13 13:59:24 CET 2003
On Wed, Mar 12, 2003 at 03:02:15PM -0800, Jim McLoughlin wrote:
> The function is loaded, and it works for when given specific (scalar)
> arguments, but does not work when passed Vector arguments, and
> therefore fails in the outer product I need.
[...]
> blackS <- function(S,K,r,T,vol){
> tempx <- .C("blackScholesR",
> as.double(S),
> as.double(K),
> as.double(r),
> as.double(T),
> as.double(vol),
> as.double(0.0))
> as.double(tempx[6])
> }
>
> This function returns the appropriate value for constant vals, example:
>
> > blackS(52,50,0,0.5,0.43)
> [1] 7.213387
>
> but fails if I use vector arguments for S or T. I ultimately want to
> create an outer product varying both S and T and graph the surface.
Allin already pointed out that your function is scalar-valued -- yet you
want a vector context. No real problem there -- you simply need to provide
it.
It so happens that I have an example in my small RQuantLib package (which is
essentially a wrapper around some functions of the larger QuantLib library)
that does just that.
>From example(EuropeanOptionArrays):
# define two vectors for the underlying and the volatility
und.seq <- seq(10,180,by=2)
vol.seq <- seq(0.1,0.9,by=0.1)
# evaluate them along with three scalar parameters
EOarr <- EuropeanOptionArrays("call", underlying=und.seq,
strike=100, dividendYield=0.01,
riskFreeRate=0.03,
maturity=1, volatility=vol.seq)
[... more code to plot this ...]
and you can look how EuropeanOptionArrays is essentially looping over all
possible vector-form arguments to pass them to the scalar function that
calls the corresponding (closed-form) QuantLib pricer.
Hope this helps, Dirk
--
Prediction is very difficult, especially about the future.
-- Niels Bohr
More information about the R-help
mailing list