This is a collection of Julia functions to make random things.
random_unit_vector(d) returns a random d-dimensional unit vector.
random_subset creates a random subset with the following variations:
random_subset(A): create a random subset ofAwith each element included with probability 0.5.random_subset(A,k): create a randomk-element subset ofA.random_subset(n): create a random subset of1:n.random_subset(n,k): create a randomk-element subset of1:n.
random_choice is used to select a number or object at random
according to some (finite, discrete distribution). We provide two
variants:
random_choice(weights)randomly chooses a value from1ton, wherenis the number of elements inweights. The probability thatkis chosen is proportional toweights[k]. Theweightsmust be nonnegative and not all zero.random_choice(dict)choose a random keykfromdictwith weight proportional todict[k]. Thus,dictmust be of typeDict{S, T<:Real}.
- No error checking is done on the input. An error might be raised for bad input, but that's not guaranteed.
- The implementation might be improved. If the size
of the argument is small, this is efficient enough.
But if
wts(ord) has many elements, I probably should do some sort of binary search through the vector of cumulative sums.
The function histplot(x) creates a PyPlot bar chart giving a histogram
of the values in the list x. Called as histplot(x,n) creates such
a plot with n bins.
Note: This function has been moved to a separate file histplot.jl in
the src directory. I've been having some issues with PyPlot and
this function doesn't really apply to creating random things (but
rather to visualizing them).
Note: I'm just wrapping stuff found in Distributions.
Probably better just to use that package directly.
binom_rv(n,p) generates a random binomial random value. p defaults
to 0.5.
poisson_rv(lambda) returns a Poisson random value with mean lambda
(which defaults to 1.0).
exp_rv(theta) returns an exponential random value with
mean theta (which defaults to 1.0).
The RV type represents a random variable with finite support; that is,
the set of possible values produced by the random variable is finite. This
rules out continuous random variables and discrete random variables with
infinite support such as Poisson random variables.
The user needs to specify the value type of the random variable
(which needs to be a Number type) and the data type for the probabilities
(which needs to be a Real type such as Float64 or Rational{Int}).
For example, to define a random variable whose values are integers and whose probabilities are rational numbers, we do this:
julia> using SimpleRandom
julia> X = RV{Int, Rational{Int}}()
RV{Int64,Rational{Int64}} with 0 values
Now let's imagine that we want the values of X to be in the
set {1,2,3} with probabilities 1/2, 1/4, and 1/4 respectively.
We can specify this in two ways.
First, we can directly enter the probabilities like this:
julia> X = RV{Int, Rational{Int}}()
RV{Int64,Rational{Int64}} with 0 values
julia> X[1]=1//2
1//2
julia> X[2]=1//4
1//4
julia> X[3]=1//4
1//4
julia> report(X)
1 1//2
2 1//4
3 1//4
Alternatively, we can enter values and have them automatically scaled so that they add to 1.
julia> X = RV{Int, Rational{Int}}()
RV{Int64,Rational{Int64}} with 0 values
julia> X[1] = 2
2
julia> X[2] = 1
1
julia> X[3] = 1
1
julia> report(X)
1 1//2
2 1//4
3 1//4
Rescaling happens automatically any time the user/computer wants to
access the probability associated with a value. In this case, the
report function prints out the probabilities associated with each
value so the rescaling took place behind the scenes then it was invoked.
Continuing this example, if we now enter X[4]=1//2, the probabilities
no longer sum to 1, so if we request the probability associated with a value,
the rescaling takes place.
julia> X[4] = 1//2
1//2
julia> X[4]
1//3
julia> report(X)
1 1//3
2 1//6
3 1//6
4 1//3
In summary, X[v]=p assigns probability p to the value v. Retrieving
a value invokes a rescaling operation (if needed) before the value is
returned. Note that if v is a value that has not been assigned a
probability, then 0 is returned.
The following functions are provided:
E(X)returns the expected value ofX.Var(X)returns the variance ofX.length(X)returns the number of values to which probabilities have been assigned.vals(X)returns an iterator to the values associated withX.probs(X)returns an iterator to the probabilities associated with values inX.report(X)prints a table consisting of the values and their associated probabilities.random_choice(X)returns a random valuevofXat random with probabilityX[v]. This function is not efficient. Compare these timings for generating an array of ten thousand binomial random values:
julia> X = Binomial_RV(20,.5)
RV{Int64,Float64} with 21 values
julia> @time A = [ random_choice(X) for _=1:10_000 ];
0.024765 seconds (60.78 k allocations: 10.015 MiB, 83.96% compilation time)
julia> @time B = [ binom_rv(20,.5) for _=1:10_000];
0.009486 seconds (27.78 k allocations: 1.928 MiB, 91.27% compilation time)
a*Xwhereais a number creates a new random variable by multiplying the values inXbya.X+Ycreates a new random variable that represents the sum of the random variablesXandYconsidered as independent. Note that2*Xis not the same asX+X.X-Yis the difference of independentXandY.
Uniform_RV(n)creates a random variable whose values are in1:neach with probability1//n.Bernoulli_RV(p)creates a random variable whose value is0with probability1-pand1with probabilityp.Binomial(n,p)creates a random variable whose values are in0:nwith probability given by the binomial distribution. That is, the valuekhas probabilitybinomial(n,k)*p^k*(1-p)^(n-k).