Skip to content

Instantly share code, notes, and snippets.

@sliminality
Last active November 11, 2016 03:33
Show Gist options
  • Save sliminality/2e1c9e9bab0ca6de930fdd5e7def3ff7 to your computer and use it in GitHub Desktop.
Save sliminality/2e1c9e9bab0ca6de930fdd5e7def3ff7 to your computer and use it in GitHub Desktop.
EM Examples
const jsonString = '{ name: Bob, children: { son: { name: MiniBob }, daughter: { name: Clara } }, age: fourteen }';
// function stringToJSON (str) {
// const pairs = [];
// for (const c in str) {
// if (c === '{') {
// // When you see a {, start a new array of pairs
// }
// }
// }
function leafString (str) {
const pairs = [];
let currentToken = '';
let currentKey = '';
for (const c of str.substring(1)) {
// If it's a colon, save the current token as a key
if (c === ':') {
currentKey = currentToken;
currentToken = '';
}
// If it's the end of a key, save the current (key, token) pair
else if (c === ',' || c === '}') {
pairs.push([ currentKey, currentToken ]);
currentKey = '';
currentToken = '';
}
else {
currentToken += c;
}
}
return pairs;
}
leafString('{ a: apple, b: boy, c: cat }');
// stringToJSON(jsonString);
function bracketParser (str) {
const stack = [];
const tokens = [];
const results = [];
let currentToken = '';
for (const c of str) {
if (c === '[') {
stack.push(c);
tokens.push(currentToken);
currentToken = '';
} else if (c === ']') {
const left = stack.pop();
if (currentToken.length) {
results.push(currentToken);
}
currentToken = tokens.pop();
} else {
currentToken += c;
}
}
return results;
}
bracketParser('[xxx[y[zzz]y]x]');
const X = {"h":20,"c":10,"d":10}; ///_**X**_ is our **observable data**. In this case, it represents the **distribution of scores**, where\\- _h_ is the number of "A" and "B" grades//- _c_ is the number of "C" grades//- _d_ is the number of "D" grades\\In this example, _**X**_ is a proper subset of the "complete dataset" because we don't know the breakdown of "A" and "B" grades.
const { h, c, d } = X; // destructuring for convenience
let mu = 0; ///_μ_ is the **parameter underlying our model**. In this case, it governs the distribution of **grades** as follows:\\- P("A") = 0.5//- P("B") = _μ_//- P("C") = 2_μ_//- P("D") = 0.5 - 3_μ_
let b; ///_b_ is the **sufficient statistic **whose expectation we are computing. In this case, _b_ denotes the **number of students who received "B" grades**; note that knowing _b_ fully determines the rest of the data set.
const numberOfIterations = 10; ///We fix a **number of times to perform EM**. As this value grows large, the MLE of the model parameters will converge.
const iterations = []; // for logging
for (let t = 0; t < numberOfIterations; t += 1) {
// E-Step: given mu, compute expectation of b
b = (mu * h) / (0.5 + mu); ///**E-step: **given our current estimate for _μ_, compute
// Log for output
iterations.push({ t, b, mu });
// M-Step: use the b obtained in the E-Step to compute MLE for mu
// P(X, z | theta) wrt theta
mu = (b + c) / (6 * (b + c + d));
}
const finalMu = mu;
// GridArrayWidget
iterations;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment