<s> from __future__ import division <EOL> from Randomforestlearner import Randomforestlearner <EOL> import numpy <EOL> import datetime <EOL> import os <EOL> import matplotlib . pyplot as plt <EOL> import numpy as np <EOL> import math <EOL> def get_graph_two_plots ( x_series , y_series , y1_series , xlabel , ylabel , name_file ) : <EOL> plt . clf ( ) <EOL> plt . xlabel ( xlabel ) <EOL> plt . ylabel ( ylabel ) <EOL> first = plt . plot ( x_series , y_series , color = '<STR_LIT:r>' ) <EOL> second = plt . plot ( x_series , y1_series , color = '<STR_LIT:b>' ) <EOL> plt . legend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> plt . savefig ( name_file ) <EOL> def get_correlation ( Y_return , Y_test ) : <EOL> covariance_matrix = np . corrcoef ( Y_return , np . squeeze ( np . asarray ( Y_test ) ) ) <EOL> return covariance_matrix [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> def get_rmse ( Y_return , Y_Test ) : <EOL> return math . sqrt ( np . mean ( np . square ( Y_return - Y_Test ) ) ) <EOL> def scatter ( Y_return , Y_test , name_file ) : <EOL> plt . clf ( ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:6> , <NUM_LIT:5> ) ) <EOL> graph = fig . add_subplot ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> graph . scatter ( Y_return , Y_test ) <EOL> graph . set_title ( "<STR_LIT>" ) <EOL> graph . set_xlabel = "<STR_LIT>" <EOL> graph . set_ylabel = "<STR_LIT:Y>" <EOL> fig . savefig ( name_file ) <EOL> def all_feature_graph ( y1 , y2 , y3 , y4 , y5 , x , xlabel , ylabel , name_file ) : <EOL> plt . clf ( ) <EOL> plt . xlabel ( xlabel ) <EOL> plt . ylabel ( ylabel ) <EOL> first = plt . plot ( x , y1 ) <EOL> second = plt . plot ( x , y2 ) <EOL> third = plt . plot ( x , y3 ) <EOL> fourth = plt . plot ( x , y4 ) <EOL> fifth = plt . plot ( x , y5 ) <EOL> plt . legend ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> plt . savefig ( name_file ) <EOL> def read_file ( filename ) : <EOL> required_all = list ( ) <EOL> inf = open ( filename ) <EOL> for s in reversed ( inf . readlines ( ) ) : <EOL> all_needed = s . split ( '<STR_LIT:U+002C>' ) <EOL> required = all_needed [ : <NUM_LIT:2> ] <EOL> if ( all_needed [ <NUM_LIT:0> ] != '<STR_LIT>' ) : <EOL> required_all . append ( float ( required [ <NUM_LIT:1> ] ) ) <EOL> return required_all <EOL> def get_mean ( dataset_21 ) : <EOL> return numpy . mean ( dataset_21 ) <EOL> def get_stddev ( dataset_21 ) : <EOL> return numpy . std ( dataset_21 ) <EOL> def get_relative_strength_idx ( dataset_21 ) : <EOL> comparer = dataset_21 [ <NUM_LIT:0> ] <EOL> gain = <NUM_LIT:0.0> <EOL> loss = <NUM_LIT:0.0> <EOL> new_dataset = dataset_21 [ <NUM_LIT:0> : ] <EOL> for each in new_dataset : <EOL> if each > comparer : <EOL> gain = gain + ( each - comparer ) <EOL> elif each < comparer : <EOL> loss = loss + ( comparer - each ) <EOL> if loss == <NUM_LIT:0.0> : <EOL> return <NUM_LIT> <EOL> elif gain == <NUM_LIT:0.0> : <EOL> return <NUM_LIT:0.0> <EOL> else : <EOL> rs = ( gain / len ( new_dataset ) ) / ( loss / len ( new_dataset ) ) <EOL> rsi = float ( <NUM_LIT:100> - ( <NUM_LIT:100> / ( <NUM_LIT:1> + rs ) ) ) <EOL> return rsi <EOL> def roc ( dataset_21 ) : <EOL> latest = dataset_21 [ len ( dataset_21 ) - <NUM_LIT:1> ] <EOL> oldest = dataset_21 [ <NUM_LIT:0> ] <EOL> ROC = ( latest - oldest ) / ( oldest ) * <NUM_LIT:100> <EOL> return ROC <EOL> def slope ( dataset_21 ) : <EOL> x = numpy . array ( [ i for i in range ( <NUM_LIT:1> , <NUM_LIT> ) ] ) <EOL> y = numpy . array ( [ data for data in dataset_21 ] ) <EOL> A = numpy . vstack ( [ x , numpy . ones ( len ( x ) ) ] ) . T <EOL> m , c = numpy . linalg . lstsq ( A , y ) [ <NUM_LIT:0> ] <EOL> return m <EOL> def createX ( datesandY ) : <EOL> totalX = [ ] <EOL> index = <NUM_LIT> <EOL> start = <NUM_LIT:0> <EOL> while index <= len ( datesandY ) - <NUM_LIT:5> : <EOL> dataset_21 = datesandY [ start : index ] <EOL> totalX . append ( [ get_mean ( dataset_21 ) , get_stddev ( dataset_21 ) , get_relative_strength_idx ( dataset_21 ) , roc ( dataset_21 ) , slope ( dataset_21 ) , datesandY [ index + <NUM_LIT:4> ] - datesandY [ index ] ] ) <EOL> start = start + <NUM_LIT:1> <EOL> index = index + <NUM_LIT:1> <EOL> return numpy . array ( totalX ) <EOL> def main ( ) : <EOL> curr_dirr = os . getcwd ( ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> datesandY = read_file ( '<STR_LIT>' ) <EOL> stack = createX ( datesandY ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> datesandY = read_file ( '<STR_LIT>' + str ( i ) + '<STR_LIT>' ) <EOL> learnedvalues = createX ( datesandY ) <EOL> stack = numpy . vstack ( ( stack , learnedvalues ) ) <EOL> for i in range ( <NUM_LIT:11> , <NUM_LIT:100> ) : <EOL> datesandY = read_file ( '<STR_LIT>' + str ( i ) + '<STR_LIT>' ) <EOL> learnedvalues = createX ( datesandY ) <EOL> stack = numpy . vstack ( ( stack , learnedvalues ) ) <EOL> print len ( stack ) <EOL> testdatesandY = read_file ( '<STR_LIT>' ) <EOL> test = createX ( testdatesandY ) <EOL> ( XTrain , YTrain ) = numpy . split ( stack , [ <NUM_LIT:5> ] , axis = <NUM_LIT:1> ) <EOL> ( XTest , YTest ) = numpy . split ( test , [ <NUM_LIT:5> ] , axis = <NUM_LIT:1> ) <EOL> randomforestlearner = Randomforestlearner ( k = <NUM_LIT:50> ) <EOL> randomforestlearner . addEvidence ( XTrain , YTrain ) <EOL> Y_Return = numpy . multiply ( numpy . array ( randomforestlearner . query ( XTest ) ) , - <NUM_LIT:1> ) <EOL> Y_Test = np . squeeze ( np . asarray ( YTest ) ) <EOL> start = <NUM_LIT:0> <EOL> index = <NUM_LIT:5> <EOL> print len ( Y_Return ) <EOL> print len ( Y_Test ) <EOL> print len ( testdatesandY ) <EOL> while index < len ( testdatesandY ) - <NUM_LIT> : <EOL> Y_Return [ start ] = Y_Return [ start ] + testdatesandY [ index ] <EOL> Y_Test [ start ] = Y_Test [ start ] + testdatesandY [ index ] <EOL> start = start + <NUM_LIT:1> <EOL> index = index + <NUM_LIT:1> <EOL> os . chdir ( curr_dirr ) <EOL> get_graph_two_plots ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , Y_Return [ : <NUM_LIT:100> ] , Y_Test [ : <NUM_LIT:100> ] , "<STR_LIT>" , "<STR_LIT:Y>" , "<STR_LIT>" ) <EOL> last126_test = Y_Test [ - <NUM_LIT> : ] <EOL> last126_return = Y_Return [ - <NUM_LIT> : ] <EOL> get_graph_two_plots ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , last126_return [ : <NUM_LIT:100> ] , last126_test [ : <NUM_LIT:100> ] , "<STR_LIT>" , "<STR_LIT:Y>" , "<STR_LIT>" ) <EOL> scatter ( Y_Return , Y_Test , "<STR_LIT>" ) <EOL> mean_series = XTest [ : , <NUM_LIT:0> ] <EOL> std_series = XTest [ : , <NUM_LIT:1> ] <EOL> rsi_series = XTest [ : , <NUM_LIT:2> ] <EOL> roc_series = XTest [ : , <NUM_LIT:3> ] <EOL> slope_series = XTest [ : , <NUM_LIT:4> ] <EOL> all_feature_graph ( mean_series [ : <NUM_LIT:100> ] , std_series [ : <NUM_LIT:100> ] , rsi_series [ : <NUM_LIT:100> ] , roc_series [ : <NUM_LIT:100> ] , slope_series [ : <NUM_LIT:100> ] , numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> print "<STR_LIT>" . format ( get_correlation ( Y_Test , Y_Return ) ) <EOL> print "<STR_LIT>" . format ( get_rmse ( Y_Test , Y_Return ) ) <EOL> os . chdir ( '<STR_LIT>' ) <EOL> testdatesandY = read_file ( '<STR_LIT>' ) <EOL> test = createX ( testdatesandY ) <EOL> ( XTrain , YTrain ) = numpy . split ( stack , [ <NUM_LIT:5> ] , axis = <NUM_LIT:1> ) <EOL> ( XTest , YTest ) = numpy . split ( test , [ <NUM_LIT:5> ] , axis = <NUM_LIT:1> ) <EOL> randomforestlearner = Randomforestlearner ( k = <NUM_LIT:50> ) <EOL> randomforestlearner . addEvidence ( XTrain , YTrain ) <EOL> Y_Return = numpy . multiply ( numpy . array ( randomforestlearner . query ( XTest ) ) , - <NUM_LIT:1> ) <EOL> Y_Test = np . squeeze ( np . asarray ( YTest ) ) <EOL> start = <NUM_LIT:0> <EOL> index = <NUM_LIT:5> <EOL> print len ( Y_Return ) <EOL> print len ( Y_Test ) <EOL> print len ( testdatesandY ) <EOL> while index < len ( testdatesandY ) - <NUM_LIT> : <EOL> Y_Return [ start ] = Y_Return [ start ] + testdatesandY [ index ] <EOL> Y_Test [ start ] = Y_Test [ start ] + testdatesandY [ index ] <EOL> start = start + <NUM_LIT:1> <EOL> index = index + <NUM_LIT:1> <EOL> os . chdir ( curr_dirr ) <EOL> get_graph_two_plots ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , Y_Return [ : <NUM_LIT:100> ] , Y_Test [ : <NUM_LIT:100> ] , "<STR_LIT>" , "<STR_LIT:Y>" , "<STR_LIT>" ) <EOL> last126_test = Y_Test [ - <NUM_LIT> : ] <EOL> last126_return = Y_Return [ - <NUM_LIT> : ] <EOL> get_graph_two_plots ( numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , last126_return [ : <NUM_LIT:100> ] , last126_test [ : <NUM_LIT:100> ] , "<STR_LIT>" , "<STR_LIT:Y>" , "<STR_LIT>" ) <EOL> scatter ( Y_Return , Y_Test , "<STR_LIT>" ) <EOL> mean_series = XTest [ : , <NUM_LIT:0> ] <EOL> std_series = XTest [ : , <NUM_LIT:1> ] <EOL> rsi_series = XTest [ : , <NUM_LIT:2> ] <EOL> roc_series = XTest [ : , <NUM_LIT:3> ] <EOL> slope_series = XTest [ : , <NUM_LIT:4> ] <EOL> all_feature_graph ( mean_series [ : <NUM_LIT:100> ] , std_series [ : <NUM_LIT:100> ] , rsi_series [ : <NUM_LIT:100> ] , roc_series [ : <NUM_LIT:100> ] , slope_series [ : <NUM_LIT:100> ] , numpy . arange ( <NUM_LIT:1> , <NUM_LIT> ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> print "<STR_LIT>" . format ( get_correlation ( Y_Test , Y_Return ) ) <EOL> print "<STR_LIT>" . format ( get_rmse ( Y_Test , Y_Return ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
