def complexRun(plot = True): dataLength = 200 actLength = 13 numActivities = 1 actSize = 0.5 actStd = 0.05 backStd = 0.1 minWindowLen = 2 maxWindowLen = 8 backMean = 0.0 #Create simple gaussian noise data = [random.gauss(backMean, backStd) for i in range(dataLength)] acts = pybb.data.generateActivities1d(10, actLength, actSize, actStd, aType = 2) #Make testing data sacts = pybb.data.generateActivities1d(numActivities, actLength, actSize, actStd, aType = 2) starts = [] for s in sacts: #Pick a random spot start = int((dataLength - actLength) * random.random()) starts.append(start) for i in range(len(s)): data[start + i] = s[i] m, mdata, mout = khmm.train(acts.tolist(), 1, actLength, \ iterations = 20, outliers = False, \ clustering = "kmeans++", verbose = False) model = [] model.append(gaussian.Gaussian(backMean, backStd)) model.append(m[0]) bf = BayesianForecast(model) bf.setStd(0, backStd) bf.setStd(1, actStd) #fdata, probs, windowLens = bf.forecast(data, windowLen = minWindowLen, ftype = "best") fdata, probs, windowLens, models = bf.windowForecast(data, \ minWindow = minWindowLen, \ maxWindow = maxWindowLen, \ ftype = "best") if plot: mpl.subplot(211) xdata = range(len(data)) mpl.plot(xdata, data, 'k', alpha = 0.5, linewidth = 2) mpl.plot(xdata, fdata, 'r', alpha = 0.5) mpl.xlim([0, len(data) - 1]) mpl.subplot(212) xdata = range(actLength) for s in sacts: mpl.plot(xdata, s, 'k', alpha = 0.5) mpl.xlim([0, actLength - 1]) return data, starts, fdata, probs, bf, windowLens, models
def complexRun(plot=True): dataLength = 200 actLength = 13 numActivities = 1 actSize = 0.5 actStd = 0.05 backStd = 0.1 minWindowLen = 2 maxWindowLen = 8 backMean = 0.0 #Create simple gaussian noise data = [random.gauss(backMean, backStd) for i in range(dataLength)] acts = pybb.data.generateActivities1d(10, actLength, actSize, actStd, aType=2) #Make testing data sacts = pybb.data.generateActivities1d(numActivities, actLength, actSize, actStd, aType=2) starts = [] for s in sacts: #Pick a random spot start = int((dataLength - actLength) * random.random()) starts.append(start) for i in range(len(s)): data[start + i] = s[i] m, mdata, mout = khmm.train(acts.tolist(), 1, actLength, \ iterations = 20, outliers = False, \ clustering = "kmeans++", verbose = False) model = [] model.append(gaussian.Gaussian(backMean, backStd)) model.append(m[0]) bf = BayesianForecast(model) bf.setStd(0, backStd) bf.setStd(1, actStd) #fdata, probs, windowLens = bf.forecast(data, windowLen = minWindowLen, ftype = "best") fdata, probs, windowLens, models = bf.windowForecast(data, \ minWindow = minWindowLen, \ maxWindow = maxWindowLen, \ ftype = "best") if plot: mpl.subplot(211) xdata = range(len(data)) mpl.plot(xdata, data, 'k', alpha=0.5, linewidth=2) mpl.plot(xdata, fdata, 'r', alpha=0.5) mpl.xlim([0, len(data) - 1]) mpl.subplot(212) xdata = range(actLength) for s in sacts: mpl.plot(xdata, s, 'k', alpha=0.5) mpl.xlim([0, actLength - 1]) return data, starts, fdata, probs, bf, windowLens, models
pybb.data.parseEvents(fData, sunTimes, eventTimes) #Get just the broncos residual 9-15 resEventData = [] for r in resSData: resEventData.append(r[9:17]) #Convert to list tmp = np.array(resEventData) resEventData = tmp.tolist() resEventData = pybb.data.stripZero(resEventData, threshold=6) #Train HMM m, mdata, mout = khmm.train(resEventData, 1, actLength, \ iterations = 20, outliers = False, \ clustering = "kmeans++", verbose = False) mase = analysis.mase(sunData1d, fData1d) mape = analysis.mape(sunData1d, fData1d) print "Mean Mase:", mase print "Mean Mape:", mape #Setup forecast model = [] model.append(gaussian.Gaussian(resData.mean(), resData.std())) #model.append(gaussian.Gaussian(0, resData.std())) model.append(m[0]) bf = bayesianforecast.BayesianForecast(model)
mpl.subplot(211) xdata = range(periodLength) for d in data: mpl.plot(xdata, d, 'k', alpha = 0.3) mpl.xlim([0, periodLength - 1]) #Residuals mpl.subplot(212) xdata = range(activityLength) for a in act: mpl.plot(xdata, a, 'k', alpha = 0.3) mpl.xlim([0, activityLength - 1]) #Train Models model, mdata, mout = khmm.train(act.tolist(), numModels, activityLength, \ iterations = 20, outliers = False, \ clustering = "kmeans++") model.append(gaussian.Gaussian(0, res1d.std())) """ #Perform a simple forecast for one dataelement. #Start with simply two periods sdata = res1d[(acts[0][0] - 2) * periodLength:(acts[0][0]) * periodLength] #Forecast all points along sdata bf = bayesian.BayesianForecast(model) bf.setStd(0, res1d.std()) bf.setStd(1, res1d.std()) windowLen = 5 bfData = []
mpl.subplot(211) xdata = range(periodLength) for d in data: mpl.plot(xdata, d, 'k', alpha=0.3) mpl.xlim([0, periodLength - 1]) #Residuals mpl.subplot(212) xdata = range(activityLength) for a in act: mpl.plot(xdata, a, 'k', alpha=0.3) mpl.xlim([0, activityLength - 1]) #Train Models model, mdata, mout = khmm.train(act.tolist(), numModels, activityLength, \ iterations = 20, outliers = False, \ clustering = "kmeans++") model.append(gaussian.Gaussian(0, res1d.std())) """ #Perform a simple forecast for one dataelement. #Start with simply two periods sdata = res1d[(acts[0][0] - 2) * periodLength:(acts[0][0]) * periodLength] #Forecast all points along sdata bf = bayesian.BayesianForecast(model) bf.setStd(0, res1d.std()) bf.setStd(1, res1d.std()) windowLen = 5 bfData = []
scores.append(a) return scores def findFit(data, models): index = [] for d in data: index.append(bestFit(d, models)) return index if __name__ == "__main__": data = sampleMany(200, 8, atype = 0) data += sampleMany(200, 8, atype = 1) m, d, o = khmm.train(data, 2, 8, iterations = 10, outliers = False, \ clustering = "kmeans++") res = findFit(data, m) counts = [0, 0] for r in range(len(data)): i = r / len(data) / 2 counts[i] += res[r] print counts print "This should print out something close to 200, 0 or 0, 200"
#Get just the broncos residual 9-15 resEventData = [] for r in resSData: resEventData.append(r[9:17]) #Convert to list tmp = np.array(resEventData) resEventData = tmp.tolist() resEventData = pybb.data.stripZero(resEventData, threshold = 6) #Train HMM m, mdata, mout = khmm.train(resEventData, 1, actLength, \ iterations = 20, outliers = False, \ clustering = "kmeans++", verbose = False) mase = analysis.mase(sunData1d, fData1d) mape = analysis.mape(sunData1d, fData1d) print "Mean Mase:", mase print "Mean Mape:", mape #Setup forecast model = [] model.append(gaussian.Gaussian(resData.mean(), resData.std())) #model.append(gaussian.Gaussian(0, resData.std())) model.append(m[0])