cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Generate n samples of size m from uniform (0,1) using MATHCAD PRIME

KN_9565343
4-Participant

Generate n samples of size m from uniform (0,1) using MATHCAD PRIME

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

9 REPLIES 9

Like this?

Cornel_0-1700828985305.png

or like this?

Cornel_0-1700829071167.png

 

Werner_E
24-Ruby V
(To:Cornel)

@Cornel 

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

Werner_E_0-1700831462380.png

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

Werner_E_1-1700831532789.png

 

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:

Werner_E_2-1700831644876.png

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

Werner_E_3-1700831765671.png

 

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

Werner_E_4-1700832367032.png

or even shorter using runif:

Werner_E_5-1700832457486.png

 

You may also consider using a more general function which you could multiple times in the worksheet to create different sets of numbers

Werner_E_6-1700832835606.png

 

Werner_E_7-1700832847997.png

You could also make the number limits (o and 1 in your example) as additional function arguments

Werner_E_8-1700832997255.png

Werner_E_9-1700833165964.png

 

Modified P9 sheet attached

KN_9565343
4-Participant
(To:Werner_E)

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.

KN_9565343
4-Participant
(To:KN_9565343)

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

LucMeekes
23-Emerald III
(To:KN_9565343)

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:

LucMeekes_0-1701021784658.png

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:

LucMeekes_1-1701021861685.png

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:

Werner_E_0-1701035566421.png

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:

Werner_E_1-1701036409629.png

 

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

Werner_E_0-1701036305693.png

 

Prime 9 sheet attached

 

 

KN_9565343
4-Participant
(To:Werner_E)

Thank you so much, Werner. Highly appreciated. It worked. Stay Blessed.

Top Tags