On Sat, 11 Jun 2016, Riccardo (Jack) Lucchetti wrote:
On Sat, 11 Jun 2016, Riccardo (Jack) Lucchetti wrote:
> On Sat, 11 Jun 2016, Giuseppe Vittucci wrote:
>
>> Dear all,
>>
>> I am running gretl 2016b on Linux Mint Debian 2.
>> I would like to sample from a known discrete distribution.
>
> How do you like this?
>
> <hansl>
> function matrix msample(matrix p, scalar r, scalar c)
> ret = {}
> matrix p = vec(p) # ensure p is a column
> matrix p /= sumc(p) # and it's a probability vector
> if minc(p) >= 0
> ret=sumr(muniform(r*c, 1) .< cum(p)')
> endif
> return ret
> end function
> </hansl>
Whoops, forgot one line, sorry!
<hansl>
function matrix msample(matrix p, scalar r, scalar c)
ret = {}
matrix p = vec(p) # ensure p is a column
matrix p /= sumc(p) # and it's a probability vector
if minc(p) >= 0
ret = sumr(muniform(r*c, 1) .< cum(p)')
ret = mshape(ret, r, c)
endif
return ret
end function
</hansl>
OMG, today's not my day, is it?
ret = sumr(muniform(r*c, 1) .< cum(p)')
should be
ret = sumr(muniform(r*c, 1) .> cum(p)')
Sorry for the noise, people.
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------