static/js/gmm.js
/**
 * Maximization step of EM algorithm.
 * @function mStep
 * @param {number[]} x - input data
 * @param {number[]} mean - means of gmm
 * @param {number[]} variance - covariance matrices of gmm
 * @param {number} k - number of clusters
 * @param {number[]} z -latent variable matrix
 * @param {number[]} weight - weight matrix of gmm
 */
Maximization step of EM algorithm.
Maximization step of EM algorithm .
function mStep(x, mean, variance, k, z, weight) {
  let m = x.length; 
  let n = x[0].length; 
  for (let j = 0; j < k; j++) {
    let c = z.map(function(a, i) {return a[j]}).reduce((acc, cur) => acc + cur);
    weight[j] = 1 / m * c;
    let mu = Array(n).fill(0);
    let sigma = Array(n).fill().map(() => Array(n).fill(0));
    for (let i = 0; i < m; i++) {
      mu = mu.map(function(a, idx) {return a + x[i].map(a => a * z[i][j])[idx]});
      sigma = sigma.map(function(a, idx){return a + cov(x[i], mean[j]).map(a => a * z[i][j])[idx]});
    }
    mean[j] = mu.map(a => a / c);
    variance[j] = sigma.map(a => a / c);
  }
}
function mStep ( x , mean , variance , k , z , weight ) { let m = x . length ; let n = x [ 0 ] . length ; for ( let j = 0 ; j < k ; j ++ ) { let c = z . map ( function ( a , i ) { return a [ j ] } ) . reduce ( ( acc , cur ) => acc + cur ) ; weight [ j ] = 1 / m * c ; let mu = Array ( n ) . fill ( 0 ) ; let sigma = Array ( n ) . fill ( ) . map ( ( ) => Array ( n ) . fill ( 0 ) ) ; for ( let i = 0 ; i < m ; i ++ ) { mu = mu . map ( function ( a , idx ) { return a + x [ i ] . map ( a => a * z [ i ] [ j ] ) [ idx ] } ) ; sigma = sigma . map ( function ( a , idx ) { return a + cov ( x [ i ] , mean [ j ] ) . map ( a => a * z [ i ] [ j ] ) [ idx ] } ) ; } mean [ j ] = mu . map ( a => a / c ) ; variance [ j ] = sigma . map ( a => a / c ) ; } }
Maximization step of EM algorithm.
Maximization step of EM algorithm .
// m samples // n dimension // column sum of latent matrix // matrix element-wise operations need to fix