[Scilab-users] Can you suggest a more efficient procedure for generating random variables?

Federico Miyara fmiyara at fceia.unr.edu.ar
Mon Mar 18 19:24:23 CET 2019


Heinz,

I'm not quite sure if this is what you need. Even if you have been 
suggested a good solution, here is a possible alternative.

Suppose you have a number of equally spaced bins (this is not really 
necessary but it generally is the case), for instance

x = (0:100);

with a cummulative distribution which may be experimental but for this 
example I'll use this one:

y = (x/100).^4;

This means that the values close to 100 are much more frequent.

You can then apply the inverse function to a series of 1000000 uniformly 
distributed random numbers such as

y1 = rand(1,1000000);

As you don't know the inverse for any value different from the tabulated 
ones, you just interpolate:

x1 = interp(y, x, y1);

This is linear interpolation, which in most cases will suffice. You 
could try splines, at the expense of reduced speed.

It is reasonably fast, 1000000 values take an average of 0.1 ms on an i7 
laptop with Windows 7.

Federico


On 18/03/2019 05:22, Heinz Nabielek wrote:
> y is a previously defined table with values monotonically increasing from zero to one.
> h
>
>> On 18.03.2019, at 06:43, Federico Miyara <fmiyara at fceia.unr.edu.ar> wrote:
>>
>>
>> Heinz,
>>
>> I don't find your example clear enough. What's y? Is it defined previously?
>>
>> Perhaps an example would be useful.
>>
>> Regards,
>>
>> Federico Miyara
>>
>>
>> On 17/03/2019 19:49, Heinz Nabielek wrote:
>>> I need to generate random deviates x according to a given cumulative distribution y that is available only in tabular form.
>>>
>>> Scilab coding was easy by table lookup:
>>>
>>> length(y)=   360. // only for general information
>>> N=1000;
>>> z=grand(1,N,'def');
>>> x=[];
>>> for i=1:N;
>>> x=[x find(y>z(i),1)];
>>> end;
>>>
>>> Problem is that execution times are exponentially increasing when I want one million deviates.
>>>
>>> Can you suggest a significantly more efficient procedure?
>>> Heinz
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>


---
El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
https://www.avast.com/antivirus




More information about the users mailing list