序列的HMM预测总是给出相同的值

时间:2018-06-12 12:50:17

标签: python time-series probability prediction hmmlearn

我可能会有一些误解,所以如果有人能帮我解决以下问题,我将非常感激:

我想训练一个HMM模型来预测时间序列的未来值(基本上与证券交易所预测的问题相同),所以我想出的是以下(使用hmm.learn库):< / p>

导入声明:

__future__ import print_function
import datetime

import numpy as np
from matplotlib import cm, pyplot as plt
from matplotlib.dates import YearLocator, MonthLocator
import warnings
with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=DeprecationWarning)
    from hmmlearn.hmm import GaussianHMM
import pandas as pd

预测功能:

def predictnext(model,data):
    max_score=0
    returnvalue = 0
    for i in np.arange(-0.5,0.5,0.01):                    
        X=np.concatenate((data[-2:],np.asarray([np.asarray([i])])))
        s=np.exp(model.score(X))
        print(str(i)+" "+str(np.exp(s)))
        if(s>max_score):
            max_score=s
            returnvalue = i
    return returnvalue 

主:

values=np.loadtxt("values.txt",usecols=(1,)) #1000 datapoints
n_obs=len(values)-1
n_feat=1
data = np.zeros((n_obs,n_feat))
for i in range(0,999):
    data[i,0] = (values[i+1]-values[i])/(values[i])
values=data
mdl=GaussianHMM(n_components=3,covariance_type='diag',n_iter=100)
mdl=mdl.fit(values)
for i in range(0,10):
    f=predictnext(mdl,values)
    values=np.concatenate([values,np.asarray([np.asarray([f])])])

代码应该做什么: 1)在一组数据点上安装HMM 2)对下一个最可能的值执行网格搜索。通过考虑序列的最后两点(在代码示例中,但并不重要)来做到这一点。 3)将获得的值附加到数据集。 4)重复

问题在于,使用功能得分获得的概率始终是-0.01的最高概率。在我看来,这个结果与我为函数提供的序列无关。

现在有人在这里做错了吗? 干杯

编辑1: 添加样本数据(希望以这种方式出现)并更正导入语句

0 207.632829889
1 606.617013222
2 975.275826654
3 1302.491133084
4 1579.367072078
5 1799.070928930
6 1956.659106380
7 2048.879064496
8 2074.065284746
9 2032.132838457
10 1924.662880730
11 1755.092835524
12 1528.938900747
13 1254.054802941
14 940.805093559
15 602.095016943
16 253.170494655
17 -88.969515357
18 -406.349299631
19 -681.083534954
20 -896.696143843
21 -1039.464037551
22 -1099.665247102
23 -1072.496060853
24 -958.519264385
25 -763.614407802
26 -498.451270758
27 -177.473671302
28 182.311337280
29 562.686212640
30 945.482796000
31 1313.631555395
32 1651.928351740
33 1947.558211675
34 2190.236429538
35 2372.152195460
36 2487.797803312
37 2533.770234061
38 2508.606015965
39 2412.712973444
40 2248.430366894
41 2020.158855971
42 1734.549586639
43 1400.704673971
44 1030.202643610
45 637.029994750
46 237.217627950
47 -151.830654215
48 -512.210337186
49 -826.636730132
50 -1079.668071470
51 -1258.946815011
52 -1356.235691310
53 -1368.160154971
54 -1296.489854582
55 -1147.940890150
56 -933.554534176
57 -667.646107776
58 -366.617342646
59 -47.716021624
60 272.106930294
61 577.088026508
62 853.326073048
63 1089.162332211
64 1275.332953074
65 1404.911414434
66 1473.146474074
67 1477.296351331
68 1416.481105946
69 1291.621349511
70 1105.486251285
71 862.793978465
72 570.419853601
73 237.540295427
74 -124.262296117
75 -501.182452118
76 -877.573872895
77 -1236.558679086
78 -1560.956637980
79 -1834.393711275
80 -2042.572198185
81 -2174.453406340
82 -2223.181147220
83 -2186.751788921
84 -2068.185379766
85 -1875.231734172
86 -1619.674498644
87 -1316.226276245
88 -981.346116954
89 -631.953547938
90 -284.313612321
91 46.792482461
92 348.690627498
93 611.047784016
94 825.986073440
95 987.972216428
96 1093.595003725
97 1141.371791946
98 1131.546135620
99 1065.987922141
100 948.166614588
101 783.140923251
102 577.642258585
103 340.090168143
104 80.549594334
105 -189.432073164
106 -457.232168261
107 -709.605738417
108 -933.410434899
109 -1116.399577416
110 -1248.075820549
111 -1320.520366705
112 -1328.979485985
113 -1272.308232180
114 -1153.049837603
115 -977.230948713
116 -753.887717300
117 -494.323570640
118 -211.316078294
119 81.731033912
120 371.552929366
121 645.607173351
122 892.577524034
123 1102.628505284
124 1267.501843436
125 1380.528824529
126 1436.546464877
127 1431.896733757
128 1364.402307308
129 1233.458554743
130 1040.216627143
131 787.810796206
132 481.718953605
133 130.009285584
134 -256.424125169
135 -663.965453100
136 -1076.499109283
137 -1475.940509110
138 -1843.161903837
139 -2159.261932781
140 -2406.903761499
141 -2571.824001552
142 -2644.003100172
143 -2618.695548764
144 -2496.881448342
145 -2285.155621306
146 -1995.106602557
147 -1642.207119963
148 -1244.482944834
149 -821.084671453
150 -390.978594927
151 28.205333833
152 420.951086733
153 774.298094008
154 1078.010649174
155 1324.601977087
156 1509.141849675
157 1629.094691528
158 1684.067835834
159 1675.675130844
160 1607.458257732
161 1484.788546353
162 1314.958931923
163 1107.079210032
164 871.971531118
165 621.978622988
166 370.509521509
167 131.513547472
168 -81.242246471
169 -254.979901038
170 -378.755071074
171 -444.275868486
172 -446.461330663
173 -383.870371888
174 -258.776798445
175 -76.962871892
176 152.757925273
177 419.290360732
178 710.055867721
179 1011.854883544
180 1311.645405483
181 1597.199861578
182 1857.631485005
183 2083.620227297
184 2267.539231038
185 2403.459290115
186 2487.014192213
187 2515.361857216
188 2487.020597863
189 2401.836190380
190 2260.993295088
191 2067.037945382
192 1824.067420652
193 1537.805273103
194 1215.709228413
195 866.989334631
196 502.480389867
197 134.386053316
198 -224.262771820
199 -560.225158734
200 -860.839559290
201 -1114.970280270
202 -1313.822144901
203 -1451.659669047
204 -1526.226683787
205 -1538.917479591
206 -1494.678786790
207 -1401.477762578
208 -1269.583569602
209 -1110.668872919
210 -936.898660744
211 -760.160756216
212 -591.311880035
213 -439.722010914
214 -312.942551906
215 -216.539769002
216 -154.128436892
217 -127.363147292
218 -136.081900854
219 -178.403565892
220 -250.856631652
221 -348.556874015
222 -465.350071328
223 -594.098386632
224 -726.991320285
225 -855.912614356
226 -972.920929687
227 -1070.638294409
228 -1142.731929323
229 -1184.298141689
230 -1192.143492444
231 -1164.980064436
232 -1103.411703293
233 -1009.853176516
234 -888.287468908
235 -743.928223971
236 -582.871444502
237 -411.665747855
238 -236.973850642
239 -65.258905930
240 97.489878748
241 245.915206720
242 375.433722172
243 482.268179511
244 563.480846959
245 616.962259488
246 641.394253593
247 636.261640687
248 601.821542532
249 539.106152262
250 449.942677955
251 336.982780888
252 203.743672214
253 54.609685674
254 -105.191978894
255 -269.674217771
256 -432.275084989
257 -586.105813311
258 -724.264076090
259 -840.207775988
260 -928.159974865
261 -983.507121233
262 -1003.146313911
263 -985.754373771
264 -931.918068817
265 -844.139696691
266 -726.676084936
267 -585.239143575
268 -426.615534094
269 -258.233703912
270 -87.700903482
271 77.578560487
272 230.788145450
273 365.906550975
274 477.935780977
275 562.985284346
276 618.331361131
277 642.447229570
278 634.997775017
279 596.808625957
280 529.835426382
281 437.091234889
282 322.632561913
283 191.493024051
284 49.563571893
285 -96.600545485
286 -240.039059380
287 -373.689026160
288 -490.688700628
289 -584.762951310
290 -650.613754331
291 -684.243794395
292 -683.217949976
293 -646.826834086
294 -576.118809856
295 -473.833743555
296 -344.211131531
297 -192.721465158
298 -25.740689816
299 149.822224331
300 326.875082016
301 498.457681775
302 658.022503463
303 799.652973893
304 918.197639582
305 1009.382286489
306 1069.860291946
307 1097.241800933
308 1090.123025921
309 1048.121025269
310 971.888886861
311 863.165790268
312 724.795117002
313 560.729100477
314 375.997552096
315 176.636092460
316 -30.456421682
317 -237.758070273
318 -437.420804554
319 -621.632431574
320 -783.080584447
321 -915.378842038
322 -1013.483436497
323 -1074.045640217
324 -1095.654925591
325 -1078.942167589
326 -1026.489636995
327 -942.648748125
328 -833.219808554
329 -705.075772844
330 -565.728411495
331 -422.879433090
332 -284.011755254
333 -156.067162211
334 -45.152003142
335 43.626418043
336 106.338605341
337 140.291258070
338 144.083767613
339 117.624004831
340 62.131673488
341 -19.892220845
342 -124.724361498
343 -247.532858319
344 -382.508538883
345 -523.076753492
346 -662.171110155
347 -792.549680948
348 -907.146482951
349 -999.461816014
350 -1063.930956836
351 -1096.246027904
352 -1093.650252346
353 -1055.124653202
354 -981.432346189
355 -875.079003003
356 -740.119387170
357 -581.932270353
358 -406.873333428
359 -221.920897900
360 -34.331620897
361 148.708011380
362 320.357800589
363 474.367288138
364 605.273713222
365 708.564502451
366 780.811457998
367 819.753550967
368 824.385789469
369 795.006533061
370 733.283648270
371 642.272778169
372 526.343514778
373 391.159164425
374 243.476802010
375 90.901691968
376 -58.439165149
377 -196.299319660
378 -314.786405266
379 -406.831848436
380 -466.648300566
381 -490.099432718
382 -474.979739830
383 -421.160820897
384 -330.579449004
385 -207.103710279
386 -56.249703306
387 115.156938800
388 299.429469528
389 488.454191796
390 674.128591050
391 848.723546236
392 1005.206294332
393 1137.482475410
394 1240.587678681
395 1310.765888935
396 1345.544795793
397 1343.795378034
398 1305.700507024
399 1232.763743039
400 1127.776037748
401 994.747320265
402 838.892898012
403 666.457943885
404 484.571514630
405 300.990293335
406 123.789832213
407 -38.972386282
408 -179.703183964
409 -291.702538460
410 -369.581046072
411 -409.629450637
412 -410.075765580
413 -371.234641182
414 -295.486738884
415 -187.136108344
416 -52.160972533
417 102.209639100
418 267.884434863
419 436.369241949
420 599.216137376
421 748.384464878
422 876.609518989
423 977.630694238
424 1046.374694896
425 1079.121606134
426 1073.512038932
427 1028.680199655
428 945.252977469
429 825.374163312
430 672.717940252
431 492.370199693
432 290.827698850
433 75.768750125
434 -144.175015165
435 -359.730030712
436 -561.454629302
437 -740.135746123
438 -887.375078697
439 -996.121990442
440 -1061.113275553
441 -1079.297107002
442 -1049.987589370
443 -974.952120931
444 -858.257147519
445 -705.954360539
446 -525.675927097
447 -326.018925799
448 -116.059460956
449 95.205717337
450 299.330467909
451 488.724692422
452 656.981571764
453 798.973511209
454 910.923991533
455 990.406949068
456 1036.218736483
457 1048.377122292
458 1027.964429107
459 977.043607874
460 898.586013170
461 796.299720279
462 674.615950931
463 538.520137579
464 393.420256721
465 244.991321166
466 98.905520203
467 -39.330875405
468 -164.647147607
469 -272.633344508
470 -359.741947383
471 -423.516580478
472 -462.628976602
473 -476.984900989
474 -467.682002072
475 -436.896931136
476 -387.772936334
477 -324.130655268
478 -250.263640798
479 -170.727065863
480 -90.051275354
481 -12.603164025
482 57.644900355
483 117.195310749
484 163.124057609
485 193.143125459
486 205.586250757
487 199.486575092
488 174.537031120
489 131.110778008
490 70.267907353
491 -6.274777423
492 -96.103290696
493 -196.187924015
494 -302.957614248
495 -412.388381718
496 -520.172862250
497 -621.886201235
498 -713.236806158
499 -790.303266083
500 -849.788114726
501 -889.252120728
502 -907.253527059
503 -903.462971473
504 -878.672947289
505 -834.691985724
506 -774.248830577
507 -700.722372662
508 -617.924685935
509 -529.844108252
510 -440.398468238
511 -353.253830392
512 -271.614560963
513 -198.143199350
514 -134.885182153
515 -83.238710274
516 -44.004072221
517 -17.364656734
518 -2.982268607
519 -0.045495482
520 -7.343722446
521 -23.371558203
522 -46.360599116
523 -74.390722307
524 -105.479896116
525 -137.656598420
526 -169.076529384
527 -198.060537014
528 -223.181787213
529 -243.321641564
530 -257.695904034
531 -265.898058092
532 -267.864119023
533 -263.873155865
534 -254.495352989
535 -240.540946523
536 -223.014450192
537 -203.030002348
538 -181.770404560
539 -160.433737894
540 -140.189545373
541 -122.152361934
542 -107.353225289
543 -96.727058566
544 -91.109760191
545 -91.221801964
546 -97.664305880
547 -110.909226576
548 -131.301799551
549 -159.026254667
550 -194.113639121
551 -236.393867141
552 -285.515062914
553 -340.905327945
554 -401.751312191
555 -467.018561383
556 -535.409593198
557 -605.435204234
558 -675.408944379
559 -743.498772696
560 -807.800685830
561 -866.370250729
562 -917.380685065
563 -959.151941032
564 -990.232490267
565 -1009.487027637
566 -1016.102589708
567 -1009.698060220
568 -990.292041528
569 -958.294285191
570 -914.488240222
571 -859.930622698
572 -795.950860096
573 -724.027111947
574 -645.727153250
575 -562.652940712
576 -476.344559062
577 -388.275047483
578 -299.785425787
579 -212.062270938
580 -126.116582883
581 -42.738342442
582 37.443362136
583 113.991499755
584 186.646510586
585 255.289155692
586 319.940351606
587 380.703410584
588 437.749395337
589 491.296044466
590 541.584787278
591 588.879273387
592 633.421404634
593 675.414714641
594 715.025750962
595 752.364739333
596 787.495373584
597 820.403677506
598 851.000115506
599 879.133617277
600 904.576476069
601 927.073634356
602 946.298717133
603 961.915673011
604 973.563285943
605 980.878518127
606 983.526941722
607 981.178590989
608 973.537472362
609 960.353636793
610 941.419340615
611 916.595963973
612 885.800190779
613 849.008378716
614 806.265132040
615 757.655612089
616 703.346819985
617 643.556162785
618 578.566610144
619 508.743839952
620 434.525716693
621 356.482152249
622 275.278945188
623 191.709503870
624 106.710285185
625 21.339658925
626 -63.186398750
627 -145.541119640
628 -224.281846327
629 -297.882567079
630 -364.782259978
631 -423.433612751
632 -472.372560435
633 -510.293275030
634 -536.126788484
635 -549.097260091
636 -548.801565924
637 -535.234105985
638 -508.836031135
639 -470.491527902
640 -421.511168410
641 -363.605928616
642 -298.800267749
643 -229.370350970
644 -157.738584409
645 -86.384397255
646 -17.743782759
647 45.898365523
648 102.464894184
649 150.151437597
650 187.490670523
651 213.369643463
652 227.057962125
653 228.214646793
654 216.896771361
655 193.545911841
656 158.962293944
657 114.294237805
658 60.993698543
659 0.763546224
660 -64.490681490
661 -132.714170416
662 -201.789005851
663 -269.590079556
664 -334.071945405
665 -393.351403166
666 -445.768425388
667 -489.972213607
668 -524.966960069
669 -550.132792063
670 -565.310925434
671 -570.745377538
672 -567.115058972
673 -555.468517601
674 -537.196343157
675 -513.944080197
676 -487.516389829
677 -459.852098705
678 -432.899538602
679 -408.522530021
680 -388.438925245
681 -374.122150859
682 -366.829791965
683 -367.495054239
684 -376.697522519
685 -394.676647788
686 -421.271908217
687 -455.979779889
688 -497.923316431
689 -545.884141831
690 -598.354593201
691 -653.543118066
692 -709.523873396
693 -764.247207063
694 -815.653172604
695 -861.796010592
696 -900.845366915
697 -931.314888536
698 -952.081202858
699 -962.423757335
700 -962.120860644
701 -951.347263204
702 -930.772743930
703 -901.442086593
704 -864.700313618
705 -822.156927773
706 -775.519245916
707 -726.589086549
708 -677.135843242
709 -628.816925042
710 -583.135584967
711 -541.295660075
712 -504.282898990
713 -472.751240684
714 -447.027946949
715 -427.146567758
716 -412.778853191
717 -403.372916140
718 -398.129791208
719 -396.064158738
720 -396.069991206
721 -396.898038110
722 -397.325702023
723 -396.130282729
724 -392.177189876
725 -384.500263818
726 -372.285774546
727 -355.022523246
728 -332.464435093
729 -304.667479664
730 -272.037363575
731 -235.240180059
732 -195.258816179
733 -153.296450409
734 -110.716427410
735 -68.983700473
736 -29.543998568
737 6.130336186
738 36.708646981
739 61.043962384
740 78.205358426
741 87.603767974
742 88.963348897
743 82.397628881
744 68.405708338
745 47.883219038
746 22.147177850
747 -7.167755746
748 -38.115270779
749 -68.539485511
750 -96.167200809
751 -118.661639977
752 -133.832995743
753 -139.678679784
754 -134.527559123
755 -117.145691101
756 -86.748492111
757 -43.168179515
758 13.199087956
759 81.374633364
760 159.785722202
761 246.402558919
762 338.714466100
763 433.910472722
764 528.943467143
765 620.675623898
766 706.017181067
767 782.035750348
768 846.037765067
769 895.737276854
770 929.285060728
771 945.394626440
772 943.319548264
773 922.946553137
774 884.756005886
775 829.857367706
776 759.963657112
777 677.337357338
778 584.728326336
779 485.298073822
780 382.482945997
781 279.870061881
782 181.030668404
783 89.336269766
784 7.798114458
785 -61.100149774
786 -115.506270709
787 -154.337514868
788 -177.307597908
789 -184.941530800
790 -178.552977993
791 -160.133954938
792 -132.300290114
793 -98.122493277
794 -61.007759979
795 -24.548277963
796 7.680931318
797 32.238208652
798 46.019249149
799 46.384335310
800 31.253855184
801 -0.727334872
802 -50.117325541
803 -116.571105053
804 -198.820856046
805 -294.705392863
806 -401.139034794
807 -514.229947707
808 -629.364680123
809 -741.417285970
810 -844.954261744
811 -934.462510737
812 -1004.707885185
813 -1050.989124410
814 -1069.441045308
815 -1057.308036376
816 -1013.075000420
817 -936.646825943
818 -829.310407443
819 -693.684876423
820 -533.576719542
821 -353.717212302
822 -159.550903428
823 43.097031834
824 248.316853209
825 450.344285426
826 643.814327402
827 823.859375576
828 986.258605291
829 1127.512577077
830 1244.864473307
831 1336.366723582
832 1400.815295233
833 1437.773885948
834 1447.551376044
835 1431.173289467
836 1390.384932026
837 1327.567005945
838 1245.729545306
839 1148.435298152
840 1039.708358695
841 923.951185054
842 805.781848455
843 689.868568187
844 580.715753469
845 482.447867315
846 398.626838489
847 331.982741873
848 284.260409500
849 256.089223024
850 246.896319738
851 254.934291847
852 277.317221756
853 310.174441572
854 348.876873339
855 388.277959917
856 423.026820800
857 447.826648239
858 457.745617694
859 448.446332104
860 416.390814144
861 359.012332863
862 274.906738404
863 163.908529312
864 27.227427805
865 -132.487940674
866 -311.089171493
867 -502.925540427
868 -700.954040139
869 -896.944416789
870 -1081.823493100
871 -1246.122304969
872 -1380.564324914
873 -1476.724960172
874 -1527.644748366
875 -1528.429845442
876 -1476.675785548
877 -1372.767412961
878 -1219.859206480
879 -1023.675767852
880 -792.117018006
881 -534.671443372
882 -261.789641383
883 15.760067350
884 287.519900181
885 543.833548083
886 776.272963739
887 977.831588419
888 1143.098930296
889 1268.288728717
890 1351.176551235
891 1391.024103119
892 1388.437626806
893 1345.262956875
894 1264.468849904
895 1150.058914980
896 1006.996526835
897 841.087636666
898 658.894888864
899 467.605797555
900 274.828773339
901 88.372883953
902 -84.087593607
903 -235.319395800
904 -358.922351197
905 -449.644847793
906 -503.704916629
907 -519.071894965
908 -495.598252646
909 -435.041321121
910 -341.067617219
911 -218.968270211
912 -75.463379642
913 81.704595229
914 244.147693370
915 403.179543636
916 550.211119752
917 677.021820305
918 776.070972871
919 840.702737523
920 865.357846166
921 845.766603511
922 779.087772283
923 664.052513222
924 501.121556199
925 292.632407453
926 42.918673787
927 -241.589904250
928 -552.356327365
929 -878.808924862
930 -1208.566204561
931 -1527.805176757
932 -1821.876498138
933 -2076.076632822
934 -2276.595659090
935 -2411.468781223
936 -2471.536827609
937 -2451.197436252
938 -2348.954397349
939 -2167.588418960
940 -1913.933739020
941 -1598.487910275
942 -1234.386968926
943 -836.635982647
944 -420.987598022
945 -2.990806223
946 402.821171077
947 783.423869618
948 1127.659554576
949 1426.451016372
950 1672.826911628
951 1861.883989789
952 1990.580539525
953 2057.638505991
954 2063.437239770
955 2009.982528983
956 1900.914874226
957 1741.510836464
958 1538.718003973
959 1301.164361338
960 1039.067191684
961 764.028948249
962 488.751900410
963 226.470154934
964 -9.669923824
965 -207.373767846
966 -355.982172416
967 -447.237365168
968 -475.930696914
969 -440.320216127
970 -342.310152689
971 -187.347382900
972 16.006511895
973 256.673908666
974 521.810491371
975 797.640134514
976 1070.255889864
977 1326.315195694
978 1553.620143218
979 1741.521011905
980 1881.163522555
981 1965.612451080
982 1989.915943262
983 1951.131756699
984 1848.362643204
985 1682.844565536
986 1457.969134960
987 1179.488894958
988 855.510517564
989 496.518232513
990 115.216612836
991 -273.945517328
992 -655.137805703
993 -1012.046374741
994 -1328.775217802
995 -1590.874748811
996 -1786.515691714
997 -1907.253697807
998 -1948.783727784
999 -1911.249544606

0 个答案:

没有答案