curiousexplorer
curiousexplorer

Reputation: 1237

Defining your own probability density function in MATLAB

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.

Upvotes: 5

Views: 6209

Answers (3)

Rasman
Rasman

Reputation: 5359

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:

  1. create a cdf
  2. use a uniform random number generator.
  3. identify the RV that maps to your cdf value.

Upvotes: 1

Memming
Memming

Reputation: 1739

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.

Upvotes: 8

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

If you have the stats toolbox, you can use random(), as it has a lot of useful PDFs built-in.

Upvotes: 1

Related Questions