1
$\begingroup$

Let's say I want to sample from 100 Beta distributions, and in each successive one, the first parameter increases by 1. So given a current iteration number n, I want to make a list of

RandomVariate[ BetaDistribution[n, b] ]

for all n values from 1 to 100. Is there an easy way to do this in Mathematica? I assume I would be able to find a way without needing to define 100 different distributions.

Edit 2: I changed the distribution I was talking about slightly--I'm asking this as a general question, the actual distribution I'm trying to sample from is a little more complicated but I'm hoping that if there's a solution that works for this, I can apply that solution elsewhere.

Edit: I thought this was possible with a For loop but after trying it out looks like I was wrong. Any help is appreciated

$\endgroup$
2
  • $\begingroup$ RandomVariate[NormalDistribution[0, sv], 100] + Range@100... $\endgroup$
    – ciao
    Apr 1, 2020 at 6:54
  • $\begingroup$ @ciao Sorry, I'll edit my OP, I forgot that Normal distributions can just be shifted like that. What I'm asking is if there's a more general way to do this--I want to be able to apply this to, say, the standard deviation instead of the mean, or to another distribution altogether. My example was just a simple example of what I'm looking for. $\endgroup$ Apr 1, 2020 at 7:08

1 Answer 1

0
$\begingroup$

I found what I was looking for. The example that I gave can be done (if we want to do it in one line) with:

RandomVariate[#]&/@(BetaDistribution[#,b]&/@Range[100]);

If we wanted to do the same thing for both variables, we could do something instead like

RandomVariate[#]&/@(MapThread[BetaDistribution[#1,#2]&,{Range[100],Range[100]}]);

This seems pretty slow right now for what I'm using it for, so if anyone has any optimization suggestions let me know!

$\endgroup$
2
  • $\begingroup$ RandomVariate[BetaDistribution[#, b], 1][[1]] & /@ Range[100] is about 6 times faster (although neither approach seems slow). $\endgroup$
    – JimB
    Apr 1, 2020 at 15:00
  • $\begingroup$ @JimB Thanks! It looks like the slowness was because I was using CategoricalDistribution in my code where BernoulliDistribution was just as useful and also faster. $\endgroup$ Apr 23, 2020 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.