一、   排列熵算法简介:

排列熵算法(Permutation Entroy)为度量时间序列复杂性的一种方法,算法描述如下:

设一维时间序列:

采用相空间重构延迟坐标法对X中任一元素x(i)进行相空间重构,对每个采样点取其连续的m个样点,得到点x(i)的m维空间的重构向量:

则序列X的相空间矩阵为:

其中m和l分别为重构维数和延迟时间;

对x(i)的重构向量Xi各元素进行升序排列,得到:

这样得到的排列方式为

其为全排列m!中的一种,对X序列各种排列情况出现次数进行统计,计算各种排列情况出现的相对频率作为其概率p1、p2、…pk,k<=m!,计算序列归一化后的排列熵:

相对来说,排列熵的计算过程较为简洁,计算量主要为k次序列长度为m的全排序,在满足功能的前提下,窗口可以尽量的小,同时,窗口大小和延迟l值的大小选取非常重要。排列熵H的大小表征时间序列的随机程度,值越小说明该时间序列越规则,反之,该时间序列越具有随机性。在排列熵算法的基础上也可以发展出诸多使用熵值的对时间序列进行异常检测的算法

二.排列熵算法的实现

已排列窗口长度为4为例(由于涉及对数组的重复排序,该窗口长度宜设置较小,已加快计算速度),计算一段序列值得排列熵

</pre><span style="white-space:pre">		</span>private double PermutationEntropyCalCulator(double[] dArrData)<span style="white-space:pre">		</span>{<span style="white-space:pre">			</span>/*<span style="white-space:pre">			</span> *  计算一段序列值的排列熵<span style="white-space:pre">			</span> */<span style="white-space:pre">			</span>int M = 4; //窗口长度值<span style="white-space:pre">			</span>int nMPer = 24;// 大小为窗口长度的集合全排列数<span style="white-space:pre">			</span>int L = 8; //延迟<span style="white-space:pre">			</span>double[] dArrSample = new double[M];<span style="white-space:pre">			</span>int[] nArrSampleSortIndex = new int[M];<span style="white-space:pre">			</span>int[] nPermCount = new int[nMPer];<span style="white-space:pre">			</span><span style="white-space:pre">			</span>Dictionary<string, int> permlist = new Dictionary<string, int>();<span style="white-space:pre">			</span>//构建字典索性<span style="white-space:pre">			</span>permlist.Add("4321", 0);<span style="white-space:pre">			</span>permlist.Add("4312", 1);<span style="white-space:pre">			</span>permlist.Add("4231", 2);<span style="white-space:pre">			</span>permlist.Add("4213", 3);<span style="white-space:pre">			</span>permlist.Add("4123", 4);<span style="white-space:pre">			</span>permlist.Add("4132", 5);<span style="white-space:pre">			</span><span style="white-space:pre">			</span>permlist.Add("3421", 6);<span style="white-space:pre">			</span>permlist.Add("3412", 7);<span style="white-space:pre">			</span>permlist.Add("3241", 8);<span style="white-space:pre">			</span>permlist.Add("3214", 9);<span style="white-space:pre">			</span>permlist.Add("3124", 10);<span style="white-space:pre">			</span>permlist.Add("3142", 11);<span style="white-space:pre">			</span><span style="white-space:pre">			</span>permlist.Add("2341", 12);<span style="white-space:pre">			</span>permlist.Add("2314", 13);<span style="white-space:pre">			</span>permlist.Add("2431", 14);<span style="white-space:pre">			</span>permlist.Add("2413", 15);<span style="white-space:pre">			</span>permlist.Add("2143", 16);<span style="white-space:pre">			</span>permlist.Add("2134", 17);<span style="white-space:pre">			</span><span style="white-space:pre">			</span>permlist.Add("1324", 18);<span style="white-space:pre">			</span>permlist.Add("1342", 19);<span style="white-space:pre">			</span>permlist.Add("1234", 20);<span style="white-space:pre">			</span>permlist.Add("1243", 21);<span style="white-space:pre">			</span>permlist.Add("1423", 22);<span style="white-space:pre">			</span>permlist.Add("1432", 23);<span style="white-space:pre">			</span><span style="white-space:pre">						</span><span style="white-space:pre">			</span>Array.Clear(nPermCount, 0, nPermCount.Length);<span style="white-space:pre">			</span><span style="white-space:pre">			</span>int j, k;<span style="white-space:pre">			</span><span style="white-space:pre">			</span>for (j=0; j < (dArrData.Length- M*L); j++)<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>for (k=0; k<M; k++)<span style="white-space:pre">				</span>{<span style="white-space:pre">					</span>dArrSample[k] = dArrData[j+k*L];<span style="white-space:pre">				</span>}<span style="white-space:pre">				</span><span style="white-space:pre">				</span>SortIdex(dArrSample, ref nArrSampleSortIndex);//排序<span style="white-space:pre">				</span><span style="white-space:pre">				</span>string strSampleSortIndex = "";<span style="white-space:pre">				</span>for (k=0; k<M; k++)<span style="white-space:pre">				</span>{<span style="white-space:pre">					</span>strSampleSortIndex += nArrSampleSortIndex[k];<span style="white-space:pre">				</span>}<span style="white-space:pre">				</span><span style="white-space:pre">				</span>if (permlist.TryGetValue(strSampleSortIndex, out k) == false)<span style="white-space:pre">				</span>{<span style="white-space:pre">					</span>k = 0;<span style="white-space:pre">				</span>}<span style="white-space:pre">				</span><span style="white-space:pre">				</span>nPermCount[k]++;<span style="white-space:pre">			</span>}<span style="white-space:pre">			</span><span style="white-space:pre">			</span>double dPermSum = 0;<span style="white-space:pre">			</span>nMPer = 24;<span style="white-space:pre">			</span>for (j=0; j<nMPer; j++)<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>dPermSum += nPermCount[j];<span style="white-space:pre">			</span>}<span style="white-space:pre">			</span><span style="white-space:pre">			</span>double dPermutationEntropy = 0;<span style="white-space:pre">			</span>for (j=0; j<nMPer; j++)<span style="white-space:pre">			</span>{<span style="white-space:pre">				</span>if(nPermCount[j]>0)<span style="white-space:pre">				</span>{<span style="white-space:pre">				</span>   double dProb = nPermCount[j]/dPermSum;<span style="white-space:pre">				</span>   dPermutationEntropy += -dProb*Math.Log(dProb);//计算熵值<span style="white-space:pre">				</span>}<span style="white-space:pre">			</span>}<span style="white-space:pre">			</span><span style="white-space:pre">			</span>dPermutationEntropy = dPermutationEntropy/Math.Log(nMPer);<span style="white-space:pre">			</span><span style="white-space:pre">			</span>double dWeight = (dPermSum-nPermCount[0])/dPermSum;<span style="white-space:pre">			</span>dPermutationEntropy = dPermutationEntropy*dWeight;<span style="white-space:pre">			</span><span style="white-space:pre">			</span>return dPermutationEntropy;<span style="white-space:pre">		</span>}</p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><pre name="code" class="html">private void SortIdex(double[] dArrSample, ref int[] nArrSampleSortIndex)
{
/*
* 由小至大,依次排序
*
*/
if (dArrSample.Length != nArrSampleSortIndex.Length)
{
return;
} int i,j;
int nTempIndex = 0;
double dTempValue = 0;
for(i=0; i<nArrSampleSortIndex.Length; i++)
{
nArrSampleSortIndex[i] = i+1;
} for(i=0; i<dArrSample.Length; i++)
{
for(j=(dArrSample.Length-1); j>i; j--)
{
if(dArrSample[j-1]>dArrSample[j])
{
dTempValue = dArrSample[j-1];
nTempIndex = nArrSampleSortIndex[j-1]; dArrSample[j-1] = dArrSample[j];
nArrSampleSortIndex[j-1] = nArrSampleSortIndex[j]; dArrSample[j] = dTempValue;
nArrSampleSortIndex[j] = nTempIndex;
}
}
}
}

窗口长度较大的时,在小型设备上实时运行需要耗费较多的资源,不知道能否做进一步的优化呢

排列熵算法简介及c#实现的更多相关文章

  1. <算法图解>读书笔记:第1章 算法简介

    阅读书籍:[美]Aditya Bhargava◎著 袁国忠◎译.人民邮电出版社.<算法图解> 第1章 算法简介 1.2 二分查找 一般而言,对于包含n个元素的列表,用二分查找最多需要\(l ...

  2. STL所有算法简介 (转) http://www.cnblogs.com/yuehui/archive/2012/06/19/2554300.html

    STL所有算法简介 STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baid ...

  3. 排列组合算法(PHP)

    用php实现的排列组合算法.使用递归算法,效率低,胜在简单易懂.可对付元素不多的情况. //从$input数组中取$m个数的组合算法 function comb($input, $m) { if($m ...

  4. C#语法灵活运用之排列组合算法

    今天群里有朋友求一个排列组合算法,题目是给定长度,输出所有指定字母的组合. 如指定字母a.b.c.d.e.f,长度为2,则结果应为:aa.ab.ac ... ef.ff. 有朋友给出算法,很有特色: ...

  5. webrtc 的回声抵消(aec、aecm)算法简介(转)

    webrtc 的回声抵消(aec.aecm)算法简介        webrtc 的回声抵消(aec.aecm)算法主要包括以下几个重要模块:1.回声时延估计 2.NLMS(归一化最小均方自适应算法) ...

  6. AES算法简介

    AES算法简介 一. AES的结构 1.总体结构 明文分组的长度为128位即16字节,密钥长度可以为16,24或者32字节(128,192,256位).根据密钥的长度,算法被称为AES-128,AES ...

  7. LARS 最小角回归算法简介

    最近开始看Elements of Statistical Learning, 今天的内容是线性模型(第三章..这本书东西非常多,不知道何年何月才能读完了),主要是在看变量选择.感觉变量选择这一块领域非 ...

  8. AI - 机器学习常见算法简介(Common Algorithms)

    机器学习常见算法简介 - 原文链接:http://usblogs.pwc.com/emerging-technology/machine-learning-methods-infographic/ 应 ...

  9. C++11 STL算法简介

    STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库.它被容纳于C++标准程序库(C++ Standard Library)中,是ANS ...

随机推荐

  1. bzoj 1217: [HNOI2003]消防局的设立

    Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了 ...

  2. oracle 分页查询数据重复问题

    最近在做项目的时候发现一个问题,oracle 在查询分页数据的时候,有几条数据重复查询了,并且有几条数据在分页的时候消失了.百度了一下发现,ORACLE 在查询数据的时候返回的行不是固定的,他只是按照 ...

  3. Linked List Cycle II--寻找单链表中环的起始点

    题目要求 Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. ...

  4. Cisco Port-Channel 设置(链路聚合)

    Port-Channel 的在实际工作中的主要作用是将两个或多个端口捆绑成为一个虚拟通道. interface Port-channel1 description port(1/0/5-6) swit ...

  5. laravel实现支付宝支付功能

    起因 前段时间因为项目中需要实现支付宝手机网站支付功能,所以写下这篇文章以作记录,不足之处,欢迎指教. 后端框架:Laravel 5.5 业务功能 适用于商家在移动端网页应用中集成支付宝支付功能.商家 ...

  6. GC其他:引用标记-清除、复制、标记-整理的说明

    对象死亡历程 1.基本的mark&sweep是必须的,后续的都是对他的改进, 2.young代理的survivor就是使用了复制算法,避免碎片 3.还有标记整理算法(压缩),就是将存活的对象移 ...

  7. python序列化pickle/cPickle

    一.pickle/Cpickle简介 Python序列化的概念很简单.内存里面有一个数据结构,你希望将它保存下来,重用,或者发送给其他人.你会怎么做?这取决于你想要怎么保存,怎么重用,发送给谁.很多游 ...

  8. Linux下DIR,dirent,stat等结构体详解

    摘自:http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/ 最近在看Linux下文件操作相关章节,遇到了这 ...

  9. C++ substr

    函数文档:http://www.cplusplus.com/reference/string/string/substr/ /* substr(size_t pos = 0,size_t len = ...

  10. jQuery 遍历 – 同胞(siblings)

    同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...