forked from SaahilMadge-zz/Spring-2015-IW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandasTest.py
219 lines (175 loc) · 6.43 KB
/
pandasTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import pandas
import numpy
import matplotlib.pyplot as plt
from scipy import stats
def findPercentile(marketCap, marketCapArray):
for i in range(1,11):
percentile = numpy.percentile(marketCapArray, i*10)
if (marketCap < percentile):
return i
return 10 # if it's the highest
df = pandas.read_csv('sp500data2.csv')
permnoList = sorted(list(set(df['PERMNO'])))
# skip certain companies b/c their price data is not full
#permnoToSkip = [10942, 11896]
#permnoList = [x for x in permnoList if x not in permnoToSkip]
print permnoList
companyChanges = {}
#permnoList.pop(0) #remove nan at the beginning
print len(permnoList)
f = open('pythonlist.txt', 'w+')
#companiesToSkip = ['WBA'] # companies that shouldn't be in this data
#for company in companiesToSkip:
# permnoList.remove(company)
# marketCapArray = numpy.array([])
marketCapArray = []
marketCapDict = {}
for i in range(len(permnoList)):
# if(permnoList[i] in companiesToSkip):
# continue
compData = df[df['PERMNO'] == permnoList[i]]
marketShare = abs(list(compData['PRC'])[-5]) * abs(list(compData['SHROUT'])[-5])
# print list(compData['PRC'])[-1]
#f.write('%s:%f\n' % (permnoList[i], marketShare))
print '%s: %f' % (permnoList[i], marketShare)
marketCapArray.append(marketShare)
marketCapDict[permnoList[i]] = [marketShare]
# print marketCapArray
for i in range(1,11):
print "%d percentile: %f" % (i*10, numpy.percentile(marketCapArray, i*10))
for permno in permnoList:
marketCap = marketCapDict[permno][0]
percentile = findPercentile(marketCap, marketCapArray)
print '%s: %f\tpercentile:%d' % (permno, marketCap, percentile)
marketCapDict[permno].append(percentile)
alpha = 0.15 # percent change needed to be significant
standardDeviationArray = []
for i in range(10):
standardDeviationArray.append([])
meanReturnArray = []
for i in range(10):
meanReturnArray.append([])
def findChangeIndices(permno):
priceList = numpy.array(list(df[df['PERMNO'] == permno]['PRC']))
priceList = priceList[~numpy.isnan(priceList)]
changeList = []
lastPrice = abs(priceList[0])
for i in range(len(priceList)):
absPrice = abs(priceList[i])
if (absPrice == 0):
absPrice = lastPrice
# print "price:%f\t%r" % (absPrice, absPrice < (1-alpha)*lastPrice or absPrice > (1+alpha)*lastPrice)
if (absPrice < (1-alpha)*lastPrice or absPrice > (1+alpha)*lastPrice):
changeList.append(i)
lastPrice = absPrice
return len(changeList)
def isFloat(x):
try:
a = float(x)
except ValueError:
return False
else:
return True
def findMeanAndStdDev(permno):
# get returns and find return
#priceList = numpy.array(list(df[df['PERMNO'] == permno]['PRC']))
# print type(returnList)
returnList = numpy.array(list(df[df['PERMNO'] == permno]['RET']))
returnList = numpy.array([x for x in returnList if isFloat(x)])
#print returnList
# print type(returnList2)
returnList = returnList.astype('float')
returnList = returnList[numpy.logical_not(numpy.isnan(returnList))]
# find the % return
# get the mean as a %
percentageMean = numpy.mean(returnList)#/float(priceList[0])
percentageStdDev = numpy.std(returnList)
return (percentageMean, percentageStdDev)
# def findStdDev(permno):
# priceList = numpy.array(list(df[df['PERMNO'] == permno]['PRC']))
# priceList = priceList[numpy.logical_not(numpy.isnan(priceList))]
# # get the stddev as a %
# percentageStdDev = numpy.std(priceList)#/float(priceList[0])
# return percentageStdDev
percentileChangesArray = [0]*10
for permno in permnoList:
# get market cap, find number of changes, and also find standard deviation and mean return
percentile = marketCapDict[permno][1]
numChanges = findChangeIndices(permno)
meanAndStdDev = findMeanAndStdDev(permno)
mean = meanAndStdDev[0]
stdDev = meanAndStdDev[1]
#mean = findMean(permno)
# stdDev = findStdDev((permno))
print "%s: %d\t%d changes\t %f mean\t %f std" % (permno, percentile, numChanges, mean, stdDev)
percentileChangesArray[percentile-1] += numChanges
standardDeviationArray[percentile-1].append(stdDev)
meanReturnArray[percentile-1].append(mean)
print percentileChangesArray
print sum(percentileChangesArray)
for i in range(10):
standardDeviationArray[i] = numpy.mean(standardDeviationArray[i])
meanReturnArray[i] = numpy.mean(meanReturnArray[i])
print "standardDeviationArray"
print standardDeviationArray
print "meanReturnArray"
print meanReturnArray
# countryRateChangeDict = {}
# def getCurrencyData():
# global countryRateChangeDict
# df2 = pandas.read_csv('basicCurrencyData.csv')
# countries = numpy.delete(df2.columns.values, 0)
# print countries
# # test = df[countries[0]]
# # test = list(test[np.logical_not(np.isnan(test))])
# # print test
# for country in countries:
# fxRates = df2[country]
# fxRates = list(fxRates[numpy.logical_not(numpy.isnan(fxRates))])
# alpha = 0.05 # percent change needed to be significant
# changeList = []
# lastRate = abs(fxRates[0])
# counter = 0
# for rate in fxRates:
# absRate = abs(rate)
# # print "price:%f\t%r" % (absPrice, absPrice < (1-alpha)*lastPrice or absPrice > (1+alpha)*lastPrice)
# if (absRate < (1-alpha)*lastRate or absRate > (1+alpha)*lastRate):
# counter += 1
# # if (country == 'china'):
# # print "last:%s new:%s" % (absRate, lastRate)
# lastRate = absRate
# countryRateChangeDict[country] = counter
# print countryRateChangeDict
# print sum(countryRateChangeDict.values())
# getCurrencyData()
# plot
decileArray = []
for i in range(10):
decileArray.append('%d' % (i+1))
# graph of percentile changes by decile
plt.figure(1)
# # add the currency data to it
# percentileChangesArray.append(sum(countryRateChangeDict.values()))
# decileArray.append("FX rates")
x_pos = numpy.arange(len(decileArray))
print len(x_pos)
print len(percentileChangesArray)
plt.bar(x_pos, percentileChangesArray)
plt.xticks(x_pos, decileArray)
plt.ylabel('Days with more than %f%% change in price' % (alpha*100))
# graph of standard deviation by decile
plt.figure(2)
plt.subplot(2,1,1)
# # add currency data to it
# standardDeviationArray.append(numpy.std(countryRateChangeDict.values()))
# meanReturnArray.append(numpy.mean(countryRateChangeDict.values()))
plt.bar(x_pos, standardDeviationArray)
plt.xticks(x_pos, decileArray)
plt.ylabel('Standard Deviation')
plt.title('Standard Deviation vs stock decile')
plt.subplot(2,1,2)
plt.plot(standardDeviationArray, meanReturnArray, 'ro')
plt.xticks(standardDeviationArray, decileArray)
plt.ylabel('Mean Return')
plt.title('Return vs Standard Deviation')
plt.show()