swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: random Distributions with swarm


From: pauljohn
Subject: Re: random Distributions with swarm
Date: Mon, 30 Apr 2001 08:23:31 -0500

zze-BAUDOIN Frederic stagiaire FTRD/DTL/LAN wrote:
> 
> Hello,
> 
> I want to create a new distribution of probability with Swarm : Age
> distribution
> 
> I have, for each age interval (every 5 years) a given integer wich
> represente  the occurence in a population.
> 
> I need something like AgeRandWithMin:... WithMax...  , according to the Age
> Pyramide and between AgeMin and AgeMax
> 
> I you know a quick way to do it, ....
> 
> Thank You
> 
> _________________________________________
> 
> Frédéric BAUDOIN
>          - stagiaire -

I'm forwarding your message into swarm-support in case somebody there
had more specific advice.  If you didn't join that list yet, you should.

I have created distributions before.  I don't understand the one you
want, but here is an example of a method I use which generates a
binomial random variable.  The only difficult part is taking the output
from one of the random generators and "transforming" it into the kind of
random distribution you want.  Note I got this approach from the gsl,
("gnu scientific library") and maybe they have the distribution you
want.  Since the gsl is written in C, it is usually pretty easy to graft
into a Swarm model.  Just replace the random generators in gsl with
swarm ones...


//Paul Johnson July 13,2000.  This next method "getBinomialVariateN:
//P: is adapted from the C code of the GSL, which is available under
//the GPL with this statement:

/* randist/binomial.c
 *
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* The binomial distribution has the form,
   prob(k) =  n!/(k!(n-k)!) *  p^k (1-p)^(n-k) for k = 0, 1, ..., n
   This is the algorithm from Knuth */


-(int) getBinomialVariateN: (int)  n P: (double) p
{
  unsigned int i, a, b, k = 0;
  while (n > 10)        /* This parameter is tunable */
    {
      double X;
      a = 1 + (n / 2);
      b = 1 + n - a;
      X = [self getBetaVariateAlpha1: a Alpha2: b];
      if (X >= p)
        {
          n = a - 1;
          p /= X;
        }
      else
        {
          k += a;
          n = b - 1;
          p = (p - X) / (1 - X);
        }
    }
  for (i = 0; i < n; i++)
    {
      double u = [uniformDblRand getDoubleWithMin: 0 withMax: 1];
      if (u < p)
        k++;
    }
  return k;
}


-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]