-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarketUtils.py
165 lines (140 loc) · 4.71 KB
/
marketUtils.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
import os
import csv
import numpy as np
import pickle
import pandas as pd
def saveToPickle(my_object, fileName):
print("Pickling my_object to file: "+str(fileName)+"...")
pickle.dump(my_object, open(fileName, "wb"))
print ("Saved.")
def getFromPickle(fileName):
if os.path.isfile(fileName):
my_object = pickle.load(open(fileName, "rb"))
return my_object
else:
return None
def getNem():
myFile = open('nem_allstates.csv')
nemData = csv.DictReader(myFile)
nem = {}
for timePeriod in nemData:
timeString = timePeriod['Time-ending']
nem[timeString] = {
'nsw': {
'price': float(timePeriod['NSW1 Price']),
'demand':float(timePeriod['NSW1 Scheduled Demand']),
'nonScheduled':float(timePeriod['NSW1 Non-scheduled']),
'generation': float(timePeriod['NSW1 Generation']),
'availability':float(timePeriod['NSW1 Availability']),
},
'vic': {
'price': float(timePeriod['VIC1 Price']),
'demand':float(timePeriod['VIC1 Scheduled Demand']),
'nonScheduled':float(timePeriod['VIC1 Non-scheduled']),
'generation': float(timePeriod['VIC1 Generation']),
'availability':float(timePeriod['VIC1 Availability']),
},
'qld': {
'price': float(timePeriod['QLD1 Price']),
'demand':float(timePeriod['QLD1 Scheduled Demand']),
'nonScheduled':float(timePeriod['QLD1 Non-scheduled']),
'generation': float(timePeriod['QLD1 Generation']),
'availability':float(timePeriod['QLD1 Availability']),
},
'sa': {
'price': float(timePeriod['SA1 Price']),
'demand':float(timePeriod['SA1 Scheduled Demand']),
'nonScheduled':float(timePeriod['SA1 Non-scheduled']),
'generation': float(timePeriod['SA1 Generation']),
'availability':float(timePeriod['SA1 Availability']),
},
'tas': {
'price': float(timePeriod['TAS1 Price']),
'demand':float(timePeriod['TAS1 Scheduled Demand']),
'nonScheduled':float(timePeriod['TAS1 Non-scheduled']),
'generation': float(timePeriod['TAS1 Generation']),
'availability':float(timePeriod['TAS1 Availability']),
},
}
return nem
def getInterconnectorFlows():
filename = 'interconnectorflows.csv'
# Reading the file
df = pd.read_csv(filename, index_col=0)
# Creating the dict
flows = df.transpose().to_dict()
return flows
def getBidStackFromFile(directory, filename, bidStacks):
path = os.path.join(directory, filename)
myFile = open(path)
retailer = filename.split('.')[0]
print retailer
# Input data is normalised to kWh/kWp or MWh / MWp (equivalent)
lines = csv.DictReader(myFile)
stack = {}
for line in lines:
stack[line['Time-ending']] = line
bidStacks[retailer] = stack
return bidStacks
def getBidStacks(state):
bidStacks = {}
directory = 'bidstacks/'+state
for filename in os.listdir(directory):
if filename.endswith(".csv") or filename.endswith(".py"):
bidStacks = getBidStackFromFile(directory, filename, bidStacks)
continue
else:
continue
return bidStacks
def _calculateHHI(values):
mysum = 0
hhi = 0
for value in values:
mysum += float(value)
if mysum > 0:
for val in values:
fraction = float(val) / float(mysum)
percent = fraction * 100.0
hhi += percent*percent
return hhi
else:
return 0
# In check which firm has the greatest share in any given time period
def _getMaxShareRetailer(values, retailers):
maxShareRetailer = retailers[0]
maxShare = 0
mysum = 0
for value in values:
mysum += float(value)
if mysum > 0:
for i in range(len(values)):
fraction = float(values[i]) / float(mysum)
if fraction > maxShare:
maxShare = fraction
maxShareRetailer = retailers[i]
return maxShareRetailer
else:
return None
def getHHI():
maxShareRetailers = getFromPickle('maxShareRetailers.pkl')
hhi = getFromPickle('hhi.pkl')
if not hhi:
categories = ['Cumulative', '-$500', '$0','$15', '$25', '$35', '$50','$100', '$300', '$500', '$1000','$5000', '$99999']
bidStacks = getBidStacks('nsw')
hhi = {}
maxShareRetailers = {}
nem = getNem()
# Calculate HHI for each category
for i, timeString in enumerate(list(nem)):
print str(i)+' of '+str(len(list(nem)))
hhi[timeString] = {}
for category in categories:
values = []
for retailer in list(bidStacks):
values.append(float(bidStacks[retailer][timeString][category]))
hhi[timeString][category+"_HHI"] = _calculateHHI(values)
if category == 'Cumulative':
maxShareRetailers[timeString] = _getMaxShareRetailer(values, list(bidStacks))
saveToPickle(hhi, 'hhi.pkl')
saveToPickle(hhi, 'maxShareRetailers.pkl')
return hhi, maxShareRetailers