Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
I am trying to generate n samples of size m from the uniform (0,1). I am using runif(1,0,1) in the loop. Please see the attached file. Thanks
Like this?
or like this?
You are creating n*m=1000 times a vector with n=10 elements (it should be a vector with m=100 elements) but your programs only return the last one created. That quite inefficient 😉
@KN_9565343 wrote:
I am trying to generate n samples of size m from the uniform (0,1). I am using runif(1,0,1) in the loop. Please see the attached file. Thanks
So I interpret what yo write that way that at the end you would like a nested n x 1 vector where each element is a m x 1 vector of random numbers.
So must note that "runif" always creates a matrix/vector. The command runif(1,0,1) creates a 1 x 1 matrix, wbich is probably not what you want.
Your program actually creates the n * m random values, but every new run in the k-loop overwrites the values created in the i-loop before. Furthermore a program (unless you explicitly use the "return" statement) always returns the last value it calculated and in our case thats the last value b[m in the last run of your loops and thats the reason you juts get one single 1x1 matrix with just one number as your output.
If you really want to calculate each of the n*m values singly you should use the "rnd" conmmand and not "runif".
The most "logical" way to do it with two nested loops would be this
but Prime refuses the usage of the double vector indices when creating b[i]k. So we have to resort to using am temporary variable
BUT ....
as you already know about the runif command and its ability to create a vector, why not use it to create each m x 1 vector in one go and just use the k-loop to create n of these vectors:
I would prefer this way (using a loop), but you could also use a range variable to do the job instead of a for-loop
Prime 9 file attached
One additional interpretation - maybe you would prefer a m x n matrix, where each column represents a m x 1 vector of random numbers
or even shorter using runif:
You may also consider using a more general function which you could multiple times in the worksheet to create different sets of numbers
You could also make the number limits (o and 1 in your example) as additional function arguments
Modified P9 sheet attached
Thank you for the prompt solution, Werner. Highly appreciated. However, the first suggestion does not work. Please see the attached code. It gives an error saying the tmp variable is not defined.
Hello Werner. I hope you can assist me. I would like to perform the following:
1) Generate n=5 random samples of size m=8 with the help of the uniform. These are the Yi's in my code. Then for each of the 5-samples I evaluate M as in my code. Apparently my code gives me 1 value, I expect n=5 values
To get an index (into an array, vector or matrix), for example index i into matrix M, you must type M[i (That is M, square bracket open, and i). With Ctrl _ (Control, underscore) you get a literal subscript, that is not an index, but just an extension of a name. They look similar, but there are differences:
The first is M index k, the second is M subscript k. The difference is more clearly shown when you put the cursor on the expression:
See the faint square bracket between M and its index k.
Further you should realise that the result of a program in Prime is the evaluation of the last expression executed.
In your case that was Mk. (which, due to k not being an index was a simple variable anyway) where you meant k to be an index to M. To get the full array M as output, you best use the program statement return..
All that is fixed in the attachment, for you to enjoy.
Success!
Luc
As Luc already wrote you got confused about literal and matrix index and mixed them up.
Furthermore you still create a 1x1 matrix when you call runif(1,0,1). You should either use rnd(1) instead or even better use runif(m,0,1) to create the whole vector of random numbers in one go without the i-loop.
Here is my suggestion:
or a bit more efficient as the ln(1-b) and also the functions G1 and G2 are only called once and not twice as in the previous version:
BTW, the calculations run into errors if a value in the random vector v is exactly 0 or 1. It never happened in my tests but according to the Prime documentation this could happen.
Not sure if you want to cope with this or if you simply want to press F9 to recalculate the sheet and so create other random numbers.
One possible way to cope with this very unlikely case is to use a custom made runif2 function instead of the built-in runif like te one below
Prime 9 sheet attached
Thank you so much, Werner. Highly appreciated. It worked. Stay Blessed.