{"id":"kl_divergence","title":"Kullback-Leibler divergence","family":"lock-step","definition":"reference","intro_en":"KL divergence weights each log probability ratio by the mass of the first distribution. Individual bin contributions can be negative even though the complete divergence is nonnegative. The animation retains those signed contributions and a declared direction p to q. Changing the order of the distributions generally changes the answer.","intro_zh":"KL 散度用第一个分布的概率质量加权对数概率比。单箱贡献可以为负，而完整散度非负。动画保留这些带符号贡献，并明确方向为 p 到 q；交换两分布通常改变结果。","steps_en":["Declare one shared bin support and normalize nonnegative mass to sum to one.","Weight log(p/q) by p.","Accumulate the displayed bin contributions, retaining any signed terms.","Add signed bin terms in the declared p-to-q direction."],"steps_zh":["声明共同箱支持集，将非负质量归一化为概率。","按该方法公式计算逐箱贡献。","累加所示贡献，保留可能出现的符号。","应用所示最后的根号、对数或直接求和。"],"formula":"KL(p||q)=sum_i p_i*log(p_i/q_i); natural logarithm","caveats_en":["If p is positive where q is zero, KL is infinite; both vectors here are strictly positive.","KL is asymmetric and does not obey the metric triangle inequality."],"caveats_zh":["若 p 在 q 为零处为正，KL 为无穷；本例两侧均严格为正。","KL 不对称，也不满足度量三角不等式。"],"sources":[{"label":"SciPy: entropy","url":"https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.entropy.html"}],"x":[1.0,3.0,6.0,9.0,7.0,4.0,2.0,1.0],"y":[1.0,2.0,3.0,6.0,9.0,7.0,4.0,2.0],"input_label":"Synthetic counts on eight shared bins; these bins are not timestamps.","parameters":{"probability_p":[0.030303030303030304,0.09090909090909091,0.18181818181818182,0.2727272727272727,0.21212121212121213,0.12121212121212122,0.06060606060606061,0.030303030303030304],"probability_q":[0.029411764705882353,0.058823529411764705,0.08823529411764706,0.17647058823529413,0.2647058823529412,0.20588235294117646,0.11764705882352941,0.058823529411764705],"log_base":"e","smoothing":"none","support":"eight shared illustrative bins"},"result":{"label":"divergence","value":0.11916686282452271,"detail":""},"frames":[{"phase":0,"caption":"Probability mass on shared bins","panel":{"kind":"lines","title":"Probability mass on shared bins","a":[0.030303030303030304,0.09090909090909091],"a_label":"x","b":[0.029411764705882353,0.058823529411764705],"b_label":"y","marker":1}},{"phase":0,"caption":"Probability mass on shared bins","panel":{"kind":"lines","title":"Probability mass on shared bins","a":[0.030303030303030304,0.09090909090909091,0.18181818181818182,0.2727272727272727],"a_label":"x","b":[0.029411764705882353,0.058823529411764705,0.08823529411764706,0.17647058823529413],"b_label":"y","marker":3}},{"phase":0,"caption":"Probability mass on shared bins","panel":{"kind":"lines","title":"Probability mass on shared bins","a":[0.030303030303030304,0.09090909090909091,0.18181818181818182,0.2727272727272727,0.21212121212121213,0.12121212121212122],"a_label":"x","b":[0.029411764705882353,0.058823529411764705,0.08823529411764706,0.17647058823529413,0.2647058823529412,0.20588235294117646],"b_label":"y","marker":5}},{"phase":0,"caption":"Probability mass on shared bins","panel":{"kind":"lines","title":"Probability mass on shared bins","a":[0.030303030303030304,0.09090909090909091,0.18181818181818182,0.2727272727272727,0.21212121212121213,0.12121212121212122,0.06060606060606061,0.030303030303030304],"a_label":"x","b":[0.029411764705882353,0.058823529411764705,0.08823529411764706,0.17647058823529413,0.2647058823529412,0.20588235294117646,0.11764705882352941,0.058823529411764705],"b_label":"y","marker":7}},{"phase":1,"caption":"Compute coordinate 1: Weight log(p/q) by p","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.0,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":0},"value":0.0009046352469600342,"value_label":"coordinate term","input_highlight":[0]},{"phase":1,"caption":"Compute coordinate 3: Weight log(p/q) by p","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":2},"value":0.13145457158356844,"value_label":"coordinate term","input_highlight":[2]},{"phase":1,"caption":"Compute coordinate 5: Weight log(p/q) by p","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,0.0,0.0,0.0],"a_label":"contribution","marker":4},"value":-0.046976674421774986,"value_label":"coordinate term","input_highlight":[4]},{"phase":1,"caption":"Compute coordinate 8: Weight log(p/q) by p","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,-0.020099824770008008],"a_label":"contribution","marker":7},"value":-0.020099824770008008,"value_label":"coordinate term","input_highlight":[7]},{"phase":2,"caption":"Accumulate 1 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.0,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":0},"value":0.0009046352469600342,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 2 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":1},"value":0.040479005361309627,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 3 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":2},"value":0.17193357694487807,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 4 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":3},"value":0.2906566872879268,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 5 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,0.0,0.0,0.0],"a_label":"contribution","marker":4},"value":0.24368001286615182,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 6 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,0.0,0.0],"a_label":"contribution","marker":5},"value":0.1794663371345468,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 7 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,0.0],"a_label":"contribution","marker":6},"value":0.13926668759453079,"value_label":"running sum"},{"phase":2,"caption":"Accumulate 8 of 8 coordinate terms","panel":{"kind":"bars","title":"Terms included so far","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,-0.020099824770008008],"a_label":"contribution","marker":7},"value":0.11916686282452271,"value_label":"running sum"},{"phase":3,"caption":"Add signed bin terms in the declared p-to-q direction","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,-0.020099824770008008],"a_label":"contribution","marker":0},"value":0.11916686282452271,"value_label":"final result"},{"phase":3,"caption":"Add signed bin terms in the declared p-to-q direction","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,-0.020099824770008008],"a_label":"contribution","marker":2},"value":0.11916686282452271,"value_label":"final result"},{"phase":3,"caption":"Add signed bin terms in the declared p-to-q direction","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,-0.020099824770008008],"a_label":"contribution","marker":4},"value":0.11916686282452271,"value_label":"final result"},{"phase":3,"caption":"Add signed bin terms in the declared p-to-q direction","panel":{"kind":"bars","title":"Weight log(p/q) by p","a":[0.0009046352469600342,0.03957437011434959,0.13145457158356844,0.11872311034304874,-0.046976674421774986,-0.06421367573160502,-0.040199649540016015,-0.020099824770008008],"a_label":"contribution","marker":7},"value":0.11916686282452271,"value_label":"final result"}],"animation_steps_en":["Normalize counts on one shared bin support.","Weight log(p/q) by p.","Accumulate the displayed bin contributions.","Add signed bin terms in the declared p-to-q direction."],"echotime_status":"Low-priority addition","echotime_api":""}
