-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResamplingAlgorithm.py
More file actions
427 lines (326 loc) · 15.2 KB
/
Copy pathResamplingAlgorithm.py
File metadata and controls
427 lines (326 loc) · 15.2 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# The following was developed to replicate distribution fitting abilities from @Risk in a Pythonic environment. It is
# primarily for the purposes of fitting distributions to historical price data and creating continuously random variables
# from which prices are sampled throughout a Monte-Carlo simulation. This algorithm attempts fitting of Normal, Pareto, Weibull,
# Gamma, Uniform, and Triangular distributions to input data and returns fit parameters, least squares residual, and
# graphical output of the fitting process. Options are built in to autoselect the best fitting distribution, or prompt
# the user to manually select the desired distribution. Functionality for sampling multiple values from the resulting
# continuously random variables at once is available to simulate an extended plant lifetime without recurring function calls.
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import math
import plotly.graph_objs as go
from scipy.stats import gamma
import random
# Stores fitting results for all 6 distributions such that residuals and fit parameters can be compared manually
# if necessary
allFitParameters = {}
# DATA MODIFICATION FUNCTIONS:
# Returns the indeces in which the mode of the dataset occurs for triangular distribution fit options.
def getModes(inputData):
modeIndeces = []
theBigGuy = max(inputData)
for i in range(len(inputData)):
if inputData[i] == theBigGuy:
modeIndeces.append(i)
return modeIndeces
# Returns the x coordinates of bin centers for the purposes of graphing a histogram of input data.
def binstoxdata(bins):
xdata = []
for i in range(len(bins) - 1):
xdata.append((bins[i] + bins[i + 1]) / 2)
return xdata
# Returns list of input data scaled to start at 1. This makes fitting Pareto distributions easier
def ScaleData(inputYData):
sum = np.sum(inputYData)
resList = []
for i in range(0, len(inputYData)):
resList.append(inputYData[i]/sum)
return resList
# FITTING FUNCTIONS
# The following functions are to fit the 6 distributions mentioned above to the input dataset. They are all structured
# identically and will be explained here.
# 1. Input data is made into a histogram.
# 2. Generalized versions of the Probability Density Function for the fitted distribution are created.
# 3. For Normal and Triangular (where the fit parameters have a physical/statistical meaning from the data) best fit
# results are calculated directly. Otherwise, fit parameters are optimized to the scaled input data histogram and
# a least squares residual is calculated. All fit results are appended to the AllFitParameters dictionary.
# 4. X,Y data synthesized and returned for plotting purposes.
def TriangularFitFunction(inputData, binArgs):
hist, bin_edges = np.histogram(inputData, bins = binArgs, density=True)
xdata = np.asarray(binstoxdata(bin_edges))
ydata = np.asarray(hist)
mode = max(ydata)
xMin = min(inputData)
xMax = max(inputData)
modeIndeces = getModes(ydata)
def leftTriFit(x, xMode):
y = (mode / (xMode - xMin)) * (x - xMin)
return y
def rightTriFit(x, xMode):
y = (mode / (xMode - xMax)) * (x - xMax)
return y
# SS_RES and evaluate fns for each occurance of mode
ss_res_summary = []
for e in modeIndeces:
ss_res = 0
for i in range(e):
ss_res += (leftTriFit(xdata[i], xdata[e]) - ydata[i]) ** 2
for i in range(e, len(xdata)):
ss_res += (rightTriFit(xdata[i], xdata[e]) - ydata[i]) ** 2
ss_res_summary.append(ss_res)
bestFitIndex = ss_res_summary.index(min(ss_res_summary))
bestMode = modeIndeces[bestFitIndex]
bestModeX = xdata[bestMode]
plotXRange = np.linspace(min(inputData),max(inputData))
leftList = []
rightList = []
for e in plotXRange:
if(e<bestModeX):
leftList.append(leftTriFit(e, bestModeX))
elif(e>bestModeX):
rightList.append(rightTriFit(e, bestModeX))
triFitData = leftList + rightList
allFitParameters['Triangular'] = [min(ss_res_summary), [min(inputData), bestModeX, max(inputData)]]
return plotXRange, triFitData
def ParetoFitFunction(inputData, binArgs):
hist, bin_edges = np.histogram(inputData, bins = binArgs, density= True)
xdata = np.asarray(binstoxdata(bin_edges))
ydata = np.asarray(hist)
# print("BEFORE PARETO")
# print(f'MINXDATA: {min(inputData)}')
def Pareto(x, alpha):
y = (alpha * (min(inputData)**alpha)/(x ** (alpha+1)))
return y
# print("AFTER PARETO")
parameters, covariance = sp.optimize.curve_fit(Pareto, xdata, ydata)
fit_A = parameters[0]
fit_y = Pareto(xdata, fit_A)
residuals = ydata - fit_y
ss_res = np.sum(residuals ** 2)
allFitParameters['Pareto'] = [ss_res, list(parameters), ['Alpha']]
paretoFitData = []
plotXRange_linspace = np.linspace(min(xdata), max(inputData), 200)
for e in plotXRange_linspace:
paretoFitData.append(Pareto(e, fit_A))
return xdata, paretoFitData
def WiebullFitFunction(inputData, binArgs):
hist, bin_edges = np.histogram(inputData,bins = binArgs, density=True)
xdata = np.asarray(binstoxdata(bin_edges))
ydata = np.asarray(hist)
def WiebullVariate(x, alpha, beta):
y = ((beta/alpha)*(x/alpha)**(beta-1))*(np.exp(-(x/alpha)**beta))
return y
parameters, covariance = sp.optimize.curve_fit(WiebullVariate, xdata, ydata)
fitAlpha = parameters[0]
fitBeta = parameters[1]
fit_y = WiebullVariate(xdata, fitAlpha, fitBeta)
residuals = ydata - fit_y
ss_res = np.sum(residuals ** 2)
allFitParameters['Weibull'] = [ss_res, list(parameters), ['Alpha', 'Beta']]
wiebullFitData = []
plotXRange = np.linspace(min(inputData), max(inputData))
for e in plotXRange:
wiebullFitData.append(WiebullVariate(e, fitAlpha, fitBeta))
return plotXRange, wiebullFitData
def NormalFitFunction(inputData, binArgs):
mean = np.mean(inputData)
stdev = np.std(inputData)
hist, bin_edges = np.histogram(inputData, bins = binArgs, density=True)
xdata = np.asarray(binstoxdata(bin_edges))
# ydataTemp = ScaleData(hist)
ydata = np.asarray(hist)
def Normal(x):
# print(f'MEAN IN FN: {mean}, STDEV IN FN: {stdev}')
y = (1 / (stdev * (2 * np.pi) ** .5)) * np.exp(-.5 * ((x - mean) / stdev) ** 2)
return y
fitY = Normal(xdata)
residuals = ydata - fitY
ss_res = np.sum(residuals ** 2)
allFitParameters['Normal'] = [ss_res, [mean, stdev], ['Mean', 'STDEV']]
normalFitData = []
plotXRange = np.linspace(min(inputData), max(inputData))
for e in plotXRange:
normalFitData.append(Normal(e))
return plotXRange, normalFitData
def GammaVariateFitFunction(inputData, binArgs):
hist, bin_edges = np.histogram(inputData, bins = binArgs, density=True)
xdata = np.asarray(binstoxdata(bin_edges))
ydata = np.asarray(hist)
def gammaVariate(x, alpha, beta):
return gamma.pdf(x, alpha, scale=beta)
parameters, covariance = sp.optimize.curve_fit(gammaVariate, xdata, ydata)
fitAlpha = parameters[0]
fitBeta = parameters[1]
fit_y = gammaVariate(xdata, fitAlpha, fitBeta)
residuals = ydata - fit_y
ss_res = np.sum(residuals ** 2)
allFitParameters['GammaVariate'] = [ss_res, list(parameters), ['Alpha', 'Beta']]
gammaFitData = []
plotXRange = np.linspace(min(inputData), max(inputData))
for e in plotXRange:
gammaFitData.append(gammaVariate(e, fitAlpha, fitBeta))
return plotXRange, gammaFitData
def UniformFitFunction(inputData, binArgs):
hist, bin_edges = np.histogram(inputData, bins = binArgs, density = True)
xdata = np.asarray(binstoxdata(bin_edges))
ydata = np.asarray(hist)
def Uniform(x, a):
y = a*x**0
return y
parameters, covariance = sp.optimize.curve_fit(Uniform, xdata, ydata)
fit_A = parameters[0]
fit_y = Uniform(xdata, fit_A)
residuals = ydata - fit_y
ss_res = np.sum(residuals ** 2)
parameters = list(parameters)
parameters.append(min(inputData))
parameters.append(max(inputData))
allFitParameters['Uniform'] = [ss_res, list(parameters), ['a', 'Min', 'Max']]
uniformFitData = []
plotXRange = np.linspace(min(inputData), max(inputData))
for e in plotXRange:
uniformFitData.append(Uniform(e, fit_A))
return plotXRange, uniformFitData
# The following take in best fit parameters of respective distributions and return a sampled value from the
# corresponding continuously random variable.
def NormalContinuousSampler(parameters):
y = random.gauss(parameters[0], parameters[1])
return y
def ParetoContinuousSampler(parameters):
y = random.paretovariate(parameters[0])
return y
def WiebullContinuousSampler(parameters):
y = random.weibullvariate(parameters[0], parameters[1])
return y
def UniformContinuousSampler(parameters):
y = random.uniform(parameters[1], parameters[2])
return y
def TriangularContinuousSampler(parameters):
y = random.triangular(parameters[0], parameters[1], parameters[2])
return y
def GammaVariateContinuousSampler(parameters):
y = random.gammavariate(parameters[0], parameters[1])
return y
# Takes in [bestFitFunctionName, [fitParam], min(dataset)], and returns the desired number of randomly sampled
# values form the best fit distribution PDF's
def GetSampledValue(bestfitInfo, numSamples = 1):
result = []
# Maps bestFitFunctionName from the input list to the correct sampler function defined above.
functionMapping = {
"Normal": NormalContinuousSampler,
"Pareto": ParetoContinuousSampler,
"Weibull": WiebullContinuousSampler,
"Uniform": UniformContinuousSampler,
"GammaVariate": GammaVariateContinuousSampler,
"Triangular": TriangularContinuousSampler,
}
# Synthesizes list of desired number of sampled values
for i in range(numSamples):
if bestfitInfo[0] in functionMapping:
result.append(functionMapping[bestfitInfo[0]](bestfitInfo[1][1]))
result = np.asarray(result)
# Re-scales the data to its original magnitude by adding the min(dataset) - 1
return result + (bestfitInfo[-1] - 1)
# Takes in an input dataset and returns a list in the following format: [bestFitFunctionName, [fitParam], min(dataset)]
def FitProcedure(inputData, show = True, autoSelect = True, outputParam = True, numBins = 10):
# [functionName, [param], min(dataset)]
bestFitParameters = []
# Scale the input data
scaledInputData = []
for e in inputData:
scaledInputData.append(e - min(inputData) + 1)
# BinArgs establishes the range of x values for which bins of the histogram will be spread over.
binArgs = np.linspace(min(scaledInputData), max(scaledInputData), numBins)
# Calling functions to fit distributions to scaled data
paretoXdata, paretoFit = ParetoFitFunction(scaledInputData, binArgs)
wiebullXdata, wiebullFitData = WiebullFitFunction(scaledInputData, binArgs)
normalXdata, normalFitData = NormalFitFunction(scaledInputData, binArgs)
gammaXData, gammaFitData = GammaVariateFitFunction(scaledInputData, binArgs)
triXData, triFitData = TriangularFitFunction(scaledInputData, binArgs)
uniformXData, uniformFitData = UniformFitFunction(scaledInputData, binArgs)
# Creates histogram of scaled input data for graphing purposes
counts, bins = np.histogram(scaledInputData, bins=binArgs, density=True)
bins = binstoxdata(bins)
# The following are code blocks associated with this functions optional arguments. 'Show' will output Plotly graphs
# depicting the fitted distributions for visual comparison. 'AutoSelect' = True will ensure the function
# automatically chooses the fitted distribution with lowest residual to model the input data. 'AutoSelect' = False
# will probe the user to manually make that decision. 'outputParam' prints best fit parameters and the selected
# distribution to console.
if autoSelect:
minKey = min(allFitParameters, key=lambda key: allFitParameters[key][0])
bestFitParameters.append(minKey)
bestFitParameters.append(allFitParameters.get(minKey))
if not autoSelect:
validFunctions = ["Normal", "GammaVariate", "Triangular", "Uniform", "Weibull", "Pareto"]
while True:
functionType = input(f'Enter the function to use as best fit from the following list: {validFunctions}')
if functionType in validFunctions:
bestFitParameters.append(functionType)
bestFitParameters.append(allFitParameters.get(functionType))
break
if show:
barTrace = go.Bar(
x=bins, # X-axis labels (categories)
y=counts, # Y-axis values (height of bars)
marker=dict(color='#99CCFF'), # Optional: Customize the color of bars
name='Histogram'
)
uniformTrace = go.Scatter(
x=uniformXData, # X-axis labels (categories)
y=uniformFitData, # Y-axis values (height of bars)
marker=dict(color='orange'), # Optional: Customize the color of bars
name='Uniform Fit'
)
paretoTrace = go.Scatter(
x=paretoXdata, # X-axis data
y=paretoFit, # Y-axis data
mode='lines', # 'lines' mode for a line plot
name='Pareto Fit', # Name of the trace
line=dict(color='blue', width=2) # Line color and width
)
GVTrace = go.Scatter(
x=gammaXData, # X-axis data
y=gammaFitData, # Y-axis data
mode='lines', # 'lines' mode for a line plot
name='Gamma Fit', # Name of the trace
line=dict(color='black', width=2) # Line color and width
)
TriTrace = go.Scatter(
x=triXData, # X-axis data
y=triFitData, # Y-axis data
mode='lines', # 'lines' mode for a line plot
name='Tri Fit', # Name of the trace
line=dict(color='gray', width=2), # Line color and width
)
wiebullTrace = go.Scatter(
x=wiebullXdata, # X-axis data
y=wiebullFitData, # Y-axis data
mode='lines', # 'lines' mode for a line plot
name='Wiebull Fit', # Name of the trace
line=dict(color='red', width=2) # Line color and width
)
normalTrace = go.Scatter(
x=normalXdata, # X-axis data
y=normalFitData, # Y-axis data
mode='lines', # 'lines' mode for a line plot
name='Normal Fit', # Name of the trace
line=dict(color='green', width=2) # Line color and width
)
layout = go.Layout(
title='Distribution Fit Functions',
xaxis=dict(title='Scaled Price Data ($)'),
yaxis=dict(title='Cumulative Frequency (%)'),
barmode='group',
bargap=0
)
fig = go.Figure(
data=[barTrace, wiebullTrace, paretoTrace, normalTrace, GVTrace, TriTrace, uniformTrace],
layout=layout)
fig.show()
if outputParam:
print(f'All Fit Parameters: {allFitParameters}')
print(f'BestFitParameters: {bestFitParameters}\n')
bestFitParameters.append(min(inputData))
return bestFitParameters