Chance operations & probability theory

From GenerativeArt

(Difference between revisions)
Jump to: navigation, search
(Statistical Distributions)
(Pragmatic notes on using random numbers in art)
A discreet random distribution is simply a random selection among a finite number of alternatives. Where each selection is equally likely a device such as a die is often used. But where different selections may have different probabilities a device such as a game spinner can be used, where various "slices" of the "pie" diagram have different sizes.  
A discreet random distribution is simply a random selection among a finite number of alternatives. Where each selection is equally likely a device such as a die is often used. But where different selections may have different probabilities a device such as a game spinner can be used, where various "slices" of the "pie" diagram have different sizes.  
-
{{SingleImage|imageWidthPlusTen=594|imageURL=http://www-viz.tamu.edu/courses/viza658/wiki/prob/07.jpg|caption=}}
+
 
 +
{{SingleImage|imageWidthPlusTen=680|imageURL=http://www-viz.tamu.edu/courses/viza658/wiki/prob/07.jpg|caption=Discreet Random Distributions}}
 +
 
 +
 
 +
One can imagine a number line from 0 to 1 wrapped around the edge of the spinner. Unwrapping this number line suggests an algorithm for generating arbitrary discreet distributions driven by a typical uniform random number generator. The following pseudo-code could be more general or more efficient, but provides and example nevertheless.
 +
 
 +
// There are 3 colors. Color 1 is Blue, Color 2 is
 +
// Orange, and Color 3 is Green. Rather than coding
 +
// exact probabilities relative proportions are
 +
// used in an array, and then used to calculate
 +
// the thresholds needed to make random selections.
 +
 +
n = 3              // 3 colors
 +
 +
p(1) = 2            // relative amount of blue
 +
p(2) = 3            // relative amount of orange
 +
p(3) = 5            // relative amount of green
 +
 +
// total the amounts needed to represent proportions
 +
 +
total = 0
 +
for i = 1 to n
 +
    total = total + p(i)
 +
end
 +
 +
// calculate the thresholds between 0 and 1
 +
 +
last = 0
 +
for i = 1 to n
 +
    p(i) = last + ( p(i)/ total )
 +
    last = p(i)
 +
end
 +
 +
// now randomly select a color with the given proportions
 +
 +
test = rand()            // test is between 0 & 1
 +
 +
if      test < p(1) then
 +
    color = "blue"
 +
else if test < p(2) then
 +
    color = "orange"
 +
else
 +
    color = "green
 +
end
 +
 
 +
 
 +
 
 +
<span style="font-size:larger;text-decoration:underline">Designing Arbitrary Continuous Random Distributions</span>
 +
 
 +
An arbitrary distribution of continuous values can be designed by using line segments to approximate the distribution curve, and then using a so called "Monte Carlo" method to filter candidate values yielding the wanted distribution.
 +
 
 +
 
 +
{{SingleImage|imageWidthPlusTen=594|imageURL=http://www-viz.tamu.edu/courses/viza658/wiki/prob/08.jpg|caption=Designing Arbitrary Continuous Random Distributions}}
 +
 
 +
 
 +
Here is a subroutine written as pseudo-code implementing this method. There are more efficient ways of writing this code, but this version is offered for clarity.
 +
 
 +
 +
Function randval = randf( xf, yf, n )
 +
 +
// randval is the returned value between 0 and 1.
 +
// xf and yf are vectors (one dimensional arrays) which
 +
// are the (x,y) pairs which form line segments that
 +
// form the distribution "curve". n is the length of
 +
// xf and yf, i.e. the number of end points
 +
 +
while()                        // loop "forever" (until break)
 +
    randval = rand()            // candidate value
 +
    test =    rand()            // test value
 +
 +
// find the x endpoints of the line segment that brackets randval
 +
 +
    for i = 2 to n
 +
      if fx(i) > randval then break
 +
    end           
 +
 +
// now interpolate the y value on that line segment
 +
 +
    x1 = fx(i-1)
 +
    x2 = fx(i)
 +
    y1 = fy(i-1)
 +
    y2 = fy(i)
 +
    m  = (y1-y2) / (x1-x2)
 +
    y  = (m*(x2-x1)) + y1
 +
 +
// now see if the test value is below the line segment
 +
// if yes return randval, if no repeat the loop to try again
 +
 +
    if test < y then break
 +
 +
end
 +
 +
return randval
 +
 
 +
 
 +
<span style="font-size:larger;text-decoration:underline">Generating Normal (Gaussian) Random Distributions</span>
 +
 
 +
A Gaussian distribution can be described with two numbers, the mean and the standard deviation. The shape of the distribution is always the same, but the peak (the mean) and the degree of spread (the size of the standard deviation) will be specific to the subject matter in question. For example, consider IQ as (arguably) a measure of intelligence.
 +
 
 +
{{SingleImage|imageWidthPlusTen=601|imageURL=http://www-viz.tamu.edu/courses/viza658/wiki/prob/09.gif|caption=Designing Arbitrary Continuous Random Distributions}}
 +
 
 +
 
 +
In this case the mean is 100 points and 1 standard deviation is equivalent to 15 points. In terms of distribution 1 standard deviation to either side of the mean will include about 68% of the population. So in this case 68% of the population will have an IQ between 85 and 115. The diagram above uses rounded numbers, and thus the total greater than 100%. More accurate numbers are:
== Links ==
== Links ==

Revision as of 04:25, 28 September 2009

Personal tools