Skip to content

Commit

Permalink
Merge pull request #76 from observerly/feature/stats/PoissonDistribut…
Browse files Browse the repository at this point in the history
…edRandomNumber

feat: add PoissonDistributedRandomNumber to stats module in @observerly/skysolve
  • Loading branch information
michealroberts authored Nov 17, 2024
2 parents fbc4ff6 + b643420 commit 32d6349
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/statistics/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,36 @@ func NormalDistributedRandomNumber(mean, stdDev float64) float64 {
}

/*****************************************************************************************************************/

// Poisson generates a Poisson-distributed random number with mean lambda.
// lambda: the mean of the distribution.
func PoissonDistributedRandomNumber(lambda float64) float64 {
// If lambda is less than 0, return 0:
if lambda < 0 {
return 0
}

// If lambda is 0, return 0:
if lambda == 0 {
return 0
}

// Calculate the exponential of -lambda:
L := math.Exp(-lambda)

// Initialize k, which is the number of random numbers generated:
k := 0.0

// Initialize p, which is the product of all random numbers generated:
p := 1.0

for p > L {
k++
u := rand.Float64()
p *= u
}

return k - 1
}

/*****************************************************************************************************************/

0 comments on commit 32d6349

Please sign in to comment.