Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
gaussianMixture
Advanced tools
An implementation of a Gaussian Mixture class in one dimension, that allows to fit models with an Expectation Maximization algorithm.
This module implements a 1D Gaussian Mixture class that allows to fit a distribution of points along a one-dimensional axis.
npm install gaussianMixture
var GMM = require('gaussianMixture');
Instantiate a new GMM.
Parameters
nComponents
Number number of components in the mixtureweights
Array array of weights for each component in the mixture, must sum to 1means
Array array of means for each componentvars
Array array of variances of each componentoptions
Object an object that can define the variancePrior
, separationPrior
, variancePriorRelevance
and separationPriorRelevance
.
The priors are taken into account when the GMM is optimized given some data. The relevance parameters should be non-negative numbers,
1 meaning that the prior has equal weight as the result of the optimal GMM in each EM step, 0 meaning no influence, and Infinity means a fixed variance (resp. separation).Examples
var gmm = new GMM(3, [0.3, 0.2, 0.5], [1, 2, 3], [1, 1, 0.5]);
Returns GMM a gmm object
Randomly sample from the GMM's distribution.
Parameters
nSamples
Number desired number of samplesReturns Array An array of randomly sampled numbers that follow the GMM's distribution
Given an array of data, determine their memberships for each component of the GMM.
Parameters
data
Array array of numbers representing the samples to score under the modelgaussians
Array (optional) an Array of length nComponents that contains the gaussians for the GMMReturns Array (data.length * this.nComponents) matrix with membership weights
Given a datapoint, determine its memberships for each component of the GMM.
Parameters
x
Number number representing the sample to score under the modelgaussians
Array (optional) an Array of length nComponents that contains the gaussians for the GMMReturns Array an array of length this.nComponents with membership weights, i.e the probabilities that this datapoint was drawn from the each component
Compute the log-likelihood for the GMM given data.
Parameters
Returns Number the log-likelihood
Compute the optimal GMM components given an array of data.
If options has a true flag for initialize
, the optimization will begin with a K-means++ initialization.
This allows to have a data-dependent initialization and should converge quicker and to a better model.
The initialization is agnostic to the other priors that the options might contain.
The initialize
flag is unavailable with the histogram version of this function
Parameters
data
(Array | Histogram) the data array or histogrammaxIterations
Number? maximum number of expectation-maximization steps (optional, default 200
)logLikelihoodTol
Number? tolerance for the log-likelihood
to determine if we reached the optimum (optional, default 0.0000001
)Returns Number the number of steps to reach the converged solution
Initialize the GMM given data with the K-means++ initialization algorithm. The k-means++ algorithm choses datapoints amongst the data at random, while ensuring that the chosen seeds are far from each other. The resulting seeds are returned sorted.
Parameters
data
Array array of numbers representing the samples to use to optimize the modelExamples
var gmm = new GMM(3, [0.3, .04, 0.3], [1, 5, 10]);
var data = [1.2, 1.3, 7.4, 1.4, 14.3, 15.3, 1.0, 7.2];
gmm.initialize(data); // updates the means of the GMM with the K-means++ initialization algorithm, returns something like [1.3, 7.4, 14.3]
Returns Array an array of length nComponents that contains the means for the initialization.
Return the model for the GMM as a raw JavaScript Object.
Returns Object the model, with keys nComponents
, weights
, means
, vars
.
Instantiate a GMM from an Object model and options.
Parameters
model
options
Examples
var gmm = GMM.fromModel({
nComponents: 3,
weights: [0.3, 0.2, 0.5],
means: [1, 2, 3],
vars: [1, 1, 0.5]
});
Returns GMM the GMM corresponding to the given model
Instantiate a new Histogram.
Parameters
h
Object? an object with keys 'counts' and 'bins'. Both are optional.
An observation x will be counted for the key i if bins[i][0] <= x < bins[i][1].
If bins are not specified, the bins will be corresponding to one unit in the scale of the data.
The keys of the 'counts' hash will be stringified integers. (optional, default {}
)Examples
var h = new Histogram({counts: {'a': 3, 'b': 2, 'c': 5}, bins: {'a': [0, 2], 'b': [2, 4], 'c': [4, 7]}});
var h = new Histogram({counts: {'1': 3, '2': 2, '3': 5}});
var h = new Histogram();
Returns Histogram a histogram object. It has keys 'bins' (possibly null) and 'counts'.
Add an observation to an histogram.
Parameters
x
Array observation to add tos the histogramReturns Histogram the histogram with added value.
Return a data array from a histogram.
Returns Array an array of observations derived from the histogram counts.
Return the median value for the given key, derived from the bins.
Parameters
key
Returns Number the value for the provided key.
Instantiate a new Histogram.
Parameters
data
Array? array of observations to include in the histogram.
Observations that do not correspond to any bin will be discarded. (optional, default []
)bins
Object? a map from key to range (a range being an array of two elements)
An observation x will be counted for the key i if bins[i][0] <= x < bins[i][1]
.
If not specified, the bins will be corresponding to one unit in the scale of the data. (optional, default {}
)Examples
var h = Histogram.fromData([1, 2, 2, 2, 5, 5], {A: [0, 1], B: [1, 5], C: [5, 10]});
// {bins: {A: [0, 1], B: [1, 5], C: [5, 10]}, counts: {A: 0, B: 4, C: 2}}
var h = Histogram.fromData([1, 2, 2, 2, 2.4, 2.5, 5, 5]);
// {counts: {'1': 1, '2': 4, '3': 1, '5': 2}}
Returns Histogram a histogram object It has keys 'bins' (possibly null) and 'counts'.
FAQs
An implementation of a Gaussian Mixture class in one dimension, that allows to fit models with an Expectation Maximization algorithm.
The npm package gaussianMixture receives a total of 12 weekly downloads. As such, gaussianMixture popularity was classified as not popular.
We found that gaussianMixture demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.