gaussianMixture

0.9.0 • Public • Published

Gaussian Mixture

Build Status

This module implements a 1D Gaussian Mixture class that allows to fit a distribution of points along a one-dimensional axis.

image

Install

npm install gaussianMixture

Require

var GMM = require('gaussianMixture');

GMM

index.js:26-37

Instantiate a new GMM.

Parameters

  • nComponents Number number of components in the mixture
  • weights Array array of weights for each component in the mixture, must sum to 1
  • means Array array of means for each component
  • vars Array array of variances of each component
  • options 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

sample

index.js:57-71

Randomly sample from the GMM's distribution.

Parameters

  • nSamples Number desired number of samples

Returns Array An array of randomly sampled numbers that follow the GMM's distribution

memberships

index.js:79-86

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 model
  • gaussians Array (optional) an Array of length nComponents that contains the gaussians for the GMM

Returns Array (data.length * this.nComponents) matrix with membership weights

membership

index.js:116-127

Given a datapoint, determine its memberships for each component of the GMM.

Parameters

  • x Number number representing the sample to score under the model
  • gaussians Array (optional) an Array of length nComponents that contains the gaussians for the GMM

Returns Array an array of length this.nComponents with membership weights, i.e the probabilities that this datapoint was drawn from the each component

logLikelihood

index.js:252-257

Compute the log-likelihood for the GMM given data.

Parameters

Returns Number the log-likelihood

optimize

index.js:345-350

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 histogram
  • maxIterations 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

index.js:428-470

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 model

Examples

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.

model

index.js:492-499

Return the model for the GMM as a raw JavaScript Object.

Returns Object the model, with keys nComponents, weights, means, vars.

fromModel

index.js:511-519

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

Histogram

index.js:533-538

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

index.js:577-587

Add an observation to an histogram.

Parameters

  • x Array observation to add tos the histogram

Returns Histogram the histogram with added value.

flatten

index.js:593-608

Return a data array from a histogram.

Returns Array an array of observations derived from the histogram counts.

value

index.js:638-645

Return the median value for the given key, derived from the bins.

Parameters

  • key

Returns Number the value for the provided key.

fromData

index.js:624-632

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'.

Package Sidebar

Install

npm i gaussianMixture

Weekly Downloads

2

Version

0.9.0

License

MIT

Last publish

Collaborators

  • benjamintd