def errorCorrectionForecast(preds, prices,companies): """ :type preds: pd.DataFrame :type prices: pd.DataFrame :type companies: list :rtype analystGrades: DataFrame :rtype forecastFAll: {} """ preds.sort_values(by=['MONTH'],inplace=True) forecastFAll = {} analystGrades = pd.DataFrame(columns=['ESTIMID','ALYSNAM','GRADES','GRADE','DATES','IDENTITY']) analystGrades.set_index('ESTIMID',inplace=True) analystGrades= analystGrades.fillna(value=0) analystGrades[['GRADES','DATES']].astype(object) for company in companies: forecastC = pd.DataFrame(columns=['COMPANY', 'FORECAST', 'OFFSET'], index=range(MONTH_BEG, MONTH_END)) forecastC.index.name = 'MONTH' forecastFAll[company] = forecastC for month in range(MONTH_BEG,MONTH_END): xit.progressRatio(month, MONTH_END - MONTH_BEG, "Offset Corrector forecast", 10) (analystGrades, preds, forecastFAll) = step(preds, prices, analystGrades, forecastFAll, month) return analystGrades, preds, forecastFAll
def evaluateForecast(forecast, prices): sum = 0 pointNb = 0 for i, pred in forecast.iterrows(): try: xit.progressRatio(i, len(forecast), "Forecast evaluation", 10) except: pass try: priceLine = prices.loc[i] #priceLine = prices.loc[int(pred['MONTH'])] price = priceLine['PRC'] except: continue forecasted = pred['FORECAST'] if forecasted == 0 or forecasted is None: continue deviation = abs(price - forecasted) / price sum += deviation pointNb += 1 if pointNb != 0: sum /= pointNb else: return 0 return sum
def evaluateForecast(forecast, prices): for i, pred in forecast.iterrows(): xit.progressRatio(i, len(forecast), "Forecast evaluation", 10) try: priceLine = prices.loc[int(pred['MONTH'])] except: continue if len(priceLine) == 1: price = priceLine['PRC'] forecasted = pred['FORECAST'] deviation = abs(price - forecasted) / price forecast.set_value(i, 'GRADE', deviation) forecast.set_value(i, 'OFFSET', price - forecasted) return forecast
def evaluateSinglePredictions(company, preds, prices): grades = pd.DataFrame(columns=[ 'COMPANY', 'MONTH', 'PREDNO', 'ESTIMID', 'ALYSNAM', 'FORECAST', 'GRADE' ], index=range(len(preds)), dtype=float) totalPred = len(preds) for i, row in preds.iterrows(): val, date = grade(preds, prices, i) if val != -1: grades.loc[i] = [ row['XCOMPANY'], date, 0, row['ESTIMID'], row['ALYSNAM'], row['VALUE'], val ] else: preds.set_value(i, 'GRADE', val) xit.progressRatio(i, totalPred, "Pred analysis", 2) xit.progressRatio(totalPred, totalPred, "Pred analysis completed", 2) grades = grades.dropna() return grades
def price_forecast(grades, company): forecast = pd.DataFrame(columns=[ 'COMPANY', 'MONTH', 'FORECAST', 'GRADE', 'BEST_FORECAST', 'STRAIGHT_FORECAST' ]) minMonth = int(grades['MONTH'].min()) maxMonth = int(grades['MONTH'].max()) for monthEstimation in range(minMonth, maxMonth): xit.progressRatio(monthEstimation - minMonth, maxMonth - minMonth, "Forecast", 10) maskGradesMonth = (grades['COMPANY'] == company) & ( grades['MONTH'] > monthEstimation - 5) & (grades['MONTH'] <= monthEstimation + 5) preds = grades.loc[maskGradesMonth] totalWeightMonth = 0 totalWeightMonthB = 0 weightedVals = [] weightedValsB = [] for j, pred in preds.iterrows(): currentMonth = pred['MONTH'] currentWeight = gauss_function(currentMonth, 1, monthEstimation, 2) * pred['GRADE'] weightedVal = currentWeight * pred['FORECAST'] currentWeightB = indicatrice_function(currentMonth, monthEstimation) weightedValB = currentWeightB * pred['FORECAST'] totalWeightMonth += currentWeight totalWeightMonthB += currentWeightB weightedVals.append(weightedVal) weightedValsB.append(weightedValB) if totalWeightMonth > 0: estimation = sum(weightedVals) / totalWeightMonth estimationB = sum(weightedValsB) / totalWeightMonthB else: estimation = 0 estimationB = 0 # Best forecast bestForecast = estimation worstForecast = estimation maskGradesMonth = (grades['COMPANY'] == company) & (grades['MONTH'] == monthEstimation) thisMonth = preds.loc[maskGradesMonth] if len(thisMonth) > 0: index = thisMonth['GRADE'].argmax() bestForecast = preds.loc[index]['FORECAST'].item() index = thisMonth['GRADE'].argmin() worstForecast = preds.loc[index]['FORECAST'].item() forecast.loc[len(forecast)] = [ company, monthEstimation, estimation, 0, bestForecast, estimationB ] return forecast