First of many Rookie questions sorry: Finally broke free of Mathematica and
gone for real OO programing, and up 2 grand a year to boot !

Problem concerns the use of nls function mostly but I have used an example
from finance that is similar to my aim:

Using an fOptions PlainVanillaOptions function [slightly edited]:

GBSOption =
function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma)
{   
    TypeFlag = TypeFlag[1]
    d1 = ( log(S/X) + (b+sigma*sigma/2)*Time ) / (sigma*sqrt(Time))
    d2 = d1 - sigma*sqrt(Time)
    if (TypeFlag == "c")
        result = S*exp((b-r)*Time)*CND(d1) - X*exp(-r*Time)*CND(d2)
    if (TypeFlag == "p")
        result = X*exp(-r*Time)*CND(-d2) - S*exp((b-r)*Time)*CND(-d1)

    # Parameters:
    param = list()
    param$TypeFlag = TypeFlag
    param$S = S
    param$X = X
    param$Time = Time
    param$r = r
    param$b = b
    param$sigma = sigma


    # Return Value:
    result
}

Lets assume you wanted to solve for a volatility that gives nls across a
range of data you had [optiontype, underlying, strike, time, premium]
(forget the ³why would you²!)

I find that using the nls function in manner:

nls(premium ~ GBSOption(optiontype, underlying, strike, time, 0, 0,
x),data=testdata)

Is problematic

I would like the nls function to scroll through a list of different types of
optiontypes, c(³c²,²c²,²p²,...) for example.

I have little understanding how nls treats IF commands in general and how it
treats a list of non-numerical flags. Any help on this would great.
Apologies again for what I am sure is a simple question.

Glenn




        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to