问题
Is it possible to define your own probability density function in MATLAB or Octave and use it for generating random numbers?
MATLAB and Octave have default functions like rand, randn built in to draw points at random from a uniform, or normal distributions but there seems to be no documentation of how to define my very own proability density function.
回答1:
Sampling from an arbitrary random distribution is not always trivial. For well known distributions there are tricks to implement them and most of them are implemented in stats toolbox as Oli said.
If your distribution of interest is of difficult form, there are many sampling algorithms that may help you, such as, rejection sampling, slice sampling, Metropolis–Hastings algorithm.
If your distribution is discrete, or can be approximated by a discrete distribution fairly well, then you can just do multinomial sampling using randsamp
.
回答2:
If you have the stats toolbox, you can use random(), as it has a lot of useful PDFs built-in.
回答3:
I've had to do that a few times recently, and it's not exactly an easy thing to accomplish. My favorite technique was to use Inverse transform sampling.
The idea is quite simple:
- create a cdf
- use a uniform random number generator.
- identify the RV that maps to your cdf value.
来源:https://stackoverflow.com/questions/9637703/defining-your-own-probability-density-function-in-matlab