Welcome, William!

You are probably looking for “random()”, which has the same syntax as 
“zeroes()” but returns a vector of pseudorandom values on [0,1).
To make a vector of a million of those, use “$a = random(1e6)”.

To make random integers based on a histogram that you already have in-hand, a 
simple way is:

$vec = pdl(0,1,1,1,2,2,2,3); # note 8 values
$rand = $vec->(floor(random(1e6) * 8))->sever;

The “random(1e6”) makes a million elements on [0,1).  Multiply by 8 and take 
the floor to get integers on [0,7].  The outermost operation indexes $vec with 
the corresponding random value.  Since there are three 1’s, 1 is three times  
as likely in the output.

Does that work?

Best,
Craig



> On Apr 6, 2018, at 5:16 PM, William Schmidt <t.william.schm...@gmail.com> 
> wrote:
> 
> Hello Piddlers,
> 
> I am moving from R to PDL, with tons of experience with Perl, lots with R but 
> zero with PDL,
> so this is a pretty basic question. I can see from the PDL Book that PDL is 
> very
> sophisticated, with much more functionality than I will ever use, but I want
> to master basic PDL to leverage my Perl. My focus is on probability in two 
> dimensions so
> I will be working mostly with 1-dimensional vectors. Here is an example from 
> R that 
> I would like to learn how to do in PDL. It is a small example but once I 
> master
> the construction of this data I will extend it to much larger vectors.
>  
> Suppose I have random variable X whose values and probabilities are as 
> follows:
> 
> x   p(x)
> 0   1/8
> 1   3/8
> 2   3/8
> 3   1/8
> 
> To get a sample of 50 random values drawn from this population with 
> replacement in R I
> would say:
> 
> x <- seq.int <http://seq.int/>(0,3)       # Concatenate a sequence of ints 
> from 0 to 3.
> x                              # print x.
> [1]  0 1 2 3
> 
> weights <- c(1/8, 3/8, 3/8, 1/8)    # Another form of concatenation.
> weights
> [1]  0.125 0.375 0.375 0.125
> 
> s <- sample(x, 50, replace=TRUE, prob=weights)
> s
>  [1] 0 1 1 3 2 2 2 3 2 0 0 1 3 1 1 3 0 2 1 2 2 1 3 1 2 2 0 2 2 2 3 2
> [33] 1 1 3 1 2 2 1 1 0 1 3 2 2 1 3 0 1 1
> 
> I can now manipulate s, calculate its statistical properties and graph its 
> probability distribution. Fifty integer values is not very interesting but 
> the problems I am studying have thousands of values and very different 
> weights. How do I do this in PDL? I have PDL::Stats::Basic and 
> PDL::Stats::Distr installed along with PGPLOT but it's generating this basic 
> data that has me stumped.
> 
> Thanks and regards,
> 
> Will Schmidt
> t.william.schm...@gmail.com <mailto:t.william.schm...@gmail.com>
> 
> 
> 
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! 
> http://sdm.link/slashdot_______________________________________________
> pdl-general mailing list
> pdl-general@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pdl-general

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
pdl-general mailing list
pdl-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdl-general

Reply via email to