简体   繁体   English

C ++ 11的随机数分布的自己实现

[英]Own implementation of Random Number Distribution for C++11

I need to implement my own random number distribution class in C++11, but I can't find a minimalistic implementation to get me started. 我需要在C ++ 11中实现自己的随机数分布类,但是找不到让我入门的简约实现。

I have already searched the gcc source code, but only found the header files and not the implementations of the different non-uniform distributions. 我已经搜索了gcc源代码,但只找到了头文件,而没有找到不同的非均匀分布的实现。

Can you point me to a simple yet complete example of a non-uniform distribution class in C++11 or post one here? 您能指出我一个简单但完整的C ++ 11中非均匀分布类的示例还是在此处发布一个?

I guess implementing your own distribution is nothing too exotic... 我猜想实现自己的发行没有什么太过奇特的...

You guess wrong. 你猜错了。 Luc Devroye wrote an 800 page book on the topic. 卢克·德弗罗耶(Luc Devroye)就这个话题写了800页的书 There is no single technique that works for all distributions. 没有一种适用于所有发行版的技术。 There are 4 general approaches: 一般有4种方法:

  1. Inversion - If the cumulative distribution function F X (b), -∞ < b < ∞, is a continuous and invertible function, then F X (X), the CDF applied to its own random variable, has a uniform(0,1) distribution. 求反-如果累积分布函数F X (b)-∞<b <∞是连续且可逆的函数,则应用于其随机变量的CDF F X (X)具有均匀(0,1 )分发。 Equate F X (X)=U and solve for X (if possible). 等于F X (X)= U并求解X(如果可能)。

  2. Convolution - Summing or differencing random variables yields a new distribution. 卷积-对随机变量求和或求和会产生新的分布。 For instance, the sum of two uniforms has a triangular distribution; 例如,两个制服的总和具有三角形分布; or, the sum of n independent chi-squared(1)'s yields a chi-square(n) 或者,n个独立卡方(1)的总和产生卡方(n)

  3. Composition - Some complex distributions can be built up piecewise from simpler distributions using conditional probability. 组合-使用条件概率可以从较简单的分布中逐步构建一些复杂的分布。

  4. Tricks/special relations - Leverage unique relationships between different distributions such as the fact that the square of a standard normal is a chi-squared(1) random variable; 技巧/特殊关系-利用不同分布之间的唯一关系,例如标准法线的平方是卡方(1)随机变量的事实; or that a chi-squared(2) is identical to an exponential(2). 或卡方(2)等于指数(2)。 Together with Pythagoras' thm, these two facts are at the heart of the Box-Muller method for generating normals. 连同毕达哥拉斯的thm一起,这两个事实是Box-Muller方法生成法线的核心。 Plot two independent standard normals together, and you get a 2-d vector from (0,0) which heads off in a uniform(0,2π) direction and has length sqrt(exponential(2)) . 一起绘制两个独立的标准法线,您会从(0,0)得到一个二维矢量,该矢量朝着均匀(0,2π)的方向前进,长度为sqrt(exponential(2)) Generate such a vector and convert it back to Cartesian coordinates using both sine and cosine transformations to yield the two independent normals. 生成这样的向量,并使用正弦和余弦变换将其转换回笛卡尔坐标,以产生两个独立的法线。

Devroye's book fills in the details for many "popular" distributions, but since there are an infinite number of distributions out there an exhaustive treatment would be impossible. Devroye的书填写了许多“流行”发行版的详细信息,但是由于发行版数量众多,因此不可能进行详尽的处理。

Section 4.3 on page 59 of this tutorial paper has one worked example for each of the four techniques sketched out above, and a proof of inversion on pages 60-62. 教程论文第59页的第4.3节为上面概述的四种技术中的每一种提供了一个有效的示例,并在第60-62页上提供了反转证明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM