.packageName <- "RQuantLib"
## RQuantLib -- R interface to the QuantLib libraries
##
## Copyright 2002 Dirk Eddelbuettel <edd@debian.org>
##
## $Id: arrays.R,v 1.2 2002/11/15 01:49:28 edd Exp $
##
## This file is part of the RQuantLib library for GNU R.
## It is made available under the terms of the GNU General Public
## License, version 2, or at your option, any later version,
## incorporated herein by reference.
##
## This program is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE.  See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public
## License along with this program; if not, write to the Free
## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
## MA 02111-1307, USA

EuropeanOptionArrays <- function(type, underlying, strike, dividendYield,
                                 riskFreeRate, maturity, volatility) {
  n.underlying <- length(underlying)
  n.strike <- length(strike)
  n.dividendYield <- length(dividendYield)
  n.riskFreeRate <- length(riskFreeRate)
  n.maturity <- length(maturity)
  n.volatility <- length(volatility)

  res <- numeric(n.underlying * n.strike * n.dividendYield * n.riskFreeRate *
                 n.maturity * n.volatility)
  dim(res) <- c(n.underlying, n.strike, n.dividendYield, n.riskFreeRate,
                 n.maturity, n.volatility)
  dimnames(res) <- list(paste("s",underlying,sep="="),
                        paste("k",strike,sep="="),
                        paste("y",dividendYield,sep="="),
                        paste("r",riskFreeRate,sep="="),
                        paste("t",maturity,sep="="),
                        paste("v",volatility,sep="="))
  value <- delta <- gamma <- vega <- theta <- rho <- divRho <- res
  for (s in 1:n.underlying)
    for (k in 1:n.strike)
      for (y in 1:n.dividendYield)
        for (r in 1:n.riskFreeRate)
          for (t in 1:n.maturity)
            for (v in 1:n.volatility) {
              val <- .Call("QL_EuropeanOption",
                           list(type=as.character(type),
                                underlying=as.double(underlying[s]),
                                strike=as.double(strike[k]),
                                dividendYield=as.double(dividendYield[y]),
                                riskFreeRate=as.double(riskFreeRate[r]),
                                maturity=as.double(maturity[t]),
                                volatility=as.double(volatility[v])),
                           PACKAGE="RQuantLib")
              value[s,k,y,r,t,v] <- val$value
              delta[s,k,y,r,t,v] <- val$delta
              gamma[s,k,y,r,t,v] <- val$gamma
              vega[s,k,y,r,t,v] <- val$vega
              theta[s,k,y,r,t,v] <- val$theta
              rho[s,k,y,r,t,v] <- val$rho
              divRho[s,k,y,r,t,v] <- val$divRho
            }
  value <- drop(value)
  delta <- drop(delta)
  gamma <- drop(gamma)
  vega <- drop(vega)
  theta <- drop(theta)
  rho <- drop(rho)
  divRho <- drop(divRho)
  invisible(list(value=value, delta=delta, gamma=gamma, vega=vega,
                 theta=theta, rho=rho, divRho=divRho,
                 parameters=list(type=type, underlying=underlying,
                   strike=strike, dividendYield=dividendYield,
                   riskFreeRate=riskFreeRate, maturity=maturity,
                   volatility=volatility)))
}

## RQuantLib -- R interface to the QuantLib libraries
##
## Copyright 2002 Dirk Eddelbuettel <edd@debian.org>
##
## $Id: implied.R,v 1.6 2004/09/12 18:53:27 edd Exp $
##
## This file is part of the RQuantLib library for GNU R.
## It is made available under the terms of the GNU General Public
## License, version 2, or at your option, any later version,
## incorporated herein by reference.
##
## This program is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE.  See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public
## License along with this program; if not, write to the Free
## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
## MA 02111-1307, USA

## also dumps core (0.3.7)
EuropeanOptionImpliedVolatility <-
  function(type, value, underlying, strike, dividendYield,
           riskFreeRate, maturity, volatility) {
  #if (is.null(class(x)))
  #  class(x) <- data.class(x)
  #UseMethod("EuropeanOptionImpliedVolatility", x, ...)
  UseMethod("EuropeanOptionImpliedVolatility")
}

EuropeanOptionImpliedVolatility.default <-
  function(type, value, underlying, strike, dividendYield,
            riskFreeRate, maturity, volatility) {
  val <- .Call("QL_EuropeanOptionImpliedVolatility",
               list(type=as.character(type),
		    value=as.double(value),
                    underlying=as.double(underlying),
                    strike=as.double(strike),
                    dividendYield=as.double(dividendYield),
                    riskFreeRate=as.double(riskFreeRate),
                    maturity=as.double(maturity),
                    volatility=as.double(volatility)),
               PACKAGE="RQuantLib")
  class(val) <- c("EuropeanOptionImpliedVolatility","ImpliedVolatility")
  warning("EuropeanOptionImpliedVolatility currently disabled.\nDebugging help on this segfaults would be appreciated.\n")
  val
}

# also dumps core (0.3.7)
AmericanOptionImpliedVolatility <-
  function(type, value, underlying, strike, dividendYield, riskFreeRate,
           maturity, volatility, timeSteps=150, gridPoints=151) {
  #if (is.null(class(x)))
  #  class(x) <- data.class(x)
  #UseMethod("AmericanOptionImpliedVolatility", x, ...)
  UseMethod("AmericanOptionImpliedVolatility")
}

AmericanOptionImpliedVolatility.default <-
  function(type, value, underlying, strike, dividendYield, riskFreeRate,
            maturity, volatility, timeSteps=150, gridPoints=151) {
  val <- .Call("QL_AmericanOptionImpliedVolatility",
               list(type=as.character(type),
		    value=as.double(value),
                    underlying=as.double(underlying),
                    strike=as.double(strike),
                    dividendYield=as.double(dividendYield),
                    riskFreeRate=as.double(riskFreeRate),
                    maturity=as.double(maturity),
                    volatility=as.double(volatility),
                    timeSteps=as.integer(timeSteps),
                    gridPoints=as.integer(gridPoints)),
               PACKAGE="RQuantLib")
  class(val) <- c("AmericanOptionImpliedVolatility","ImpliedVolatility")
  warning("AmericanOptionImpliedVolatility currently disabled.\nDebugging help on this segfaults would be appreciated.\n")
  val
}

# dumps core :-/
#BinaryOptionImpliedVolatility <- function(x, ...) {
#  if (is.null(class(x)))
#    class(x) <- data.class(x)
#  UseMethod("BinaryOptionImpliedVolatility", x, ...)
#}

#BinaryOptionImpliedVolatility.default <-
#  function(type, value, underlying, strike, dividendYield, riskFreeRate,
#            maturity, volatility, cashPayoff=1) {
#  val <- .Call("QL_BinaryOptionImpliedVolatility",
#               list(type=as.character(type),
#		    value=as.double(value),
#                    underlying=as.double(underlying),
#                    strike=as.double(strike),
#                    dividendYield=as.double(dividendYield),
#                    riskFreeRate=as.double(riskFreeRate),
#                    maturity=as.double(maturity),
#                    volatility=as.double(volatility),
#                    cashPayoff=as.double(cashPayoff)),
#               PACKAGE="RQuantLib")
#  class(val) <- c("BinaryOptionImpliedVolatility","ImpliedVolatility")
#  val
#}

print.ImpliedVolatility <- function(x, ...) {
  cat(paste("Implied Volatility for", class(x)[1], "is", format(x[1]), "\n"))
  invisible(x)
}

summary.ImpliedVolatility <- function(object, ...) {
  cat(paste("Implied Volatility for", class(object)[1],
            "is", format(object[1]), "\n"))
  cat(paste("with parameters\n"))
  print(unlist(object[[2]]))
  invisible(object)
}
## RQuantLib -- R interface to the QuantLib libraries
##
## Copyright 2002 Dirk Eddelbuettel <edd@debian.org>
##
## $Id: option.R,v 1.4 2003/06/01 04:27:40 edd Exp $
##
## This file is part of the RQuantLib library for GNU R.
## It is made available under the terms of the GNU General Public
## License, version 2, or at your option, any later version,
## incorporated herein by reference.
##
## This program is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE.  See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public
## License along with this program; if not, write to the Free
## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
## MA 02111-1307, USA

EuropeanOption <- function(type, underlying, strike, dividendYield,
                           riskFreeRate, maturity, volatility) {
  #if (is.null(class(x)))
  #  class(x) <- data.class(x)
  #UseMethod("EuropeanOption", x, ...)
  UseMethod("EuropeanOption")
}

EuropeanOption.default <- function(type, underlying, strike, dividendYield,
                                   riskFreeRate, maturity, volatility) {
  val <- .Call("QL_EuropeanOption",
               list(type=as.character(type),
                    underlying=as.double(underlying),
                    strike=as.double(strike),
                    dividendYield=as.double(dividendYield),
                    riskFreeRate=as.double(riskFreeRate),
                    maturity=as.double(maturity),
                    volatility=as.double(volatility)),
               PACKAGE="RQuantLib")
  class(val) <- c("EuropeanOption", "Option")
  val
}

AmericanOption <- function(type, underlying, strike, dividendYield,
                           riskFreeRate, maturity, volatility,
                           timeSteps=150, gridPoints=151) {
  #if (is.null(class(x)))
  #  class(x) <- data.class(x)
  #UseMethod("AmericanOption", x, ...)
  UseMethod("AmericanOption")
}

AmericanOption.default <- function(type, underlying, strike, dividendYield,
                                   riskFreeRate, maturity, volatility,
                                   timeSteps=150, gridPoints=151) {
  val <- .Call("QL_AmericanOption",
               list(type=as.character(type),
                    underlying=as.double(underlying),
                    strike=as.double(strike),
                    dividendYield=as.double(dividendYield),
                    riskFreeRate=as.double(riskFreeRate),
                    maturity=as.double(maturity),
                    volatility=as.double(volatility),
                    timeSteps=as.integer(timeSteps),
                    gridPoints=as.integer(gridPoints)),
               PACKAGE="RQuantLib")
  class(val) <- c("AmericanOption","Option")
  val
}

BinaryOption <- function(type, underlying, strike, dividendYield,
                         riskFreeRate, maturity, volatility,
                         cashPayoff) {
  #if (is.null(class(x)))
  #  class(x) <- data.class(x)
  #UseMethod("BinaryOption", x, ...)
  UseMethod("BinaryOption")
}

BinaryOption.default <- function(type, underlying, strike, dividendYield,
                                 riskFreeRate, maturity, volatility,
                                 cashPayoff) {
  val <- .Call("QL_BinaryOption",
               list(type=as.character(type),
                    underlying=as.double(underlying),
                    strike=as.double(strike),
                    dividendYield=as.double(dividendYield),
                    riskFreeRate=as.double(riskFreeRate),
                    maturity=as.double(maturity),
                    volatility=as.double(volatility),
                    cashPayoff=as.double(cashPayoff)),
               PACKAGE="RQuantLib")
  class(val) <- c("BinaryOption", "Option")
  val
}

BarrierOption <- function(barrType, type, underlying, strike,
                          dividendYield, riskFreeRate, maturity,
                          volatility, barrier, rebate=0.0) {
  #if (is.null(class(x)))
  #  class(x) <- data.class(x)
  #UseMethod("BarrierOption", x, ...)
  UseMethod("BarrierOption")
}

BarrierOption.default <- function(barrType, type, underlying, strike,
                                  dividendYield, riskFreeRate, maturity,
                                  volatility, barrier, rebate=0.0) {
  val <- .Call("QL_BarrierOption",
               list(barrType=as.character(barrType),
                    type=as.character(type),
                    underlying=as.double(underlying),
                    strike=as.double(strike),
                    dividendYield=as.double(dividendYield),
                    riskFreeRate=as.double(riskFreeRate),
                    maturity=as.double(maturity),
                    volatility=as.double(volatility),
                    barrier=as.double(barrier),
                    rebate=as.double(rebate)),
               PACKAGE="RQuantLib")
  class(val) <- c("BarrierOption", "Option")
  val
}

plot.Option <- function(x, ...) {
  warning(paste("No plotting available for class", class(x)[1],"\n"))
  invisible(x)
}

print.Option <- function(x, digits=options('digits')[[1]], ...) {
  cat(paste("Concise summary of valuation for", class(x)[1], "\n"))
  print(unlist(x[1:7]), digits=digits)
  invisible(x)
}

summary.Option <- function(object, digits=options('digits')[[1]], ...) {
  cat(paste("Detailed summary of valuation for", class(object)[1], "\n"))
  print(unlist(object[1:7]),digits=digits)
  print(unlist(object[["parameters"]]),digits=digits)
  invisible(object)
}




## RQuantLib -- R interface to the QuantLib libraries
##
## Copyright 2002 Dirk Eddelbuettel <edd@debian.org>
##
## $Id: zzz.R,v 1.2 2003/02/06 03:14:06 edd Exp $
##
## This file is part of the RQuantLib library for GNU R.
## It is made available under the terms of the GNU General Public
## License, version 2, or at your option, any later version,
## incorporated herein by reference.
##
## This program is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE.  See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public
## License along with this program; if not, write to the Free
## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
## MA 02111-1307, USA

.First.lib <- function(lib, pkg) {
  #cat("This is a pre-release. The interface might change...\n")
  library.dynam(pkg, pkg, lib )
}

