Skip to content

as.function parameters

11 messages · Rui Barradas, David Winsemius, Jason Edgecombe +2 more

#
Hi,

i have a question regarding the as.function functionality.
I need to save multiple functions with adjusted parameters in a list.
The adjusted parameters are changed via variables in a loop.
The problem I'm facing right now is that the functions saved in
the list don't have the values of the parameters but the variable parameter
itself. Is there a way to force a call-by-value instead of a
call-by-address?

Example(not the real function):
tmp <- 4
f <- as.function(alist(y=,x=tmp,y+x)
f(1) > 5
tmp <- 2
f(1) > 3

thanks ahead and sorry if this question has already been asked before.
I could not really grasp the problem to one Subject.

--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,
Yes, there is one way, just do nothing. R always uses call-by-value.
I'm not completely sure about this, but when you create f() the parameter x
default value
is a promise, not an actual, evaluated value. Courtesy lazy evaluation.
It's only when you need it that it's evaluated.

For instance, using your code, you don't need to create tmp before creating
f().
Since R is not evaluating tmp, there's no error to throw.
You'll only get an error  if you try to *use* f() without creating tmp or
without a default value override.

rm(f, tmp) # just to be sure
f <- as.function(alist(y=, x=tmp, y + x))
f(1)	# error
f(1, 3) # no error
tmp <- 2
f(1) # 3

Hope this helps,
Rui Barradas



jackl wrote
--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4621903.html
Sent from the R help mailing list archive at Nabble.com.
1 day later
#
Thanks for the fast answer..

The problem with your "free" definition of f is, that it does not
really fit my task.

I have to build a tree where each node contains the same function
just with adjusted parameters. Setting the parameters in the function call
is not really an option.

Is there another way to do so?

thanks ahead

--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4627012.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

I'm not completely sure if I understand, but maybe using environments.
If each function copy in the list has a different environment, you can set
the parameters values in that environment.
Something like


f <- as.function(alist(y=,x=tmp,y+x))

env <- list(
	e1=new.env(),
	e2=new.env()
)
fun <- list(
	f1=f,
	f2=f
)

environment(fun$f1) <- env$e1
environment(fun$f2) <- env$e2
env$e1$tmp <- 2
env$e2$tmp <- 4
fun$f1(1)
fun$f2(1)


Also, a code example would be nice, like this I'm just guessing.

Hope this helps,

Rui Barradas

jackl wrote
--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4627079.html
Sent from the R help mailing list archive at Nabble.com.
3 days later
#
Hi,

~ well that seems to be a better solution. 
Question is how much an enviroment for each node costs in terms of 
save space..

The example code is hard to present, because it is really based on that
problem.
The frame of the problem is, that I have to write a program that gives
each node in a binomial tree a function with individual parameters.. in my
case
the current stock price at that node. 

I don't want to create these functions manually.. that would be too much of
an
overload with a tree even of moderate size. But if I define these functions
via the as.function functionality it gives me the above problem.

Hope that clarifies the problem a bit

thanks again

--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4629979.html
Sent from the R help mailing list archive at Nabble.com.
#
On May 14, 2012, at 1:56 PM, jackl wrote:

            
Seems unlike that it would expand you space very much. Every function  
you create will have an environment.
You need to demonstrate what "tree" structure you propose to populate  
and then access. Since tree's are not an R data type, you should show  
us _exactly_ how you will create one, presumably using "lists". Once  
you have a small example it is very easy to present by using the  
output of the 'dput' function.
You should also realize that you are probably limiting your audience  
severely by not posting context. Most people do not read Rhelp on  
Nabble.
2 days later
#
Hi..

Ok here is an example on how I wanted the tree to be implemented in R:

-  the tree is, as you wrote, saved as a list of different tree levels
- each tree level is also saved as a list of different nodes in that
specific level
- and for the last part, each node is then saved as a list of functions

example:
tree <- list(root, lvl1, lvl2)
root <- list(node00)
lvl1 <- list(node10, node11)
lvl2 <- list(node20, node21, node22)

node00 <- list(f1,f2,f3)
node10 <- list(f1,f2,f3)
node11 <- list(f1,f2,f3)
..

note: I wrote f1, f2 and f3 in each node because it is the same function,
just with the different parameter, the stock price at that node.

I tried implementing a tree manually and I found out that the
independences between one node and each childnode cause a
heavy computation power.. (the function f3 contains f3 of the
two childnodes and so on..)
example:
node11$f3 <- max(node11$f2, node21$f3, node22$f3)

Im facing this problem even with a tree with 'only' 4-5 layers.. 

best thanks for any answers

--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4630353.html
Sent from the R help mailing list archive at Nabble.com.
#
On 05/17/2012 06:30 AM, jackl wrote:
Hi,

First, what problem are you trying to solve? A tree structure is not a 
problem... it's a tool, to solve a problem.

Second, a tree data structure is simply a simplified graph structure. 
There are many packages in R that deal with and store graph structures. 
Two that come to mind are igraph 
<http://cran.r-project.org/web/packages/igraph/index.html> and sna 
<http://cran.r-project.org/web/packages/sna/>

Since those packages can handle graphs, they can handle trees as well. 
As an added bonus, the code is already available and well-tested.

If memory usage is a concern, then look into using a sparse matrix 
implementation.

Jason
4 days later
#
Hi there,

~ the 'problem' or rather the task I'm trying to solve is to implement
an algorithm to compute the ask/bid price of american options in a 
close to R related program language. 

because i'm not really using R but just it's basic functionalities I cannot
rely on different packages included in the R space.

is it usual for R to compute nested functions with such a workload?

Best thanks

--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4630858.html
Sent from the R help mailing list archive at Nabble.com.
#
Jumping in from a finance perspective...

You really don't want to actually use nested lists for this -- the
overhead and index-book-keeping will quickly become quite annoying.

Instead use a 2D array (I assume you're using the CRR Binomial Tree
model for an American Option) where the column number less one is how
many steps into the tree you are and the row is how many ups/downs
you've had (your call which one) -- then just roll your option valuing
formula with loops over the array.

This is implemented in the fOptions package in the CRRBinomialTree
function, but if you can't use real R that might/might not help you.
It is open source however...

Michael
On Tue, May 22, 2012 at 6:22 AM, jackl <jackspam at hotmail.de> wrote:
7 days later
#
Hi there, 

~ sry for the late answer.. and thanks for the advice.

i'm using a different approach than the CRR model because
i'm implementing a pricing algorithm for american options
with transaction costs.

i'm not sure if i should close this thread for now, because
right now i'm trying to do a different approach facing other
difficulties and any other question that may come up would not fit to
the topic anymore. i might start a new thread regarding a current
problem though.

so thanks for all the help ^^



--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4631759.html
Sent from the R help mailing list archive at Nabble.com.