原始数据
1.股票指数
在代码中会对收盘价做对数处理:yi=log2(X_{i+1}/X_i)
2.股票状态切换概率(股票状态参考:从时间序列到复杂网络,概率为一阶马尔科夫计算的状态转移矩阵概率)
计算代码:
# coding: utf-8
import matplotlib.pyplot as plt
import pandas as pd
import math
from datetime import datetime
import matplotlib.dates as mdates
from dateutil.parser import parse
'''
Function:probFluct VS.LogIndex Scatter
Parametter:2input file name 3output picture name 4output picture title
'''
#char time transfer to datetime type
def strTime2DateTime(time1):
time2=[]
for i in time1:
time2.append(parse(str(i)))
return time2
def loadProbFluctSquenceData():
#load stock data
timeAndProbFluctSquence=pd.read_csv('timeAndProbFluctData/timeAndProbFluctSquence_slideVisibilityGraph_NASDAQk5.csv',parse_dates=True)
probFluct=timeAndProbFluctSquence['probFluct']#y轴值
closePriceTime=timeAndProbFluctSquence['time']
closePriceTime=strTime2DateTime(closePriceTime)#x轴值
return closePriceTime,list(probFluct)
def loadLogStockData():
#load score data
stockData=pd.read_csv('data/data__NASDAQ.csv',parse_dates=True)#,index_col=0
index=stockData['close_price']#y轴值
logIndexTime=stockData['time']
logIndex=[] #y_i=log2(X_{i+1}/X_i)
for i in range(len(index)-1):
medium=math.log(float(index[i+1]/index[i]),2)
logIndex.append(medium)
logIndexTime=logIndexTime[0:-1]#delete the last time
logIndexTime=strTime2DateTime(logIndexTime)#x轴值
return logIndexTime,logIndex
def BindingData(closePriceTime,probFluct,logIndexTime,logIndex):
closePriceTimeprobFluctDict={}
count=0
for i in closePriceTime:
closePriceTimeprobFluctDict[i]=probFluct[count]
count+=1
count=0
logIndexTimelogIndexDict={}
for i in logIndexTime:
logIndexTimelogIndexDict[i]=logIndex[count]
count+=1
#get the time intersection
X_probFluct=[]
Y_logIndex=[]
timeIntersection=[val for val in closePriceTime if val in logIndexTime]
for i in timeIntersection:
X_probFluct.append(closePriceTimeprobFluctDict[i])
Y_logIndex.append(logIndexTimelogIndexDict[i])
return X_probFluct,Y_logIndex
def DrawScatter(X_probFluct,Y_logIndex):
fig = plt.figure(figsize=(18,9))
ax=fig.add_subplot(111)
ax.set_title("Scatter of ProbFluct VS. LogIndex__NASDAQk5")
ax = plt.gca()#grid setting
ax.yaxis.grid(True)#delete y axis grid
ax.xaxis.grid(True)#save x axis grid
ax.set_xlabel('ProbFluct')
ax.set_ylabel('LogIndex')
ax.scatter(X_probFluct,Y_logIndex,alpha=0.5)
#plt.savefig("outputResult/Scatter_slideVisibilityGraph_probFluctVS.LogIndex__NASDAQk5.png") #save the graph
plt.show()
if __name__=="__main__":
closePriceTime,probFluct=loadProbFluctSquenceData()
logIndexTime,logIndex=loadLogStockData()
X_probFluct,Y_logIndex=BindingData(closePriceTime,probFluct,logIndexTime,logIndex)
DrawScatter(X_probFluct,Y_logIndex)
结果图: