Create loop for Monte Carlo Simulation

#1
Hi

I have some trouble writing out a monte carlo simulation properly.
I assign variables for the process before the steps.

The first step is assigning variable j = j + 1
Second step i draw three random numbers from a U(0,1) dist. u1, u2 and u3.
Third step is a long if formula. It has to be a function of j, V(j), so as j increases it should record a new output. The result in this step is what is used in the end to create the final result.
Fourth step is another if forumla: if(j = n) j = 0 && s = s +1 && a long formula
Fifth step is another if formula if s < S then loop to step 3

How do I create this in an efficient manner? Some of the if formulas are rather long.

Thanks for your help in advance

Morten
 
#2
You shouldn't generate 3 random numbers in each loop of the simulation.

Let's say you want 2000 simulations. Then generate a 2000*3 matrix of random numbers as your first step, and in each loop/sapply/lapply of your monte carlo use MATRIX[i,] to slice off 3 new random numbers. It should be faster that way.

Why don't you write the loop yourself then submit it for critique?
 
Last edited:
#4
I don't understand if(u[,i]<delta) for i=1,2,3. u[,i] is a vector of 100 numbers and delta is 1 number. What does it mean to say that 100 numbers is less than 1 number?

If you mean to say that every number in the vector is less than delta, well that's fine, but you have to tell R this properly.

This is why you're getting the error message that you're getting.

Solution:

Code:
if(sum(u[,1]>delta)==0)DO SOMETHING CONDITIONAL UPON EVERY SINGLE VALUE IN u[,1] BEING LESS THAN DELTA.
Or, alternatively

Code:
if(sum(u[,1]<delta)==length(sum))
Will give you what you want. To see what this is doing, type in u[,1]<delta. Then, sum just counts how many TRUEs there are.

By the way I think you want u[1,] and u[2,], because you want 3 numbers (a row) not 100 (a column).

EDIT based on re-reading your OP:

I suspect that what you want is to test individual numbers in these if statements. Do a for loop or a sapply over 1 to 100, and use u[i,1], u[i,2], u[i,3] (i'th row, where i is in 1 to 100, and the 1st 2nd or 3rd number in that row).
 
Last edited:
#5
I need to test the individually generated numbers in the if statements yes.
I used the [j,1] format as j will be added 1 for each loop. Would R read that correctly?

I will look into the for loop and sapply functions.
 
#7
I have reworked the code, but I am still having some trouble.
When looking at the syntax for if functions this is how I get the setup for a if function:

if(cond){
command 1
command 2
} else {
command 3
}

As far as I can see this is the setup I have used for the if formulas. I think the problem occurs because I need to include the second if statement inside the first if statement? I keep getting an '}' unexpected, or 'else' unexpected error in R.
Is my problem in the if formulas setup?
 
Last edited:
#8
Code:
if(cond){
   command 1
   command 2
} else {
   command 3
}
}
The last "}" shouldn't be there. Just delete it and your syntax is correct.
 
#9
Code:
if(u[,1] < alpha){

if (u[,2] < delta){

V_b[j] <- qnorm( u[,3], epsilon, 1)
V_s[j] <- qnorm( u[,3], epsilon + mu, 1)
} else {

V_b[j] <- qnorm( u[,3], epsilon + mu, 1)
V_s[j] <- qnorm( u[,3], epsilon, 1)

} else {

V_b[j] <- qnorm( u[,3], epsilon, 1)
V_s[j] <- qnorm( u[,3], epsilon, 1)
}
This is unnecessarily confusing and will cause mistakes. I think you should be avoiding using else here.

Just do something like this:

Code:
if(u[,1] < alpha && u[,2] < delta){
   V_b[j] <- qnorm( u[,3], epsilon, 1)
   V_s[j] <- qnorm( u[,3], epsilon + mu, 1)
}

if(u[,1] < alpha && u[,2] >= delta){
   V_b[j] <- qnorm( u[,3], epsilon + mu, 1)
   V_s[j] <- qnorm( u[,3], epsilon, 1)
}
etc etc etc ...

You can use || for "or". "&&" means "and".
Also you're still trying to test whether a bunch of numbers (e.g. u[,1]) is bigger or smaller than a single number (e.g. alpha), which as I've said doesn't make any sense. The "<" and ">" symbols test for 1 number. They can be used for vectors but not in the way you're doing it, and that's a bit too advanced for you.

Also here's a tip. Don't try to get your whole loop to work first off. Set j = 1 and see if it runs with that. And run tiny subsets of your loop for j=1 to identify exactly where the problem area is. If you find that there's an error but you don't break up the block of code into tiny bits and test each bit, then your approach to bug-testing is uber inefficient.
 
Last edited:
#10
I have gotten it to work.

Thanks a lot for your help.

The reason why I'm testing if a number is larger or smaller than a given value is based on the model I am using, so it is correct here.
 
#11
Your code was testing (unintentionally by you) whether a vector of numbers (u[,1], which is the first column of number sin u) was larger than a single number. This did not make sense.

Testing whether one number is bigger than another of course makes sense.