def plotPrediction(time, data, predictedTime, predictedData, predictedVar, options): """ Function for ploting N-day ahead prediction Args: time: the data time data: the original data predictedTime: the predicted time period predictedData: the predicted data predictedVar: the predicted variance options: options for the plot, for details please refer to @dlm """ # plot the original data plotData(time, data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') # overlay the predicted data plotData(predictedTime, predictedData, showDataPoint=options.showFittedPoint, color=options.predictedColor, label='N-day prediction') # overlay confidence interval if options.showConfidenceInterval: upper, lower = getInterval(predictedData, predictedVar, p=options.confidence) plotInterval(time=predictedTime, upper=upper, lower=lower, intervalType=options.intervalType, color=options.predictedColor)
def getInterval(self, p=0.95, filterType='forwardFilter', name='main'): """ get the confidence interval for data or component. If the filtered dates are not (0, self.n - 1), then a warning will prompt stating the actual filtered dates. Args: p: The confidence level. filterType: the type of CI to be returned. Could be 'forwardFilter', 'backwardSmoother', and 'predict'. Default to 'forwardFilter'. name: the component to get CI. When name = 'main', then it returns the confidence interval for the time series. When name = some component's name, then it returns the confidence interval for that component. Default to 'main'. Returns: A tuple with the first element being a list of upper bounds and the second being a list of the lower bounds. """ # get the working date start, end = self._checkAndGetWorkingDates(filterType=filterType) end += 1 # get the mean and the variance for the time series data if name == 'main': # get out of the matrix form if filterType == 'forwardFilter': compMean = self._1DmatrixToArray( self.result.filteredObs[start:end]) compVar = self._1DmatrixToArray( self.result.filteredObsVar[start:end]) elif filterType == 'backwardSmoother': compMean = self._1DmatrixToArray( self.result.smoothedObs[start:end]) compVar = self._1DmatrixToArray( self.result.smoothedObsVar[start:end]) elif filterType == 'predict': compMean = self._1DmatrixToArray( self.result.predictedObs[start:end]) compVar = self._1DmatrixToArray( self.result.predictedObsVar[start:end]) else: raise NameError('Incorrect filter type.') # get the mean and variance for the component else: self._checkComponent(name) compMean = self._getComponentMean(name=name, filterType=filterType, start=start, end=(end - 1)) compVar = self._getComponentVar(name=name, filterType=filterType, start=start, end=(end - 1)) # get the upper and lower bound upper, lower = getInterval(compMean, compVar, p) return (upper, lower)
def getInterval(self, p=0.95, filterType='forwardFilter', name='main'): """ get the confidence interval for data or component. If the filtered dates are not (0, self.n - 1), then a warning will prompt stating the actual filtered dates. Args: p: The confidence level. filterType: the type of CI to be returned. Could be 'forwardFilter', 'backwardSmoother', and 'predict'. Default to 'forwardFilter'. name: the component to get CI. When name = 'main', then it returns the confidence interval for the time series. When name = some component's name, then it returns the confidence interval for that component. Default to 'main'. Returns: A tuple with the first element being a list of upper bounds and the second being a list of the lower bounds. """ # get the working date start, end = self._checkAndGetWorkingDates(filterType=filterType) # get the mean and the variance for the time series data if name == 'main': # get out of the matrix form if filterType == 'forwardFilter': compMean = self._1DmatrixToArray( self.result.filteredObs[start:end]) compVar = self._1DmatrixToArray( self.result.filteredObsVar[start:end]) elif filterType == 'backwardSmoother': compMean = self._1DmatrixToArray( self.result.smoothedObs[start:end]) compVar = self._1DmatrixToArray( self.result.smoothedObsVar[start:end]) elif filterType == 'predict': compMean = self._1DmatrixToArray( self.result.predictedObs[start:end]) compVar = self._1DmatrixToArray( self.result.predictedObsVar[start:end]) else: raise NameError('Incorrect filter type.') # get the mean and variance for the component else: self._checkComponent(name) compMean = self._getComponentMean(name=name, filterType=filterType, start=start, end=end) compVar = self._getComponentVar(name=name, filterType=filterType, start=start, end=end) # get the upper and lower bound upper, lower = getInterval(compMean, compVar, p) return (upper, lower)
def getPredictedInterval(self, p): """ get the predicted confidence interval. If the predicted dates are not (0, self.n - 1), then a warning will prompt stating the actual predicted dates. Args: p: The confidence level. Returns: A tuple with the first element being a list of upper bounds and the second being a list of the lower bounds. """ if self.result.filteredSteps != [0, self.n - 1]: print('The predicted dates are from ' + str(self.result.filteredSteps[0]) + \ ' to ' + str(self.result.filteredSteps[1])) start = self.result.filteredSteps[0] end = self.result.filteredSteps[1] + 1 upper, lower = getInterval(self.result.predictedObs[start : end], \ self.result.predictedObsVar[start : end], p) return (self._1DmatrixToArray(upper), self._1DmatrixToArray(lower))
def plotPrediction(time, data, predictedTime, predictedData, predictedVar, options): """ Function for ploting N-day ahead prediction Args: time: the data time data: the original data predictedTime: the predicted time period predictedData: the predicted data predictedVar: the predicted variance options: options for the plot, for details please refer to @dlm """ # plot the original data plotData(time, data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') # overlay the predicted data plotData(predictedTime, predictedData, showDataPoint=options.showFittedPoint, color=options.predictedColor, label='N-day prediction') # overlay confidence interval if options.showConfidenceInterval: upper, lower = getInterval(predictedData, predictedVar, p=options.confidence) plotInterval(time=predictedTime, upper=upper, lower=lower, intervalType=options.intervalType, color=options.predictedColor)
def plotInMultipleFigure(time, data, result, options): """ Plot the dlm results in multiple figure, each with one result and the original data Args: time: the time label data: the original data result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ # first compute how many plots are needed numOfPlots = options.plotFilteredData + options.plotPredictedData + \ options.plotSmoothedData size = (numOfPlots, 1) location = 1 # plot all needed results # plot fitered results if needed if options.plotFilteredData: # the location subplot(size, location) # plot original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 if start < end: plotData(time=time[start:end], data=to1dArray(result.filteredObs[start:end]), showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered series') if options.showConfidenceInterval: upper, lower = getInterval(result.filteredObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.filteredColor) plt.legend(loc='best', shadow=True) # , fontsize = 'x-large') location += 1 # plot predicted results if needed if options.plotPredictedData: # the location subplot(size, location) # plot original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 if start < end: plotData(time=time[start:end], data=to1dArray(result.predictedObs), showDataPoint=options.showFittedPoint, color=options.predictedColor, label='one-day prediction') if options.showConfidenceInterval: upper, lower = getInterval(result.predictedObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.predictedColor) plt.legend(loc='best', shadow=True) location += 1 # plot smoothed results if needed if options.plotSmoothedData: # the location subplot(size, location) # plot original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 if start < end: plotData(time=time[start:end], data=to1dArray(result.smoothedObs), showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed series') if options.showConfidenceInterval: upper, lower = getInterval(result.smoothedObs[start:end], result.smoothedObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True)
def plotComponent(time, data, result, options): """ Plot the results of a single component in one figure Args: time: the time label data: a dictionary contains all information. The basic keys are data['filteredMean'] = ... data['filteredVar'] = ... data['predictedMean']= ... data['predictedVar'] = ... data['smoothedMean'] = ... data['smoothedVar'] = ... data['name'] = ... not all are needed, depending on the setting in options. result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ if options.separatePlot: numOfPlots = options.plotFilteredData + options.plotPredictedData + \ options.plotSmoothedData size = (numOfPlots, 1) location = 1 # plot fitered results if needed if options.plotFilteredData: if options.separatePlot: subplot(size, location) location += 1 start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=data['filteredMean'], showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered ' + data['name']) if options.showConfidenceInterval: upper, lower = getInterval(data['filteredMean'], map(abs, data['filteredVar']), p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.filteredColor) plt.legend(loc='best', shadow=True) # plot predicted results if needed if options.plotPredictedData: if options.separatePlot: subplot(size, location) location += 1 start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=data['predictedMean'], showDataPoint=options.showFittedPoint, color=options.predictedColor, label='one-step predict for ' + data['name']) if options.showConfidenceInterval: upper, lower = getInterval(data['predictedMean'], map(abs, data['predictedVar']), p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.predictedColor) plt.legend(loc='best', shadow=True) # plot smoothed results if needed if options.plotSmoothedData: if options.separatePlot: subplot(size, location) location += 1 start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 plotData(time=time[start:end], data=data['smoothedMean'], showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed ' + data['name']) if options.showConfidenceInterval: upper, lower = getInterval(data['smoothedMean'], map(abs, data['smoothedVar']), p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True)
def plotSingleState(time, dimension, result, options): """ Plot a single coordinate in the latent state vector Args: time: the time label dimension: the coordinate of the plot result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ # plot fitered results if needed if options.plotFilteredData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 data = [item[dimension, 0] for item in result.filteredState[start:end]] var = [ abs(item[dimension, dimension]) for item in result.filteredCov[start:end] ] plotData(time=time[start:end], data=data, showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered state') if options.showConfidenceInterval: upper, lower = getInterval(data, var, p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.filteredColor) # plot predicted results if needed if options.plotPredictedData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 data = [ item[dimension, 0] for item in result.predictedState[start:end] ] var = [ abs(item[dimension, dimension]) for item in result.predictedCov[start:end] ] plotData(time=time[start:end], data=data, showDataPoint=options.showFittedPoint, color=options.predictedColor, label='predicted state') if options.showConfidenceInterval: upper, lower = getInterval(data, var, p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.predictedColor) # plot smoothed results if needed if options.plotSmoothedData: start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 data = [item[dimension, 0] for item in result.smoothedState[start:end]] var = [ abs(item[dimension, dimension]) for item in result.smoothedCov[start:end] ] plotData(time=time[start:end], data=data, showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed state') if options.showConfidenceInterval: upper, lower = getInterval(data, var, p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True)
def plotInOneFigure(time, data, result, options): """ Plot the dlm results in one figure Args: time: the time label data: the original data result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ # plot the original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') # plot fitered results if needed if options.plotFilteredData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=to1dArray(result.filteredObs[start:end]), showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered series') if options.showConfidenceInterval: upper, lower = getInterval(result.filteredObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.filteredColor) # plot predicted results if needed if options.plotPredictedData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=to1dArray(result.predictedObs), showDataPoint=options.showFittedPoint, color=options.predictedColor, label='one-day prediction') if options.showConfidenceInterval: upper, lower = getInterval(result.predictedObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.predictedColor) # plot smoothed results if needed if options.plotSmoothedData: start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 plotData(time=time[start:end], data=to1dArray(result.smoothedObs), showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed series') if options.showConfidenceInterval: upper, lower = getInterval(result.smoothedObs[start:end], result.smoothedObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True) # , fontsize = 'x-large')
def plotInMultipleFigure(time, data, result, options): """ Plot the dlm results in multiple figure, each with one result and the original data Args: time: the time label data: the original data result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ # first compute how many plots are needed numOfPlots = options.plotFilteredData + options.plotPredictedData + \ options.plotSmoothedData size = (numOfPlots, 1) location = 1 # plot all needed results # plot fitered results if needed if options.plotFilteredData: # the location subplot(size, location) # plot original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 if start < end: plotData(time=time[start:end], data=to1dArray(result.filteredObs[start:end]), showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered series') if options.showConfidenceInterval: upper, lower = getInterval(result.filteredObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.filteredColor) plt.legend(loc='best', shadow=True) # , fontsize = 'x-large') location += 1 # plot predicted results if needed if options.plotPredictedData: # the location subplot(size, location) # plot original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 if start < end: plotData(time=time[start:end], data=to1dArray(result.predictedObs), showDataPoint=options.showFittedPoint, color=options.predictedColor, label='one-day prediction') if options.showConfidenceInterval: upper, lower = getInterval(result.predictedObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.predictedColor) plt.legend(loc='best', shadow=True) location += 1 # plot smoothed results if needed if options.plotSmoothedData: # the location subplot(size, location) # plot original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 if start < end: plotData(time=time[start:end], data=to1dArray(result.smoothedObs), showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed series') if options.showConfidenceInterval: upper, lower = getInterval(result.smoothedObs[start:end], result.smoothedObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True)
def plotComponent(time, data, result, options): """ Plot the results of a single component in one figure Args: time: the time label data: a dictionary contains all information. The basic keys are data['filteredMean'] = ... data['filteredVar'] = ... data['predictedMean']= ... data['predictedVar'] = ... data['smoothedMean'] = ... data['smoothedVar'] = ... data['name'] = ... not all are needed, depending on the setting in options. result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ if options.separatePlot: numOfPlots = options.plotFilteredData + options.plotPredictedData + \ options.plotSmoothedData size = (numOfPlots, 1) location = 1 # plot fitered results if needed if options.plotFilteredData: if options.separatePlot: subplot(size, location) location += 1 start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=data['filteredMean'], showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered ' + data['name']) if options.showConfidenceInterval: upper, lower = getInterval(data['filteredMean'], list(map(abs, data['filteredVar'])), p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.filteredColor) plt.legend(loc='best', shadow=True) # plot predicted results if needed if options.plotPredictedData: if options.separatePlot: subplot(size, location) location += 1 start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=data['predictedMean'], showDataPoint=options.showFittedPoint, color=options.predictedColor, label='one-step predict for ' + data['name']) if options.showConfidenceInterval: upper, lower = getInterval(data['predictedMean'], list(map(abs, data['predictedVar'])), p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.predictedColor) plt.legend(loc='best', shadow=True) # plot smoothed results if needed if options.plotSmoothedData: if options.separatePlot: subplot(size, location) location += 1 start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 plotData(time=time[start:end], data=data['smoothedMean'], showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed ' + data['name']) if options.showConfidenceInterval: upper, lower = getInterval(data['smoothedMean'], list(map(abs, data['smoothedVar'])), p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True)
def plotSingleState(time, dimension, result, options): """ Plot a single coordinate in the latent state vector Args: time: the time label dimension: the coordinate of the plot result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ # plot fitered results if needed if options.plotFilteredData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 data = [item[dimension, 0] for item in result.filteredState[start:end]] var = [abs(item[dimension, dimension]) for item in result.filteredCov[start:end]] plotData(time=time[start:end], data=data, showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered state') if options.showConfidenceInterval: upper, lower = getInterval(data, var, p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.filteredColor) # plot predicted results if needed if options.plotPredictedData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 data = [item[dimension, 0] for item in result.predictedState[start:end]] var = [abs(item[dimension, dimension]) for item in result.predictedCov[start:end]] plotData(time=time[start:end], data=data, showDataPoint=options.showFittedPoint, color=options.predictedColor, label='predicted state') if options.showConfidenceInterval: upper, lower = getInterval(data, var, p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.predictedColor) # plot smoothed results if needed if options.plotSmoothedData: start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 data = [item[dimension, 0] for item in result.smoothedState[start:end]] var = [abs(item[dimension, dimension]) for item in result.smoothedCov[start:end]] plotData(time=time[start:end], data=data, showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed state') if options.showConfidenceInterval: upper, lower = getInterval(data, var, p=options.confidence) plotInterval(time=time[start:end], upper=upper, lower=lower, intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True)
def plotInOneFigure(time, data, result, options): """ Plot the dlm results in one figure Args: time: the time label data: the original data result: the fitted result from dlm class options: options for the plot, for details please refer to @dlm """ # plot the original data plotData(time=time, data=data, showDataPoint=options.showDataPoint, color=options.dataColor, label='time series') # plot fitered results if needed if options.plotFilteredData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=to1dArray(result.filteredObs[start:end]), showDataPoint=options.showFittedPoint, color=options.filteredColor, label='filtered series') if options.showConfidenceInterval: upper, lower = getInterval(result.filteredObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.filteredColor) # plot predicted results if needed if options.plotPredictedData: start = result.filteredSteps[0] end = result.filteredSteps[1] + 1 plotData(time=time[start:end], data=to1dArray(result.predictedObs), showDataPoint=options.showFittedPoint, color=options.predictedColor, label='one-day prediction') if options.showConfidenceInterval: upper, lower = getInterval(result.predictedObs[start:end], result.filteredObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.predictedColor) # plot smoothed results if needed if options.plotSmoothedData: start = result.smoothedSteps[0] end = result.smoothedSteps[1] + 1 plotData(time=time[start:end], data=to1dArray(result.smoothedObs), showDataPoint=options.showFittedPoint, color=options.smoothedColor, label='smoothed series') if options.showConfidenceInterval: upper, lower = getInterval(result.smoothedObs[start:end], result.smoothedObsVar[start:end], p=options.confidence) plotInterval(time=time[start:end], upper=to1dArray(upper), lower=to1dArray(lower), intervalType=options.intervalType, color=options.smoothedColor) plt.legend(loc='best', shadow=True) # , fontsize = 'x-large')