Beispiel #1
0
    def root_mean_square_error(y_true, y_pred, n=0):
        """Root mean squared error.

        Args:
            y_true -- truth value
            y_pred -- Predicted value
            n -- number of periods
        """
        if n == 0:
            # This is directly from the sklearn metric
            mse = sk_mse(y_true, y_pred)
            result = math.sqrt(mse)
            return result
        else:
            # This is directly from the sklearn metric
            mse = sk_mse(y_true, y_pred, n)
            result = math.sqrt(mse)
            return result
Beispiel #2
0
    def mean_squared_error(y_true, y_pred):
        """Root mean squared error (Overloaded from sklearn).

        Args:
            y_true -- truth value
            y_pred -- Predicted value
            n -- number of periods
        """
        mse = sk_mse(y_true, y_pred)
        return mse
Beispiel #3
0
    def predict(self,
                stock_prices,
                stock_processor,
                window_len=5,
                step_size=1,
                plot=True):
        '''
        Predict stock prices for the given day given last day
        Runs for the entire length of stock given
        Prints RMSE
        ::param stock_prices np.array - prices normalised and stationary
        ::param stock_processor StockProcessor instance (for denorming etc)
        ::param window_len int number of days to predict
        ::param step_size int step size of window
        '''
        preds = []
        logging.debug(stock_prices[0])
        stock_windows = rolling_window(stock_prices, window_len, step_size)
        for idx, price_window in enumerate(stock_windows[:, ]):
            if idx == 0:
                preds.append(np.zeros(5))
            else:
                preds.append(stock_windows[idx - 1])
        #Check shapes
        preds = np.array(preds)
        logging.debug(preds[0:3])
        try:
            assert preds.shape == stock_windows.shape
        except AssertionError:
            logging.exception('Shape error: %s, %s', preds.shape,
                              stock_windows.shape)
        '''
        #De norm and re-trend to get real prices
        preds = stock_processor.de_normalise(preds)
        stock_prices = stock_processor.de_normalise(stock_prices)

        preds = stock_processor.re_trend(preds)
        stock_prices = stock_processor.re_trend(stock_prices)
        '''
        #Check error
        rmse = sqrt(sk_mse(stock_windows, preds))
        logging.info('Previous Window Method RMSE:%s', rmse)
        if plot:
            plt.plot(stock_prices, label='Stock')
            plt.plot(preds, label='Preds')
            leg = plt.legend()
            plt.show()
Beispiel #4
0
def print_accuracy():
    # Cosine mse
    y_true = [None, None]
    y_true[0] = [0.0 for i in range(len(_cosine_pred[0]))]
    y_true[1] = [1.0 for i in range(len(_cosine_pred[1]))]
    cosine_dis_mse = sk_mse(_cosine_pred[0], y_true[0])
    cosine_sim_mse = sk_mse(_cosine_pred[1], y_true[1])
    cosine_tot_mse = sk_mse(_cosine_pred[0] + _cosine_pred[1], y_true[0] + y_true[1]) 

    simnet_dis_mse = sk_mse(_simnet_pred[0], y_true[0])
    simnet_sim_mse = sk_mse(_simnet_pred[1], y_true[1])
    simnet_tot_mse = sk_mse(_simnet_pred[0] + _simnet_pred[1], y_true[0] + y_true[1])
    print("Disimilarity error: cosine={}, simnet={}", cosine_dis_mse, simnet_dis_mse)
    print("Similarity error: cosine={}, simnet={}", cosine_sim_mse, simnet_sim_mse)
    print("Total error: cosine={}, simnet={}", cosine_tot_mse, simnet_tot_mse)
Beispiel #5
0
    def predict(self, stock_prices, stock_processor, plot=True):
        '''
        Predict stock prices for the given day given last day
        Runs for the entire length of stock given
        Prints RMSE
        ::param stock_prices np.array - prices normalised and stationary
        ::param stock_processor StockProcessor instance (for denorming etc)
        ::param time_period int number of days to predict
        '''
        preds = []
        logging.debug(stock_prices[0])
        for idx, price in enumerate(stock_prices[:]):
            if idx == 0:
                preds.append(0.0)
            else:
                preds.append(stock_prices[idx - 1])
        #Check shapes
        preds = np.array(preds)
        try:
            assert preds.shape[0] == stock_prices.shape[0]
        except AssertionError:
            logging.exception('Shape error: %s, %s', preds.shape[0],
                              stock_prices.shape[0])

        #De norm and re-trend to get real prices
        preds = stock_processor.de_normalise(preds)
        stock_prices = stock_processor.de_normalise(stock_prices)

        preds = stock_processor.re_trend(preds)
        stock_prices = stock_processor.re_trend(stock_prices)

        #Check error
        rmse = sqrt(sk_mse(stock_prices, preds))
        logging.info('Previous Method RMSE:%s', rmse)
        if plot:
            plt.plot(stock_prices, label='Stock')
            plt.plot(preds, label='Preds')
            leg = plt.legend()
            plt.show()
Beispiel #6
0
 def mse(self, predictions, targets):
     """
     Calculate Mean Squered Error
     """
     mse = sk_mse(predictions, targets)
     return mse
Beispiel #7
0
    pred_outputs = sp.de_normalise(np.array(pred_outputs))
    tst_outputs = sp.de_normalise(np.array(tst_outputs))

    silver_prices = silver_prices.reshape(gdelt_feats.shape[0])
    silver_prices = sp.de_normalise(silver_prices[train_outputs.shape[0]:])
    logging.info(silver_prices.shape)

    #Not sure this is actually working for test data shifted - esp windows
    '''
    pred_outputs = sp.re_trend(pred_outputs)
    tst_outputs = sp.re_trend(tst_outputs)
    prices = sp.re_trend(prices)
    logging.debug('%s, %s', prices[0:3], prices.shape)
    '''

    rmse = sqrt(sk_mse(tst_outputs[:-1], pred_outputs[:-1]))
    logging.info('RMS: %s', rmse)
    '''
    plt.plot(prices, label='Stock')
    i = 0
    #Only plot a few of them
    for idx in range(0, pred_outputs.shape[0], WINDOW_SIZE):
        #Plot individual windows - in the right place
        X = [idx+i for i in range(WINDOW_SIZE)]
        logging.debug([X, pred_outputs[idx]])
        i+=1
        #if i > 10:
        #    break
        plt.plot(X, pred_outputs[idx], label='Preds')
    leg = plt.legend()
    plt.show()