## ultimate R recursion

One of my students wrote the following code for his R exam, trying to do accept-reject simulation (of a Rayleigh distribution) and constant approximation at the same time:

```fAR1=function(n){
u=runif(n)
x=rexp(n)
f=(C*(x)*exp(-2*x^2/3))
g=dexp(n,1)
test=(u<f/(3*g))
y=x[test]
p=length(y)/n #acceptance probability
M=1/p
C=M/3
hist(y,20,freq=FALSE)
return(x)
}
```

which I find remarkable if alas doomed to fail! I wonder if there exists a (real as opposed to fantasy) computer language where you could introduce constants C and only define them later… (What’s rather sad is that I keep insisting on the fact that accept-reject does not need the constant C to operate. And that I found the same mistake in several of the students’ code. There is a further mistake in the above code when defining g. I also wonder where the 3 came from…)

### 10 Responses to “ultimate R recursion”

1. Wit Jakuczun Says:

This would work in Prolog language which is VERY real language.

• Wit Jakuczun Says:

Sorry I missed Matt’s comment before posting mine.

• xi'an Says:

No problem, thanks!

2. Matt Says:

As stated above, you could make declarative languages work that way, with one example being Prolog (http://en.wikipedia.org/wiki/Prolog ). Another example, which isn’t a language, but might be closer to what you are asking for, are propagation of constraint solvers, where you put in quantity relationships, and the variables won’t resolve to numbers until you put in enough points to determine the system (http://en.wikipedia.org/wiki/Propagation_constraint , and http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-22.html#%_sec_3.3.5 ). But both of these are no substitute for just plain declaring constants up front when you have enough information to do so… these systems would allow you to cheat that, but it is not their main utility.

3. Bill Tozier Says:

Mathematica?

4. Moyenne Armorique Says:

I don’t know if this answer your question, but in R you can define and initialize variables outside a function before calling it, thanks to scoping. In this case, since accept-reject does not need C, you could do the following:

> C=1
> fAR1(10^4)

C and fAR1() belong to the same environment, so there won’t be any problem.

If you want to change the value of C from within fAR1() you can substitute the line

C=M/3

with

C <<- M/3

It goes without saying that you have to be extremely careful in handling local and global variables.

• Dani Says:

anyway, initializing the variable C outside the function does not make a lot of sense to me…

as Christian said there are two mistakes here:
1) the pupil does not seem to understand the importance of initialization.. this is a general coding error
2) the pupil does not understand that he needs just the kernel of the distribution.. that’s one of the beauties of the AR algorithm.. and this is a statistical error

• xi'an Says:

Thanks! Actually, I was not really asking a question, more wondering about the state of mind of my student who could handle an unknown constant as if it has a fixed if unknown value and then later find its value… All this done without the slightest idea of what this kind of programming would involve!

5. Simon Byrne Says:

This would work for a declarative language, the example most statisticians would be familiar with is WinBUGS.

• xi'an Says:

Thanks. I had not thought of Bugs! What I had in mind was more in line with a recursion where you would introduce a constant later to be solved.

This site uses Akismet to reduce spam. Learn how your comment data is processed.