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:

 p=length(y)/n #acceptance probability

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. This would work in Prolog language which is VERY real language.

  2. 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. 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

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

    • 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

    • 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!

  4. Simon Byrne Says:

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

    • 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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