0

我想寫一個基本的模擬(一個隊列),它依賴於生成隨機expovariates。儘管Powershell提供了一個Get-Random函數,但您可以指定min和max,但它在Python的random.expovaariate(lambd)函數附近沒有任何地方。在Powershell中,如何用指定的均值生成一個隨機變量(指數)?

據說,這是我應該遵循模型:日誌(1- $ U)/( - λ)

優秀的Python文檔有這樣一段話吧:

Exponential distribution。 lambd是1.0除以所需的平均值,它應該是非零值(該參數將被稱爲「lambda」,但它是Python中的保留字)。如果lambd爲正數,則返回值的範圍從0到正無窮大,並且從負數如果lambd爲負,無窮大爲0。「在另一種描述中,「expovariate()產生一個指數分佈,可用於模擬均勻泊松過程中的到達時間間隔或間隔時間值,例如放射性衰變速率或進入網絡服務器的請求。

帕累託或冪律,分佈符合許多可觀察到的現象,並被Chris Anderon的書The Long Tail推廣。paretovariate()函數對於模擬資源向個人的分配(財富對人,對音樂家的需求,對博客的關注等)非常有用。

我曾試着在Powershell中寫這篇文章,但是我的發行版已經關閉了。如果我把平均值設爲3,那麼我得到的結果應該緊跟我從1的平均值得出的結果。我的代碼嚴格模仿John D. Cook's SimpleRNG C# library

function GetUniform #GetUint 
{ 
    Return Get-Random -Minimum -0.00 -Maximum 1 
} 



# Get exponential random sample with specified mean 
function GetExponential_SpecMean{ 
param([double]$mean) 

    if ($mean -le 0.0) 
    { 
       Write-Host "Mean must be positive. Received $mean." 

      } 
    $a = GetExponential 
    $R = $mean * $a 
    Return $R 
} 


# Get exponential random sample with mean 1 
function GetExponential 
{ 
    $x = GetUniform 
    Return -[math]::log10(1-$x) # -Math.Log(GetUniform()); 
} 

cls 
$mean5 = 1 
$rangeBottom = 0.0 
$rangeTop = 1.0 

$j = 0 
$k = 0 
$l = 0 

    for($i=1; $i -le 1000; $i++){ 
     $a = GetExponential_SpecMean $mean5 

     if($a -le 1.0){Write-Host $a;$j++} 
     if($a -gt 1.0){Write-Host $a;$k++} 
     if(($a -gt $rangeBottom) -and ($a -le $rangeTop)){#Write-Host $a; 
     $l++} 

     Write-Host "      -> $i " 
     } 

Write-Host "One or less: $j" 
Write-Host "Greater than one: $k" 
Write-Host "Total in range between bottom $rangeBottom and top $rangeTop : $l" 

對於1的1000樣品和平均數($ mean5),我應該得到(I相信)500的結果是1.0或更小和500,其大於1.0(1:1的比例),然而我得到的比例約爲9:1,平均值爲1,比值約爲53:47,平均值爲3.

這個Stack Overflow問題有一些討論,但有一些很好的背景,但它是不是特定於Powershell:Pseudorandom Number Generator - Exponential Distribution

回答

1

我看到您使用的是[Math]::log10(),它是以10爲底的對數,並且我在鏈接中看到的所有函數都使用自然l ogarithm。您應該改用[Math]::Log()來代替log10。應該做。

相關問題