@keystonehq/alias-sampling
Advanced tools
Comparing version 0.1.0 to 0.1.1
type RNGFunction = () => number; | ||
export default function sample(probabilities: number[], outcomes?: any[], rng?: RNGFunction): { | ||
type AliasData = { | ||
prob: number[]; | ||
alias: number[]; | ||
}; | ||
declare const next: (aliasData: AliasData, outcomes: any[], rng: RNGFunction, numOfSamples?: number) => any | any[]; | ||
declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunction) => { | ||
next: (numOfSamples?: number) => any | any[]; | ||
}; | ||
export {}; | ||
export default sample; |
@@ -6,17 +6,14 @@ "use strict"; | ||
if (val < 0) { | ||
throw new Error('Probabilities must be positive.'); | ||
throw new Error('Probability must be a positive: p[' + p.indexOf(val) + ']=' + val); | ||
} | ||
return acc + val; | ||
}, 0); | ||
if (sum <= 0) { | ||
if (sum === 0) { | ||
throw new Error('Probability sum must be greater than zero.'); | ||
} | ||
var scaledProbabilities = p.map(function (prob) { return (prob * n) / sum; }); | ||
var aliasData = { | ||
prob: new Array(n), | ||
alias: new Array(n), | ||
}; | ||
var aliasData = { prob: new Array(n), alias: new Array(n) }; | ||
var small = []; | ||
var large = []; | ||
for (var i = 0; i < n; i++) { | ||
for (var i = n - 1; i >= 0; i--) { | ||
if (scaledProbabilities[i] < 1) { | ||
@@ -34,3 +31,3 @@ small.push(i); | ||
aliasData.alias[less] = more; | ||
scaledProbabilities[more] -= 1 - scaledProbabilities[less]; | ||
scaledProbabilities[more] = (scaledProbabilities[more] + scaledProbabilities[less]) - 1; | ||
if (scaledProbabilities[more] < 1) { | ||
@@ -43,5 +40,8 @@ small.push(more); | ||
} | ||
large.concat(small).forEach(function (g) { | ||
aliasData.prob[g] = 1; | ||
}); | ||
while (large.length > 0) { | ||
aliasData.prob[large.pop()] = 1; | ||
} | ||
while (small.length > 0) { | ||
aliasData.prob[small.pop()] = 1; | ||
} | ||
return aliasData; | ||
@@ -64,3 +64,3 @@ }; | ||
}; | ||
function sample(probabilities, outcomes, rng) { | ||
var sample = function (probabilities, outcomes, rng) { | ||
if (rng === void 0) { rng = Math.random; } | ||
@@ -70,2 +70,5 @@ if (!Array.isArray(probabilities)) { | ||
} | ||
if (probabilities.length === 0) { | ||
throw new Error('Probabilities array must not be empty.'); | ||
} | ||
var n = probabilities.length; | ||
@@ -80,3 +83,3 @@ var indexedOutcomes = outcomes !== null && outcomes !== void 0 ? outcomes : Array.from({ length: n }, function (_, i) { return i; }); | ||
}; | ||
} | ||
}; | ||
exports.default = sample; |
{ | ||
"name": "@keystonehq/alias-sampling", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A Node.js module for efficient sampling from a discrete probability distribution using the alias method.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7155
89
0