Skip to main content
The Deno 2 Release Candidate is here
Learn more
class ProbabilityObjectArray
extends Array<ProbabilityObject<K, V>>
import { ProbabilityObjectArray } from "https://deno.land/x/sptaki@1.2.0/utils/RandomUtil.d.ts";

Array of ProbabilityObjectArray which allow to randomly draw of the contained objects based on the relative probability of each of its elements. The probabilities of the contained element is not required to be normalized.

Example: po = new ProbabilityObjectArray( new ProbabilityObject("a", 5), new ProbabilityObject("b", 1), new ProbabilityObject("c", 1) ); res = po.draw(10000); // count the elements which should be distributed according to the relative probabilities res.filter(x => x==="b").reduce((sum, x) => sum + 1 , 0)

Constructors

new
ProbabilityObjectArray(
mathUtil: MathUtil,
jsonUtil: JsonUtil,
...items: ProbabilityObject<K, V>[],
)

Type Parameters

K
optional
V = undefined

Properties

private
jsonUtil
private
mathUtil

Methods

Clone this ProbabilitObjectArray

cumulativeProbability(probValues: number[]): number[]

Calculates the normalized cumulative probability of the ProbabilityObjectArray's elements normalized to 1

data(key: K): V

Return the data field of a element of the ProbabilityObjectArray

draw(
count?: number,
replacement?: boolean,
locklist?: Array<K>,
): K[]

Draw random element of the ProbabilityObject N times to return an array of N keys. Drawing can be with or without replacement

Drop an element from the ProbabilityObjectArray

filter(callbackfn: (
index: number,
array: ProbabilityObject<K, V>[],
) => any
): ProbabilityObjectArray<K, V>
maxProbability(): number

Get the maximum relative probability out of a ProbabilityObjectArray

Example: po = new ProbabilityObjectArray(new ProbabilityObject("a", 5), new ProbabilityObject("b", 1)) po.maxProbability() // returns 5

minProbability(): number

Get the minimum relative probability out of a ProbabilityObjectArray

Example: po = new ProbabilityObjectArray(new ProbabilityObject("a", 5), new ProbabilityObject("b", 1)) po.minProbability() // returns 1

probability(key: K): number

Get the relative probability of an element by its key

Example: po = new ProbabilityObjectArray(new ProbabilityObject("a", 5), new ProbabilityObject("b", 1)) po.maxProbability() // returns 5