Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
random numbers from beta distribution

Subject: random numbers from beta distribution

From: Ulrik Nash

Date: 8 Mar, 2010 09:55:24

Message: 1 of 4

Hi Everyone,

I have obtained the function code below regarding the beta distribution. Unfortunately, I am not sure I can use it for what I need. What I need is to draw n random numbers from a beta function with specified parameters. The code below does allow me to input these parameters, but how do I then go about drawing numbers randomly given these parameters?

function [cdf]=generalizedBetaCdf(x,a,b,m,n)
 
% The generalized beta distribution, cdf(x).
% x : point(s) in the distribution (point, vector or matrix)
% a : first/right shape parameter (real, positive)
% b : second/left shape parameter (real, positive)
% m : left/lower boundary of support (real)
% n : right/upper boundary of support (real)
% cdf : cumulative distribution function
%
% Type: continuous, finite support
% Support: the real interval [m,n]
% Requirements: m<n
%
% Example:
% x_min=-1; x_max=1; points=1000; x=linspace(x_min,x_max,points);
% y=generalizedBetaCdf(x,5,5,x_min,x_max); plot(x,y);
%
%assert(isreal(x)&&isnumeric(x));
%assert((numel(a)==1)&&isreal(a)&&isnumeric(a)&&(a>0));
%assert((numel(b)==1)&&isreal(b)&&isnumeric(b)&&(b>0));
%assert((numel(m)==1)&&isreal(m)&&isnumeric(m));
%assert((numel(n)==1)&&isreal(n)&&isnumeric(n));
%assert(m<n);
%assert(all(m<=x(:))&&all(x(:)<=n));
 
cdf = betainc((x-m)/(n-m),a,b);

Subject: random numbers from beta distribution

From: Ulrik Nash

Date: 8 Mar, 2010 10:37:02

Message: 2 of 4

Nevermind. There is a very useful program in the MatLab library called randdraw, which works very well.








"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hn2hic$7vt$1@fred.mathworks.com>...
> Hi Everyone,
>
> I have obtained the function code below regarding the beta distribution. Unfortunately, I am not sure I can use it for what I need. What I need is to draw n random numbers from a beta function with specified parameters. The code below does allow me to input these parameters, but how do I then go about drawing numbers randomly given these parameters?
>
> function [cdf]=generalizedBetaCdf(x,a,b,m,n)
>
> % The generalized beta distribution, cdf(x).
> % x : point(s) in the distribution (point, vector or matrix)
> % a : first/right shape parameter (real, positive)
> % b : second/left shape parameter (real, positive)
> % m : left/lower boundary of support (real)
> % n : right/upper boundary of support (real)
> % cdf : cumulative distribution function
> %
> % Type: continuous, finite support
> % Support: the real interval [m,n]
> % Requirements: m<n
> %
> % Example:
> % x_min=-1; x_max=1; points=1000; x=linspace(x_min,x_max,points);
> % y=generalizedBetaCdf(x,5,5,x_min,x_max); plot(x,y);
> %
> %assert(isreal(x)&&isnumeric(x));
> %assert((numel(a)==1)&&isreal(a)&&isnumeric(a)&&(a>0));
> %assert((numel(b)==1)&&isreal(b)&&isnumeric(b)&&(b>0));
> %assert((numel(m)==1)&&isreal(m)&&isnumeric(m));
> %assert((numel(n)==1)&&isreal(n)&&isnumeric(n));
> %assert(m<n);
> %assert(all(m<=x(:))&&all(x(:)<=n));
>
> cdf = betainc((x-m)/(n-m),a,b);

Subject: random numbers from beta distribution

From: John D'Errico

Date: 8 Mar, 2010 10:40:25

Message: 3 of 4

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hn2hic$7vt$1@fred.mathworks.com>...
> Hi Everyone,
>
> I have obtained the function code below regarding the beta distribution. Unfortunately, I am not sure I can use it for what I need. What I need is to draw n random numbers from a beta function with specified parameters. The code below does allow me to input these parameters, but how do I then go about drawing numbers randomly given these parameters?
>
> function [cdf]=generalizedBetaCdf(x,a,b,m,n)
>
> % The generalized beta distribution, cdf(x).
> % x : point(s) in the distribution (point, vector or matrix)
> % a : first/right shape parameter (real, positive)
> % b : second/left shape parameter (real, positive)
> % m : left/lower boundary of support (real)
> % n : right/upper boundary of support (real)
> % cdf : cumulative distribution function
> %
> % Type: continuous, finite support
> % Support: the real interval [m,n]
> % Requirements: m<n
> %
> % Example:
> % x_min=-1; x_max=1; points=1000; x=linspace(x_min,x_max,points);
> % y=generalizedBetaCdf(x,5,5,x_min,x_max); plot(x,y);
> %
> %assert(isreal(x)&&isnumeric(x));
> %assert((numel(a)==1)&&isreal(a)&&isnumeric(a)&&(a>0));
> %assert((numel(b)==1)&&isreal(b)&&isnumeric(b)&&(b>0));
> %assert((numel(m)==1)&&isreal(m)&&isnumeric(m));
> %assert((numel(n)==1)&&isreal(n)&&isnumeric(n));
> %assert(m<n);
> %assert(all(m<=x(:))&&all(x(:)<=n));
>
> cdf = betainc((x-m)/(n-m),a,b);

This the beta cdf. It returns a number in [0,1]. You need
an INVERSE beta cdf. Thus, start with a uniform random
number, call it U. Rand will do nicely. Then solve for the
value of x, such that the beta cdf gives you the spcified
value U.

The result will have the required distribution.

help betaincinv

HTH,
John

Subject: random numbers from beta distribution

From: Ulrik Nash

Date: 8 Mar, 2010 10:47:02

Message: 4 of 4

Sorry John, I didn't see your reply. Thank you for taking the time.

Regards,

Ulrik.

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hn2k0e$es6$1@fred.mathworks.com>...
> Nevermind. There is a very useful program in the MatLab library called randdraw, which works very well.
>
>
>
>
>
>
>
>
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hn2hic$7vt$1@fred.mathworks.com>...
> > Hi Everyone,
> >
> > I have obtained the function code below regarding the beta distribution. Unfortunately, I am not sure I can use it for what I need. What I need is to draw n random numbers from a beta function with specified parameters. The code below does allow me to input these parameters, but how do I then go about drawing numbers randomly given these parameters?
> >
> > function [cdf]=generalizedBetaCdf(x,a,b,m,n)
> >
> > % The generalized beta distribution, cdf(x).
> > % x : point(s) in the distribution (point, vector or matrix)
> > % a : first/right shape parameter (real, positive)
> > % b : second/left shape parameter (real, positive)
> > % m : left/lower boundary of support (real)
> > % n : right/upper boundary of support (real)
> > % cdf : cumulative distribution function
> > %
> > % Type: continuous, finite support
> > % Support: the real interval [m,n]
> > % Requirements: m<n
> > %
> > % Example:
> > % x_min=-1; x_max=1; points=1000; x=linspace(x_min,x_max,points);
> > % y=generalizedBetaCdf(x,5,5,x_min,x_max); plot(x,y);
> > %
> > %assert(isreal(x)&&isnumeric(x));
> > %assert((numel(a)==1)&&isreal(a)&&isnumeric(a)&&(a>0));
> > %assert((numel(b)==1)&&isreal(b)&&isnumeric(b)&&(b>0));
> > %assert((numel(m)==1)&&isreal(m)&&isnumeric(m));
> > %assert((numel(n)==1)&&isreal(n)&&isnumeric(n));
> > %assert(m<n);
> > %assert(all(m<=x(:))&&all(x(:)<=n));
> >
> > cdf = betainc((x-m)/(n-m),a,b);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us