买股票最先要看大盘,不过这大盘和个股之间的关联度到底有多大呢?这个问题值得考虑考虑。大家也都知道牛市的时候,所有的股票都上涨,买啥都赚钱,熊市的时候,几乎所有股票都下跌,不过到底哪些股票和大盘的关联性更强呢?很多人都说指数是可以造假的,到底指数能不能代表股市的总体趋势呢?这个问题肯定难不倒我们程序员,毕竟我们都是受过高等教育,学过概率论和数理统计的。我们先回顾一下,内容复制自山东 胡大波的《线性回归中的相关系数》一文。
对于我们来说,接下来就要计算各个股票和大盘的相关性了,按照这上面的说明,如果超过0.75则相关性很强。
不过在计算的时候,我们股票的数据需要修正一番,主要如下:
1) 股票需要考虑除权除息日的影响,因为我们计算的数据足够多,而在Daydata表中,除权除息日的股票名称都加了XD,我们很容易辨识,只要把这些数据剔除就可以了
2) 股票停牌的影响,在大盘剧烈波动的时候,很多股票停盘自救,而复盘以后,很可能会补涨或补跌,而股票每天的涨跌幅只有±10%,因此可能需要多日补涨或补跌,而这个天数如何确定。我自己是根据大盘的涨跌来确定的,如果大盘在停盘期间涨跌了25%,正常来说,这个股票的前3天的数据都是用来补涨跌的,不过我们可以做的更保守一些,直接把这个天数乘以2肯定足够了。
所需的理论知识都具备了,那剩下来就是计算了,大家看到这么复杂的公式,肯定头昏眼花了,不过好在第一我们有库,叫做Math.Net,API点击这里,第二我们有Excel,Excel有个函数CORREL,可以直接计算两个数列的相关性,说明点击这里。
为了方便,我们先把数据复制到Excel中操作,在下一节中,再计算各个股票和各个指数的相关性。
获得数据对于我们来说很容易,一个SQL 语句就出来了,我们就先计算中石油和上证指数的关系吧,SQL语句如下:
select a.日期,a.涨跌幅 中石油涨跌,b.涨跌幅 大盘涨跌 from ( select 日期,涨跌幅 from dbo.DayData where 股票代码=‘601857‘ AND 股票名称 not like ‘XD%‘) a, (select 日期,涨跌幅 from StockIndex where 股票代码=‘000001‘ ) b WHERE a.日期=b.日期
得到数据如下:
日期 中石油涨跌 大盘涨跌 2015-07-14 00:00:00 -1.9154 -1.1561 2015-07-06 00:00:00 9.9746 2.4139 2015-07-01 00:00:00 -5.2074 -5.2259 2015-06-23 00:00:00 2.8648 2.1912 2015-06-18 00:00:00 -3.9088 -3.6744 2015-06-15 00:00:00 -2.7778 -2.0006 2015-06-10 00:00:00 -1.5175 -0.1466 2015-06-05 00:00:00 1.6653 1.5361 2015-06-02 00:00:00 -0.2419 1.6938 2015-05-28 00:00:00 -8.6142 -6.5048 2015-05-25 00:00:00 2.6899 3.3537 2015-05-20 00:00:00 -0.1641 0.6505 2015-05-15 00:00:00 -2.8869 -1.5901 2015-05-12 00:00:00 0.2370 1.5607 2015-05-07 00:00:00 -3.4893 -2.7677 2015-04-24 00:00:00 -0.1499 -0.4717 2015-04-16 00:00:00 4.3304 2.7095 2015-04-03 00:00:00 -0.5877 0.9971 2015-03-24 00:00:00 -1.7797 0.0998 2015-03-16 00:00:00 1.2658 2.2649 2015-03-11 00:00:00 -0.1857 0.1470 2015-01-07 00:00:00 5.9880 0.6716 2015-01-05 00:00:00 9.9907 3.5813 2015-03-03 00:00:00 -3.4452 -2.1950 2015-02-26 00:00:00 0.9795 2.1530 2015-02-13 00:00:00 0.0000 0.9583 2015-02-10 00:00:00 1.8132 1.5014 2015-02-05 00:00:00 -2.2787 -1.1844 2015-02-02 00:00:00 -3.6082 -2.5562 2015-04-29 00:00:00 -4.3266 0.0090 2015-01-28 00:00:00 -2.8549 -1.4084 2015-01-23 00:00:00 1.5044 0.2518 2015-01-20 00:00:00 3.4091 1.8195 2015-01-15 00:00:00 8.6649 3.5383 2015-07-20 00:00:00 4.2521 0.8783 2015-07-15 00:00:00 10.0081 -3.0267 2015-07-10 00:00:00 1.7094 4.5419 2015-07-07 00:00:00 4.2275 -1.2921 2015-07-02 00:00:00 8.7523 -3.4767 2015-06-24 00:00:00 2.8721 2.4835 2015-06-19 00:00:00 -5.3390 -6.4152 2015-06-11 00:00:00 -0.5393 0.3047 2015-06-01 00:00:00 4.0268 4.7052 2015-05-29 00:00:00 -2.2951 -0.1844 2015-05-19 00:00:00 3.3927 3.1297 2015-05-11 00:00:00 2.0145 3.0354 2015-05-06 00:00:00 -3.0746 -1.6154 2015-04-28 00:00:00 -2.1843 -1.1305 2015-04-23 00:00:00 0.9077 0.3641 2015-04-20 00:00:00 -4.3348 -1.6378 2015-04-15 00:00:00 -0.3197 -1.2429 2015-04-10 00:00:00 0.5650 1.9400 2015-04-07 00:00:00 3.6318 2.5220 2015-04-02 00:00:00 0.4216 0.4065 2015-03-30 00:00:00 1.5385 2.5865 2015-03-25 00:00:00 -2.2433 -0.8312 2015-03-20 00:00:00 0.3436 0.9783 2015-03-17 00:00:00 2.5000 1.5523 2015-03-12 00:00:00 2.6047 1.7753 2015-01-06 00:00:00 -1.6821 0.0277 2015-03-04 00:00:00 -0.0915 0.5051 2015-02-27 00:00:00 0.7055 0.3621 2015-02-09 00:00:00 1.2856 0.6248 2015-02-06 00:00:00 -2.3318 -1.9328 2015-01-27 00:00:00 -3.6669 -0.8933 2015-01-19 00:00:00 -9.2063 -7.7046 2015-01-14 00:00:00 -2.8351 -0.3976 2015-07-23 00:00:00 0.2201 2.4311 2015-07-17 00:00:00 -1.7164 3.5095 2015-06-29 00:00:00 3.9886 -3.3353 2015-06-26 00:00:00 -6.9788 -7.3967 2015-06-16 00:00:00 -4.0476 -3.4675 2015-06-08 00:00:00 3.9782 2.1657 2015-06-03 00:00:00 -0.1617 -0.0112 2015-05-26 00:00:00 0.5393 2.0171 2015-05-21 00:00:00 0.4108 1.8697 2015-05-18 00:00:00 -2.6424 -0.5849 2015-05-13 00:00:00 -2.0489 -0.5785 2015-05-08 00:00:00 1.9721 2.2787 2015-05-05 00:00:00 -3.6296 -4.0567 2015-04-27 00:00:00 9.9850 3.0432 2015-04-22 00:00:00 1.7706 2.4425 2015-04-17 00:00:00 2.8440 2.2045 2015-04-14 00:00:00 -1.4184 0.3360 2015-04-09 00:00:00 -0.9592 -0.9331 2015-04-01 00:00:00 1.1083 1.6648 2015-03-27 00:00:00 -2.4187 0.2445 2015-03-19 00:00:00 -0.0858 0.1389 2015-03-09 00:00:00 1.1142 1.8888 2015-01-08 00:00:00 -1.4528 -2.3859 2015-03-06 00:00:00 0.5602 -0.2244 2015-02-16 00:00:00 -0.9839 0.5786 2015-02-11 00:00:00 -0.6233 0.5128 2015-02-03 00:00:00 4.1889 2.4488 2015-01-29 00:00:00 -2.2041 -1.3139 2015-04-30 00:00:00 -2.3341 -0.7811 2015-01-26 00:00:00 2.1061 0.9374 2015-01-21 00:00:00 7.2697 4.7449 2015-01-16 00:00:00 2.5224 1.2001 2015-01-13 00:00:00 0.1721 0.1853 2015-07-21 00:00:00 -1.8208 0.6404 2015-07-16 00:00:00 -0.8876 0.4591 2015-07-13 00:00:00 -4.2781 2.3876 2015-07-08 00:00:00 -9.0708 -5.9009 2015-07-03 00:00:00 1.2842 -5.7722 2015-06-30 00:00:00 3.4703 5.5315 2015-06-25 00:00:00 -4.2301 -3.4620 2015-06-17 00:00:00 1.5715 1.6464 2015-06-12 00:00:00 0.3873 0.8739 2015-06-09 00:00:00 -1.1253 -0.3575 2015-06-04 00:00:00 2.1053 0.7561 2015-05-27 00:00:00 2.2989 0.6275 2015-05-22 00:00:00 3.4370 2.8298 2015-05-14 00:00:00 0.3218 0.0583 2015-05-04 00:00:00 0.8215 0.8738 2015-04-21 00:00:00 1.4844 1.8151 2015-04-13 00:00:00 1.8459 2.1665 2015-04-08 00:00:00 1.9560 0.8440 2015-03-31 00:00:00 -1.2626 -1.0212 2015-03-26 00:00:00 5.8252 0.5837 2015-03-23 00:00:00 1.0274 1.9465 2015-03-18 00:00:00 1.4808 2.1255 2015-03-13 00:00:00 0.2720 0.7043 2015-03-10 00:00:00 -1.1019 -0.4948 2015-03-05 00:00:00 -1.9231 -0.9470 2015-03-02 00:00:00 -0.8757 0.7849 2015-02-25 00:00:00 1.3538 -0.5563 2015-02-17 00:00:00 0.0903 0.7616 2015-02-12 00:00:00 0.1792 0.4976 2015-02-04 00:00:00 -2.3952 -0.9604 2015-01-30 00:00:00 -2.8381 -1.5922 2015-01-22 00:00:00 -0.4728 0.5937 2015-01-12 00:00:00 -3.7283 -1.7074 2015-01-09 00:00:00 -1.1466 -0.2442
代入EXCEL,直接用CORREL,得到相关系数为0.63,那看看工商银行呢,得到相关系数为0.65。可见大盘和个股关系还是比较大的。但是大盘和谁的关系更紧密一些呢?这个我们下一节再说。