Generating Random Numbers From Skewed Normal Distribution
Solution 1:
For most languages, the random number generated can be dependent on an algorithm within that language, or generated randomly based on the several factors such as time, processor, seed number.
The distribution is not normal. In fact say if the function returns 5 integers, all 5 integers have a fair chance of appearing in the next function call. This is also known as uniformed distribution.
So say if you wish to produce a number (say 7) for 20% of the time, and another number (say 13) for 80% of the time, you can do an array like this:
var arr = [7,13,13,13,13];
var picked = arr[Math.floor(Math.random()*arr.length)] ;
// since Math.random() returns a float from 0.0 to 1.0
So thus 7 has a 20% chance of appearing, and 13 has 80% chance.
Solution 2:
This is one possible method:
ranges = [(10..15), (20..30)]
selector = [0, 0, 1,1,1,1,1,1,1,1] # 80:20 distribution array
# now select a range randomly
random_within_range(ranges(selector[random(10)]))
def random_within_range range
rand (range.last - range.begin - (range.exclude_end? ? 1 : 0)) + range.begin
end
Solution 3:
Most pseudo random generators built-in programming languages produce a uniform distribution, i.e. each value within the range has the same probability of being produced as any other value in the range. Indeed in some cases this requirement is part of the language standard. Some languages such as Python or R support various of the common distributions.
If the language doesn't support it, you either have to use mathematical tricks to produce other distributions such as a normal distribution from a uniform one, or you can look for third-party libraries which perform this function.
Your problem seems much simpler however since the random variable is discrete (and of the simpler type thereof, i.e binary). The trick for these is to produce a random number form the uniform distribution, in a given range, say 0 to 999, and to split this range in the proportions associated with each value, in the case at hand this would be something like :
If (RandomNumber) < 200// 20%
RandomVariable = 0Else// 80%
RandomVariable = 1
This logic can of course be applied to n discrete variables.
Solution 4:
Your question differs from your example quite a bit. So I'll answer both and you can figure out whichever answers what you're really looking for.
1) Your example (I don't know ruby or java, so bear with me)
- First generate a random number from a uniform distribution from 0 to 1, we'll call it X.
- You can then setup a if/else (i.e. if ( x < .2) {1} else {0})
2) Generating random numbers from a normal distribution with skew
- You can look into skewed distributions such as a skewed student T's distribution with high degree of freedom.
- You can also use the normal CDF and just pick off numbers that way.
- Here's a paper which discusses how to do it with multiple random numbers from a uniform distribution
- Finally, you can use a non-parametric approach which would involve kernal density estimation (I suspect you aren't looking for anything this sophisticated however).
Solution 5:
Like anybody says, pseudo-random number generator on most languages implements the uniform distribution over (0,1). If you have two responses categories (0,1) with p probability for 1, you have a Bernoulli distribution and can be emulated with
# returns 1 with p probability and 0 with (1-p) probability
def bernoulli(p)
rand()<p ? 1:0;
end
Simple as that. Skewed normal distribution is a entirely different beast, made by the 'union' of pdf and cdf of a normal distribution to create the skew. You can read Azzalini's work here. Using gem distribution, you can generate the probability density function, with
# require 'distribution'defsn_pdf(x,alpha)
sp = 2*Distribution::Normal.pdf(x)*Distribution::Normal.cdf(x*alpha)
end
Obtains the cdf is difficult, because there isn't an analytical solution, so you should integrate. To obtain random numbers from a skewed normal, you could use the acceptation-rejection algorithm.
Post a Comment for "Generating Random Numbers From Skewed Normal Distribution"