Stable random distribution numbers?

How to generate random numbers with stable distribution in C #? The Random class has a uniform distribution. Many other codes on the Internet show are normal distribution. But we need a stable distribution which means infinite dispersion, aka the distribution of fat in the tail.

The reason is to generate realistic stock prices. In the real world, huge price swings are much more likely than in normal distributions.

Does anyone know C # code to convert Random class data into a stable distribution?

Edit: Hmmm. Accurate distribution is less critical than guaranteeing that it will arbitrarily generate a huge sigma of at least 20 sigma. We want to test a trading strategy for stability in the true distribution of fat with tail, which also affects stock market prices.

I just read about ZipFian and Kuchy because of the comments. As I have to choose, release the Cauchy distribution, but I will also try to compare ZipFian.

+4
source share
4 answers

In general, the method:

  • Choose a stable fat distribution. Say the Cauchy distribution.

  • See the quantile function of the selected distribution.

For the Cauchy distribution, this will be p --> peak + scale * tan( pi * (p - 0.5) ) .

  • And now you have a way to convert evenly distributed random numbers to random numbers distributed by Cauchy.

Make sense? Cm.

http://en.wikipedia.org/wiki/Inverse_transform_sampling

for details.

Caveat: It's been a long time since I took the statistics.

UPDATE:

I liked this question so much, I just wrote it on the blog: see

http://ericlippert.com/2012/02/21/generating-random-non-uniform-data/

In my article on exploring some interesting examples of Zipfian distributions, you can find it here:

http://blogs.msdn.com/b/ericlippert/archive/2010/12/07/10100227.aspx

+6
source

If you are interested in using the Zipfian distribution (which is often used when modeling processes from sciences or social domains), you do something in accordance with:

  • Choose k (skew) to spread
  • Precommute cumulative distribution domain (this is just optimization)
  • Creating random values ​​for distribution by finding the closest value from the domain

Code example:

 List<int> domain = Enumerable.Range(0,1000); // generate your domain double skew = 0.37; // select a skew appropriate to your domain double sigma = domain.Aggregate(0.0d, (z,x) => x + 1.0 / Math.Pow(z+1, skew)); List<double> cummDist = domain.Select( x => domain.Aggregate(0.0d, (z,y) => z + 1.0/Math.Pow(y, skew) * sigma)); 

Now you can generate random values ​​by selecting the closest value within the domain:

 Random rand = new Random(); double seek = rand.NextDouble(); int searchIndex = cummDist.BinarySearch(seek); // return the index of the closest value from the distribution domain return searchIndex < 0 ? (~searchIndex)-1 : searchIndex-1; 

You can, of course, generalize this whole process by decoupling the logic that materializes the distribution area from the process that displays and returns the value from this domain.

+1
source

I have the James Gentle Springer inscription on this subject, “Random Number Generation and Monte Carlo Methods” , kindly provided by my statistics. It discusses a stable family on page 105:

A stable family of distributions is a flexible family of usually heavy tail distributions. This family includes the normal distribution at one extreme value of one of the parameters and Cauchy at another extreme value. Chambers, Mallows, and Stuck (1976) provide a way to generate deviations from stable distributions. (Watch for some errors in the constants of the helper function D2 to evaluate (e x -1) / x.) Their method is used in the IMSL libraries. For a symmetric stable distribution, Devroye (1986) indicates that a faster method can be developed using the relationship of symmetric stability with the Feyer de la Valle Poissin distribution. Buckle (1995) shows how to simulate sustainable distribution parameters driven by data.

Deviation from the overall sustainable distribution is difficult. If you need to do this, I would recommend a library such as IMSL. I do not advise you to try this on your own.

However, if you are looking for a specific distribution in a stable family, for example. Cauchy, then you can use the method described by Eric, known as the integral transformation of probability . As long as you can write the inverse distribution function in closed form, you can use this approach.

0
source

The following C # code generates a random number after a stable distribution, taking into account the form parameters alpha and beta . I will unsubscribe it in the public domain under Creative Commons Zero.

 public static double StableDist(Random rand, double alpha, double beta){ if(alpha<=0 || alpha>2 || beta<-1 || beta>1) throw new ArgumentException(); var halfpi=Math.PI*0.5; var unif=NextDouble(rand); while(unif == 0.0)unif=NextDouble(rand); unif=(unif - 0.5) * Math.PI; // Cauchy special case if(alpha==1 && beta==0) return Math.Tan(unif); var expo=-Math.Log(1.0 - NextDouble(rand)); var c=Math.Cos(unif); if(alpha == 1){ var s=Math.Sin(unif); return 2.0*((unif*beta+halfpi)*s/c - beta * Math.Log(halfpi*expo*c/( unif*beta+halfpi)))/Math.PI; } var z=-Math.Tan(halfpi*alpha)*beta; var ug=unif+Math.Atan(-z)/alpha; var cpow=Math.Pow(c, -1.0 / alpha); return Math.Pow(1.0+z*z, 1.0 / (2*alpha))* (Math.Sin(alpha*ug)*cpow)* Math.Pow(Math.Cos(unif-alpha*ug)/expo, (1.0-alpha) / alpha); } private static double NextDouble(Random rand){ // The default NextDouble implementation in .NET (see // https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/shared/System/Random.cs) // is very problematic: // - It generates a random number 0 or greater and less than 2^31-1 in a // way that very slightly biases 2^31-2. // - Then it divides that number by 2^31-1. // - The result is a number that uses roughly only 32 bits of pseudorandomness, // even though `double` has 53 bits in its significand. // To alleviate some of these problems, this method generates a random 53-bit // random number and divides that by 2^53. Although this doesn't fix the bias // mentioned above (for the default System.Random), this bias may be of // negligible importance for most purposes not involving security. long x=rand.Next(0,1<<30); x<<=23; x+=rand.Next(0,1<<23); return (double)x / (double)(1L<<53); } 

In addition, I set the pseudo-code for sustainable distribution in a separate article .

0
source

Source: https://habr.com/ru/post/1397453/


All Articles