Creating a function that returns a series (not a real)
Creating a function that returns a series (not a real)
Dear all,
I have a complex nonlinear model which for which I need to use custom functions. In particular I would like to define the to-be-optimized equation as:
frm1 f1 seriesOfIndepVars = %functionReturningDepVars
where "seriesOfIndepVars" is a given series of observed values and "%functionReturningDepVars" is a custom function that is supposed to return a series containing the dependent variables as produced by my model.
My problem now is the following: Despite having read the manuals, I have not yet succeeded in creating a function that returns a series (and not a real). Studying the manual, I thought it should work like this, but it does not:
function %functionReturningDepVars z
type series %functionReturningDepVars
set %functionReturningDepVars = loanV * z^2
end
where loanV is a given global variable of type series. My actual function is much more complicated, this is just for illustration.
If I now try to use this function in my nonlinear model or if I try to call it using
set testVar = %functionReturningDepVars(2.5)
I get
Expected Type REAL, Got SERIES Instead
I should mention that I use version 5. Any help would be greatly apprechiated,
many thanks
Timo
I have a complex nonlinear model which for which I need to use custom functions. In particular I would like to define the to-be-optimized equation as:
frm1 f1 seriesOfIndepVars = %functionReturningDepVars
where "seriesOfIndepVars" is a given series of observed values and "%functionReturningDepVars" is a custom function that is supposed to return a series containing the dependent variables as produced by my model.
My problem now is the following: Despite having read the manuals, I have not yet succeeded in creating a function that returns a series (and not a real). Studying the manual, I thought it should work like this, but it does not:
function %functionReturningDepVars z
type series %functionReturningDepVars
set %functionReturningDepVars = loanV * z^2
end
where loanV is a given global variable of type series. My actual function is much more complicated, this is just for illustration.
If I now try to use this function in my nonlinear model or if I try to call it using
set testVar = %functionReturningDepVars(2.5)
I get
Expected Type REAL, Got SERIES Instead
I should mention that I use version 5. Any help would be greatly apprechiated,
many thanks
Timo
Re: Creating a function that returns a series (not a real)
Timo:zyxxer wrote: function %functionReturningDepVars z
type series %functionReturningDepVars
set %functionReturningDepVars = loanV * z^2
end
The FUNCTION instruction, for creating user-defined functions, wasn't added until version 5.1.
I believe you have 5.02, so I'm not sure how you are even getting as far as you did?
Try doing Help-About RATS to check exactly which version you are using.
Regards,
Tom Maycock
Re: Creating a function that returns a series (not a real)
I think what you want is to define a FRML which uses a FUNCTION taking the entry number as one of its inputs. For example:
This is used in the (version 6 and later) BONDS example program to estimate the parameters governing the term-structure function given a set of bond prices.
Code: Select all
function BondPV bond
type real BondPV
type integer bond
*
local real mdate cdate
*
compute mdate=maturity(bond)
compute BondPV=100.0 * $
exp(-mdate*(a0+mdate*a1+%max(mdate-cusp,0.0)*a2))
*
* Walk backwards through the coupons.
*
compute cdate=mdate
while (cdate > 0.0) {
compute BondPV=BondPV + coupon(bond) * $
exp(-cdate*(a0+cdate*a1+%max(cdate-cusp,0.0)*a2))
compute cdate=cdate-1
}
*
* Adjust for simple interest payable by the purchaser for the initial
* coupon. cdate will be -(fraction of period).
*
compute BondPV=BondPV+coupon(bond)*cdate
end
*
frml BondPrice value = BondPV(t)Re: Creating a function that returns a series (not a real)
@Tom Maycock:
Help -> about says I am using version 5.11, maybe they updated it at some time.
@Tom Doan:
I got it: I let the function return a real and use "t", which seems to be a reserved variable name, as one of the input variables to cycle through the series. This way the function actually returns a series. That works, many thanks.
Help -> about says I am using version 5.11, maybe they updated it at some time.
@Tom Doan:
I got it: I let the function return a real and use "t", which seems to be a reserved variable name, as one of the input variables to cycle through the series. This way the function actually returns a series. That works, many thanks.