当前位置: 首页 > news >正文

matlab表示数据散度的统计量,matlab kl-divergence(KL散度)实现代码 | 学步园

机器上装的matlab找不到kldiv()这个函数,于是在网上搜到了,貌似这个是最好的了。而且这个在网上流传的并不多,所以在这里把它贴出来。没有污染网络资源环境的意思,从不乱转载的。

来自 matlab central

function KL = kldiv(varValue,pVect1,pVect2,varargin)

%KLDIV Kullback-Leibler or Jensen-Shannon divergence between two distributions.

%   KLDIV(X,P1,P2) returns the Kullback-Leibler divergence between two

%   distributions specified over the M variable values in vector X.  P1 is a

%   length-M vector of probabilities representing distribution 1, and P2 is a

%   length-M vector of probabilities representing distribution 2.  Thus, the

%   probability of value X(i) is P1(i) for distribution 1 and P2(i) for

%   distribution 2.  The Kullback-Leibler divergence is given by:

%

%       KL(P1(x),P2(x)) = sum[P1(x).log(P1(x)/P2(x))]

%

%   If X contains duplicate values, there will be an warning message, and these

%   values will be treated as distinct values.  (I.e., the actual values do

%   not enter into the computation, but the probabilities for the two

%   duplicate values will be considered as probabilities corresponding to

%   two unique values.)  The elements of probability vectors P1 and P2 must

%   each sum to 1 +/- .00001.

%

%   A "log of zero" warning will be thrown for zero-valued probabilities.

%   Handle this however you wish.  Adding 'eps' or some other small value

%   to all probabilities seems reasonable.  (Renormalize if necessary.)

%

%   KLDIV(X,P1,P2,'sym') returns a symmetric variant of the Kullback-Leibler

%   divergence, given by [KL(P1,P2)+KL(P2,P1)]/2.  See Johnson and Sinanovic

%   (2001).

%

%   KLDIV(X,P1,P2,'js') returns the Jensen-Shannon divergence, given by

%   [KL(P1,Q)+KL(P2,Q)]/2, where Q = (P1+P2)/2.  See the Wikipedia article

%   for "Kullback朙eibler divergence".  This is equal to 1/2 the so-called

%   "Jeffrey divergence."  See Rubner et al. (2000).

%

%   EXAMPLE:  Let the event set and probability sets be as follow:

%                X = [1 2 3 3 4]';

%                P1 = ones(5,1)/5;

%                P2 = [0 0 .5 .2 .3]' + eps;

%

%             Note that the event set here has duplicate values (two 3's). These

%             will be treated as DISTINCT events by KLDIV. If you want these to

%             be treated as the SAME event, you will need to collapse their

%             probabilities together before running KLDIV. One way to do this

%             is to use UNIQUE to find the set of unique events, and then

%             iterate over that set, summing probabilities for each instance of

%             each unique event.  Here, we just leave the duplicate values to be

%             treated independently (the default):

%                 KL = kldiv(X,P1,P2);

%                 KL =

%                      19.4899

%

%             Note also that we avoided the log-of-zero warning by adding 'eps'

%             to all probability values in P2.  We didn't need to renormalize

%             because we're still within the sum-to-one tolerance.

%

%   REFERENCES:

%   1) Cover, T.M. and J.A. Thomas. "Elements of Information Theory," Wiley,

%      1991.

%   2) Johnson, D.H. and S. Sinanovic. "Symmetrizing the Kullback-Leibler

%      distance." IEEE Transactions on Information Theory (Submitted).

%   3) Rubner, Y., Tomasi, C., and Guibas, L. J., 2000. "The Earth Mover's

%      distance as a metric for image retrieval." International Journal of

%      Computer Vision, 40(2): 99-121.

%   4) Kullback朙eibler divergence. Wikipedia, The Free Encyclopedia.

%

%   See also: MUTUALINFO, ENTROPY

if ~isequal(unique(varValue),sort(varValue)),

warning('KLDIV:duplicates','X contains duplicate values. Treated as distinct values.')

end

if ~isequal(size(varValue),size(pVect1)) || ~isequal(size(varValue),size(pVect2)),

error('All inputs must have same dimension.')

end

% Check probabilities sum to 1:

if (abs(sum(pVect1) - 1) > .00001) || (abs(sum(pVect2) - 1) > .00001),

error('Probablities don''t sum to 1.')

end

if ~isempty(varargin),

switch varargin{1},

case 'js',

logQvect = log2((pVect2+pVect1)/2);

KL = .5 * (sum(pVect1.*(log2(pVect1)-logQvect)) + ...

sum(pVect2.*(log2(pVect2)-logQvect)));

case 'sym',

KL1 = sum(pVect1 .* (log2(pVect1)-log2(pVect2)));

KL2 = sum(pVect2 .* (log2(pVect2)-log2(pVect1)));

KL = (KL1+KL2)/2;

otherwise

error(['Last argument' ' "' varargin{1} '" ' 'not recognized.'])

end

else

KL = sum(pVect1 .* (log2(pVect1)-log2(pVect2)));

end

相关文章:

  • mysql anzhaung xiangjie,GitHub - dizhaung/spring-boot-student: spring-boot-student
  • php 2m 上传 限制,解决wordpress上传文件2M限制
  • matlab画图的参数,Matlab 画图plot参数 颜色 类型
  • 谱聚类算法 matlab,SpectralClustering 谱聚类算法的matlab实现 238万源代码下载- www.pudn.com...
  • centos 查看php并发访问量,如何查看centos是否联网
  • java编程实现求素数个数,Java并行程序设计——求素数个数(Runnable接口实现)...
  • wordpress文章页饮用php文件,wordpress进阶教程之在后台文章编辑页面添加自定义面板类文件|wordpress主题定制-阿树工作室...
  • oracle与用友的差别,用友U9 PK SAP/Oracle:敢比就是赢
  • oracle监听unknow,为什么侦听的status 为UNKNOWN ?
  • oracle不能单步调试,oracle没有存储过程debug权限
  • oracle失效对象是什么意思,Oracle中无效数据库对象的处理方法
  • oracle sql优化指导,Oracle优化SQL基础
  • oracle冷迁移windows,windows下oracle冷备份
  • php模拟java aes,AES php java 互转
  • php调试日志模式,ThinkPHP调试模式与日志记录概述,thinkphp日志记录_PHP教程
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • hadoop集群管理系统搭建规划说明
  • JavaScript DOM 10 - 滚动
  • JavaScript服务器推送技术之 WebSocket
  • springboot_database项目介绍
  • Terraform入门 - 3. 变更基础设施
  • 闭包--闭包作用之保存(一)
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 前端
  • 前端性能优化--懒加载和预加载
  • 使用common-codec进行md5加密
  • 手机端车牌号码键盘的vue组件
  • 网页视频流m3u8/ts视频下载
  • 用Canvas画一棵二叉树
  • 智能合约开发环境搭建及Hello World合约
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • ​学习一下,什么是预包装食品?​
  • #includecmath
  • #前后端分离# 头条发布系统
  • (06)Hive——正则表达式
  • (1)(1.9) MSP (version 4.2)
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (一)认识微服务
  • (转)visual stdio 书签功能介绍
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • .NET BackgroundWorker
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .net 微服务 服务保护 自动重试 Polly
  • .net 无限分类
  • .net2005怎么读string形的xml,不是xml文件。
  • .net生成的类,跨工程调用显示注释
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑
  • /etc/fstab 只读无法修改的解决办法
  • [ 云计算 | AWS ] AI 编程助手新势力 Amazon CodeWhisperer:优势功能及实用技巧
  • [AIGC 大数据基础]hive浅谈