Skip to content

Instantly share code, notes, and snippets.

@jlcarvalho
Created October 27, 2022 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlcarvalho/9cc15a4d4d708f00130eb8266a7b8162 to your computer and use it in GitHub Desktop.
Save jlcarvalho/9cc15a4d4d708f00130eb8266a7b8162 to your computer and use it in GitHub Desktop.
Get KL Divergence from Normal Distribution
export function getKLDivergence(d1: Array<number>, d2: Array<number>) {
const [mu1, sigma1] = d1;
const [mu2, sigma2] = d2;
const eq1 = Math.log(sigma2) / Math.log(sigma1);
const eq2 = (sigma1 ** 2 + (mu1 - mu2) ** 2) / (2 * sigma2) ** 2;
return eq1 + eq2 - 1 / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment