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
nulldata 200
set seed 123
series x = msample({0.1,0.6,0.3},$nobs,1)
freq x --plot=none
</hansl>
Perhaps we could make this an internal function. Allin?
-------------------------------------------------------
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
-------------------------------------------------------