马尔科夫模型推算股票
/** 股票 隐马尔可夫模型 共3种状态 B M E 大 中 小 AMap 为状态转移概率矩阵 3*3,表示从{B M E}到{B M E}的概率 BMap 为当前字属于某种状态{B M E}的概率 * */ //有限状态 const S=['B','M','E']; const AMap={ B:0, M:0, E:0, BB:1, BM:1, BE:1, MB:1, MM:1, ME:1, EB:1, EM:1, EE:1, } const BMap={ B:0, M:1, E:0, }; const mekf={ add(numArr){ for(let i=0;i<numArr.length;i++){ this.push(numArr[i]) if(i>0){ this.pushState(numArr[i-1],numArr[i]) }else{ this.pushState(0,numArr[i]) } } }, getM(num){ let key='M' if(num>0){ key='B' }else if(num<0){ key='E' } return key; }, pushState(n0,n1){ const t0=this.getM(n0) const t1=this.getM(n1) const key=t0+t1; AMap[key]++; AMap[t0]++ }, push(num){ const key=this.getM(num) if(!BMap[num]){ BMap[num]=0; } BMap[num]++; BMap[key]++; }, getT1(n0){ const t0=this.getM(n0) //马尔可夫链条 let agl=0; let bgl=0; const obj={} const arr=[] for(let i=-10;i<=10;i++){ const t1=this.getM(i); const key=t0+t1; if(!BMap[i]){ BMap[i]=0; } const gl=(BMap[i]/BMap[t1])*(AMap[key]/AMap[t0]) if(gl>0){ agl=agl+gl; arr.push({i:i,gl:gl}) } } arr.forEach(function (item) { item.gl=Math.round(item.gl*100/agl); if(item.i>0){ bgl=bgl+item.gl } }) arr.sort(function (item1,item2) { return item2.gl-item1.gl }) console.log(bgl) return arr; } } const data=[0,-2,0,-1,0,0,0,-3,1,2,-2,1,2,1,-1,1,0,2,1,1,7,4,10,10,-2,1,0,-4,-4,3,10,-6,1,3,6,1,-2,-1,10,-2,7,9,10,-2,2,-7,3,2,2,1,-7,-3,-1] mekf.add(data) const t1=mekf.getT1(-1) console.log(t1)