gaussianMixture
Advanced tools
Comparing version 0.2.0 to 0.3.0
15
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var gaussian = require('gaussian'); | ||
var barycenter = require('./utilities/barycenter'); | ||
var _ = require('underscore'); | ||
@@ -88,2 +89,3 @@ | ||
var memberships = this.memberships(data); | ||
var alpha; | ||
@@ -106,2 +108,13 @@ // Update the mixture weights | ||
} | ||
// If there is a separation prior: | ||
if (this.options.separationPrior && this.options.priorRelevance) { | ||
var separationPrior = this.options.separationPrior; | ||
var priorMeans = _.range(this.nComponents).map(function (a) { return (a * separationPrior); }); | ||
var priorCenter = barycenter(priorMeans, this.weights); | ||
var center = barycenter(this.means, this.weights); | ||
for (k = 0; k < this.nComponents; k++) { | ||
alpha = this.weights[k] / (this.weights[k] + this.options.priorRelevance); | ||
this.means[k] = center + alpha * (this.means[k] - center) + (1 - alpha) * (priorMeans[k] - priorCenter); | ||
} | ||
} | ||
@@ -117,3 +130,3 @@ // Update the mixture variances | ||
if (this.options.variancePrior && this.options.priorRelevance) { | ||
var alpha = this.weights[k] / (this.weights[k] + this.options.priorRelevance); | ||
alpha = this.weights[k] / (this.weights[k] + this.options.priorRelevance); | ||
this.vars[k] = alpha * this.vars[k] + (1 - alpha) * this.options.variancePrior; | ||
@@ -120,0 +133,0 @@ } |
{ | ||
"name": "gaussianMixture", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "An implementation of a Gaussian Mixture class in one dimension, that allows to fit models with an Expectation Maximization algorithm.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,2 +31,3 @@ # Gaussian Mixture | ||
variancePrior: // Float, | ||
separationPrior: // Float, | ||
priorRelevance: // Positive float. | ||
@@ -40,2 +41,4 @@ }; | ||
Similarly, the separation prior allows you to define a prior for the difference between consecutive gaussian mixture means. This prior is mixed with the sale `options.priorRelevance` score. | ||
The mixing weight `alpha` for component `i` is `alpha = weights[i] / (weights[i] + options.priorRelevance)`. |
@@ -133,1 +133,29 @@ 'use strict'; | ||
}); | ||
test('Separation prior', function (t) { | ||
t.plan(3); | ||
var options = { | ||
separationPrior: 3, | ||
priorRelevance: 0.01 | ||
}; | ||
var options2 = { | ||
separationPrior: 3, | ||
priorRelevance: 1 | ||
}; | ||
var options3 = { | ||
separationPrior: 3, | ||
priorRelevance: 1000000 | ||
}; | ||
var gmm = new GMM(3, undefined, [-1, 13, 25], [1, 1, 1], options); | ||
var gmm2 = new GMM(3, undefined, [-1, 13, 25], [1, 1, 1], options2); | ||
var gmm3 = new GMM(3, undefined, [-1, 13, 25], [1, 1, 1], options3); | ||
gmm.optimize(data); | ||
gmm2.optimize(data); | ||
gmm3.optimize(data); | ||
var cropFloat = function (a) { return Number(a.toFixed(1)); }; | ||
t.same(gmm.means.map(cropFloat), [0.7, 10.2, 29.8]); | ||
t.same(gmm2.means.map(cropFloat), [11.6, 13.5, 17.8]); | ||
t.same(gmm3.means.map(cropFloat), [11.4, 14.4, 17.4]); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18147
11
303
43
0