Php 生成具有概率分布的随机数
好的,这是我的问题。我们正在考虑从一家公司购买一个数据集,以扩充我们现有的数据集。就这个问题而言,假设这个数据集用一个有机数字对地方进行排名(意味着分配给一个地方的数字与分配给另一个地方的数字没有关系)。技术范围是0到无穷大,但从我看到的样本集来看,它是0到70。根据样本,这绝对不是一个统一的分布(在10000个地方中,可能有5个地方的分数超过40分,50个地方的分数超过10分,1000个地方的分数超过1分)。在我们决定购买这台电视机之前,我们想模拟一下,这样我们就可以看到它有多有用了 所以,为了模拟它,我一直在考虑为每个地方生成一个随机数(大约150000个随机数)。但是,我也希望保持数据的精神,并保持分布相对相同(或至少相当接近)。我一整天都在绞尽脑汁想办法,结果却一无所获 我的一个想法是将随机数平方(介于0和sqrt(70)之间)。但这将有利于小于1和大于1的数字 我认为,在第一象限中,实际分布应该是双曲线的。。。我只是想知道如何将随机数的线性、偶数分布转化为双曲线分布(如果双曲线是我首先想要的) 有什么想法吗 总之,这是我想要的分布(大约):Php 生成具有概率分布的随机数,php,random,distribution,probability,Php,Random,Distribution,Probability,好的,这是我的问题。我们正在考虑从一家公司购买一个数据集,以扩充我们现有的数据集。就这个问题而言,假设这个数据集用一个有机数字对地方进行排名(意味着分配给一个地方的数字与分配给另一个地方的数字没有关系)。技术范围是0到无穷大,但从我看到的样本集来看,它是0到70。根据样本,这绝对不是一个统一的分布(在10000个地方中,可能有5个地方的分数超过40分,50个地方的分数超过10分,1000个地方的分数超过1分)。在我们决定购买这台电视机之前,我们想模拟一下,这样我们就可以看到它有多有用了 所以,为
- 40-70:0.02%-0.05%
- 10-40:0.5%-1%
- 1-10:10%-20%
- 0-1:剩余(78.95%-89.48%)
<?php
define( 'RandomGaussian', 'gaussian' ) ; // gaussianWeightedRandom()
define( 'RandomBell', 'bell' ) ; // bellWeightedRandom()
define( 'RandomGaussianRising', 'gaussianRising' ) ; // gaussianWeightedRisingRandom()
define( 'RandomGaussianFalling', 'gaussianFalling' ) ; // gaussianWeightedFallingRandom()
define( 'RandomGamma', 'gamma' ) ; // gammaWeightedRandom()
define( 'RandomGammaQaD', 'gammaQaD' ) ; // QaDgammaWeightedRandom()
define( 'RandomLogarithmic10', 'log10' ) ; // logarithmic10WeightedRandom()
define( 'RandomLogarithmic', 'log' ) ; // logarithmicWeightedRandom()
define( 'RandomPoisson', 'poisson' ) ; // poissonWeightedRandom()
define( 'RandomDome', 'dome' ) ; // domeWeightedRandom()
define( 'RandomSaw', 'saw' ) ; // sawWeightedRandom()
define( 'RandomPyramid', 'pyramid' ) ; // pyramidWeightedRandom()
define( 'RandomLinear', 'linear' ) ; // linearWeightedRandom()
define( 'RandomUnweighted', 'non' ) ; // nonWeightedRandom()
function mkseed()
{
srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff) ;
} // function mkseed()
/*
function factorial($in) {
if ($in == 1) {
return $in ;
}
return ($in * factorial($in - 1.0)) ;
} // function factorial()
function factorial($in) {
$out = 1 ;
for ($i = 2; $i <= $in; $i++) {
$out *= $i ;
}
return $out ;
} // function factorial()
*/
function random_0_1()
{
// returns random number using mt_rand() with a flat distribution from 0 to 1 inclusive
//
return (float) mt_rand() / (float) mt_getrandmax() ;
} // random_0_1()
function random_PN()
{
// returns random number using mt_rand() with a flat distribution from -1 to 1 inclusive
//
return (2.0 * random_0_1()) - 1.0 ;
} // function random_PN()
function gauss()
{
static $useExists = false ;
static $useValue ;
if ($useExists) {
// Use value from a previous call to this function
//
$useExists = false ;
return $useValue ;
} else {
// Polar form of the Box-Muller transformation
//
$w = 2.0 ;
while (($w >= 1.0) || ($w == 0.0)) {
$x = random_PN() ;
$y = random_PN() ;
$w = ($x * $x) + ($y * $y) ;
}
$w = sqrt((-2.0 * log($w)) / $w) ;
// Set value for next call to this function
//
$useValue = $y * $w ;
$useExists = true ;
return $x * $w ;
}
} // function gauss()
function gauss_ms( $mean,
$stddev )
{
// Adjust our gaussian random to fit the mean and standard deviation
// The division by 4 is an arbitrary value to help fit the distribution
// within our required range, and gives a best fit for $stddev = 1.0
//
return gauss() * ($stddev/4) + $mean;
} // function gauss_ms()
function gaussianWeightedRandom( $LowValue,
$maxRand,
$mean=0.0,
$stddev=2.0 )
{
// Adjust a gaussian random value to fit within our specified range
// by 'trimming' the extreme values as the distribution curve
// approaches +/- infinity
$rand_val = $LowValue + $maxRand ;
while (($rand_val < $LowValue) || ($rand_val >= ($LowValue + $maxRand))) {
$rand_val = floor(gauss_ms($mean,$stddev) * $maxRand) + $LowValue ;
$rand_val = ($rand_val + $maxRand) / 2 ;
}
return $rand_val ;
} // function gaussianWeightedRandom()
function bellWeightedRandom( $LowValue,
$maxRand )
{
return gaussianWeightedRandom( $LowValue, $maxRand, 0.0, 1.0 ) ;
} // function bellWeightedRandom()
function gaussianWeightedRisingRandom( $LowValue,
$maxRand )
{
// Adjust a gaussian random value to fit within our specified range
// by 'trimming' the extreme values as the distribution curve
// approaches +/- infinity
// The division by 4 is an arbitrary value to help fit the distribution
// within our required range
$rand_val = $LowValue + $maxRand ;
while (($rand_val < $LowValue) || ($rand_val >= ($LowValue + $maxRand))) {
$rand_val = $maxRand - round((abs(gauss()) / 4) * $maxRand) + $LowValue ;
}
return $rand_val ;
} // function gaussianWeightedRisingRandom()
function gaussianWeightedFallingRandom( $LowValue,
$maxRand )
{
// Adjust a gaussian random value to fit within our specified range
// by 'trimming' the extreme values as the distribution curve
// approaches +/- infinity
// The division by 4 is an arbitrary value to help fit the distribution
// within our required range
$rand_val = $LowValue + $maxRand ;
while (($rand_val < $LowValue) || ($rand_val >= ($LowValue + $maxRand))) {
$rand_val = floor((abs(gauss()) / 4) * $maxRand) + $LowValue ;
}
return $rand_val ;
} // function gaussianWeightedFallingRandom()
function logarithmic($mean=1.0, $lambda=5.0)
{
return ($mean * -log(random_0_1())) / $lambda ;
} // function logarithmic()
function logarithmicWeightedRandom( $LowValue,
$maxRand )
{
do {
$rand_val = logarithmic() ;
} while ($rand_val > 1) ;
return floor($rand_val * $maxRand) + $LowValue ;
} // function logarithmicWeightedRandom()
function logarithmic10( $lambda=0.5 )
{
return abs(-log10(random_0_1()) / $lambda) ;
} // function logarithmic10()
function logarithmic10WeightedRandom( $LowValue,
$maxRand )
{
do {
$rand_val = logarithmic10() ;
} while ($rand_val > 1) ;
return floor($rand_val * $maxRand) + $LowValue ;
} // function logarithmic10WeightedRandom()
function gamma( $lambda=3.0 )
{
$wLambda = $lambda + 1.0 ;
if ($lambda <= 8.0) {
// Use direct method, adding waiting times
$x = 1.0 ;
for ($j = 1; $j <= $wLambda; $j++) {
$x *= random_0_1() ;
}
$x = -log($x) ;
} else {
// Use rejection method
do {
do {
// Generate the tangent of a random angle, the equivalent of
// $y = tan(pi * random_0_1())
do {
$v1 = random_0_1() ;
$v2 = random_PN() ;
} while (($v1 * $v1 + $v2 * $v2) > 1.0) ;
$y = $v2 / $v1 ;
$s = sqrt(2.0 * $lambda + 1.0) ;
$x = $s * $y + $lambda ;
// Reject in the region of zero probability
} while ($x <= 0.0) ;
// Ratio of probability function to comparison function
$e = (1.0 + $y * $y) * exp($lambda * log($x / $lambda) - $s * $y) ;
// Reject on the basis of a second uniform deviate
} while (random_0_1() > $e) ;
}
return $x ;
} // function gamma()
function gammaWeightedRandom( $LowValue,
$maxRand )
{
do {
$rand_val = gamma() / 12 ;
} while ($rand_val > 1) ;
return floor($rand_val * $maxRand) + $LowValue ;
} // function gammaWeightedRandom()
function QaDgammaWeightedRandom( $LowValue,
$maxRand )
{
return round((asin(random_0_1()) + (asin(random_0_1()))) * $maxRand / pi()) + $LowValue ;
} // function QaDgammaWeightedRandom()
function gammaln($in)
{
$tmp = $in + 4.5 ;
$tmp -= ($in - 0.5) * log($tmp) ;
$ser = 1.000000000190015
+ (76.18009172947146 / $in)
- (86.50532032941677 / ($in + 1.0))
+ (24.01409824083091 / ($in + 2.0))
- (1.231739572450155 / ($in + 3.0))
+ (0.1208650973866179e-2 / ($in + 4.0))
- (0.5395239384953e-5 / ($in + 5.0)) ;
return (log(2.5066282746310005 * $ser) - $tmp) ;
} // function gammaln()
function poisson( $lambda=1.0 )
{
static $oldLambda ;
static $g, $sq, $alxm ;
if ($lambda <= 12.0) {
// Use direct method
if ($lambda <> $oldLambda) {
$oldLambda = $lambda ;
$g = exp(-$lambda) ;
}
$x = -1 ;
$t = 1.0 ;
do {
++$x ;
$t *= random_0_1() ;
} while ($t > $g) ;
} else {
// Use rejection method
if ($lambda <> $oldLambda) {
$oldLambda = $lambda ;
$sq = sqrt(2.0 * $lambda) ;
$alxm = log($lambda) ;
$g = $lambda * $alxm - gammaln($lambda + 1.0) ;
}
do {
do {
// $y is a deviate from a Lorentzian comparison function
$y = tan(pi() * random_0_1()) ;
$x = $sq * $y + $lambda ;
// Reject if close to zero probability
} while ($x < 0.0) ;
$x = floor($x) ;
// Ratio of the desired distribution to the comparison function
// We accept or reject by comparing it to another uniform deviate
// The factor 0.9 is used so that $t never exceeds 1
$t = 0.9 * (1.0 + $y * $y) * exp($x * $alxm - gammaln($x + 1.0) - $g) ;
} while (random_0_1() > $t) ;
}
return $x ;
} // function poisson()
function poissonWeightedRandom( $LowValue,
$maxRand )
{
do {
$rand_val = poisson() / $maxRand ;
} while ($rand_val > 1) ;
return floor($x * $maxRand) + $LowValue ;
} // function poissonWeightedRandom()
function binomial( $lambda=6.0 )
{
}
function domeWeightedRandom( $LowValue,
$maxRand )
{
return floor(sin(random_0_1() * (pi() / 2)) * $maxRand) + $LowValue ;
} // function bellWeightedRandom()
function sawWeightedRandom( $LowValue,
$maxRand )
{
return floor((atan(random_0_1()) + atan(random_0_1())) * $maxRand / (pi()/2)) + $LowValue ;
} // function sawWeightedRandom()
function pyramidWeightedRandom( $LowValue,
$maxRand )
{
return floor((random_0_1() + random_0_1()) / 2 * $maxRand) + $LowValue ;
} // function pyramidWeightedRandom()
function linearWeightedRandom( $LowValue,
$maxRand )
{
return floor(random_0_1() * ($maxRand)) + $LowValue ;
} // function linearWeightedRandom()
function nonWeightedRandom( $LowValue,
$maxRand )
{
return rand($LowValue,$maxRand+$LowValue-1) ;
} // function nonWeightedRandom()
function weightedRandom( $Method,
$LowValue,
$maxRand )
{
switch($Method) {
case RandomGaussian :
$rVal = gaussianWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomBell :
$rVal = bellWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomGaussianRising :
$rVal = gaussianWeightedRisingRandom( $LowValue, $maxRand ) ;
break ;
case RandomGaussianFalling :
$rVal = gaussianWeightedFallingRandom( $LowValue, $maxRand ) ;
break ;
case RandomGamma :
$rVal = gammaWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomGammaQaD :
$rVal = QaDgammaWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomLogarithmic10 :
$rVal = logarithmic10WeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomLogarithmic :
$rVal = logarithmicWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomPoisson :
$rVal = poissonWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomDome :
$rVal = domeWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomSaw :
$rVal = sawWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomPyramid :
$rVal = pyramidWeightedRandom( $LowValue, $maxRand ) ;
break ;
case RandomLinear :
$rVal = linearWeightedRandom( $LowValue, $maxRand ) ;
break ;
default :
$rVal = nonWeightedRandom( $LowValue, $maxRand ) ;
break ;
}
return $rVal;
}
?>
看看可靠性分析中使用的分布-它们往往有这些长尾巴。相对简单的可能性是P(X>X)=exp[-(X/b)^A]的威布尔分布 将你的值拟合为p(X>1)=0.1和p(X>10)=0.005,我得到a=0.36和b=0.1。这意味着P(X>40)*10000=1.6,这有点太低,但P(X>70)*10000=0.2,这是合理的 编辑
哦,要从均匀(0,1)值U生成威布尔分布随机变量,只需计算b*[-log(1-U)]^(1/a)。这是1-P(X>X)的反函数,以防我计算错误。
这种天真的方法很可能会以某种我现在看不到的方式扭曲分布。其思想是简单地迭代第一个数据集,排序并成对进行。然后在每对之间随机分配15个新数字,得到新的数组
Ruby示例,因为我不太会说PHP。希望这样一个简单的想法能够很容易地翻译成PHPnumbers=[0.1,0.1,0.12,0.13,0.15,0.17,0.3,0.4,0.42,0.6,1,3,5,7,13,19,27,42,69]
more_numbers=[]
numbers.each_cons(2) { |a,b| 15.times { more_numbers << a+rand()*(b-a) } }
more_numbers.sort!
number=[0.1,0.1,0.12,0.13,0.15,0.17,0.3,0.4,0.42,0.6,1,3,5,7,13,19,27,42,69]
更多_数字=[]
数字。每一个数字(2){a,b{15.倍{更多的数字生成遵循给定分布的随机数的最简单(但不是非常有效)的方法是一种称为
这项技术的简单解释是:创建一个完全封闭您的分发内容的框。(我们称您的分发内容为f
),然后在框中选择一个随机点(x,y)
。如果y
,则使用x
作为随机数。如果y>f(x)
,然后丢弃x
和y
并选择另一点。继续,直到您有足够的值可使用。您未拒绝的x
的值将根据f
进行分配。我找到了此统计词汇表[]。这可能会有帮助。我不太明白。你有10千个介于0和70之间的浮点数,你想分布在一组150千个浮点数上吗?@Jonas Elfström:嗯,反过来说。我想用指定的分布生成150千个随机浮点数……谢谢你的代码。不过,我试着查找了你使用的所有方法提供了,但我没有看到任何似乎适合我的模型。统计数据从来都不是我的强项。如果你能指出一个你认为可能适合的模型,我会洗耳恭听……谢谢……一个选择是尝试生成一系列值,并使用每个不同的预定义分布将它们绘制在图形上,以查看曲线的变化Wikipedia也有许多关于这些发行版的大量条目……尽管是为了你所描述的(如果我解释正确的话)如果您想要更多的上限值,请尝试GaussianWarghtDrisingRandom,如果您想要更多的下限值,请尝试GaussianWarghtDrisingRandom……虽然泊松法对于许多实际情况来说通常是一种有用的方法,好吧,我尝试了每种方法。GaussianWarghtDrisingRandom最接近,但它的下降速度仍然不够快(200,而不是5,超过40, 5700,而不是50,超过10,9500而不是1000超过1。我已经尝试CSCH,它看起来更近(因为它匹配高范围),但在中间脱落得太快。在这种情况下,使用GuangeWebDeDrand($LoValm,$Max Rand,$Mead,$STDDEV)。但是要为均值和标准差设置自己的值,或者修改gaussianwarghtedfallingrandom()中对gauss()的调用以调用gauss_ms($mean,$stddev)用你自己的平均值和标准偏差值。这可能需要一些实验……但是看看维基百科页面,看看这些参数的变化是如何影响形状的curve@MarkBaker很好的资源!!我看到这篇文章已经很老了,但是我可以问一下基于经验数据的离散分布函数是否不是b这里比理论函数更好?除非我弄错了,这不就是在一条由f(x)
定义的曲线下得到随机点吗?考虑到我的曲线看起来是双曲线,最大的点密度会在原点附近,所以生成的数字不会偏向有界框的中间