def build_episodes(episodesParams): fromDt = date.from_yyyymmdd(episodesParams['fromDate']) toDt = date.from_yyyymmdd(episodesParams['toDate']) testDays = episodesParams['testDays'] validateRatio = episodesParams['validateRatio'] trainRatio = episodesParams['trainRatio'] validateDays = testDays * validateRatio trainDays = testDays * trainRatio episodeDays = trainDays + validateDays + testDays diffDays = date.days_between(fromDt, toDt) numEpisodes = int(diffDays / float(testDays)) subArrays = np.array_split(np.array(range(diffDays)), numEpisodes) episodeFrom_ = [int(subArray[0]) for subArray in subArrays[:-(trainRatio + validateRatio)]] trainFrom_ = [date.add_days(fromDt, episodeFrom) for episodeFrom in episodeFrom_] trainTo_ = [date.add_days(fromDt, episodeFrom + trainDays) for episodeFrom in episodeFrom_] validateFrom_ = [date.add_days(fromDt, episodeFrom + trainDays + 1) for episodeFrom in episodeFrom_] validateTo_ = [date.add_days(fromDt, episodeFrom + trainDays + validateDays) for episodeFrom in episodeFrom_] testFrom_ = [date.add_days(fromDt, episodeFrom + trainDays + validateDays + 1) for episodeFrom in episodeFrom_] testTo_ = [date.add_days(fromDt, episodeFrom + trainDays + validateDays + testDays) for episodeFrom in episodeFrom_] trainFromTo_ = zip(trainFrom_, trainTo_) validateFromTo_ = zip(validateFrom_, validateTo_) testFromTo_ = zip(testFrom_, testTo_) episodes = {'fromDate' : fromDt, 'toDate' : toDt, 'num' : len(episodeFrom_), 'train' : trainFromTo_, 'validate' : validateFromTo_, 'test' : testFromTo_} return episodes
def __init__(self, portfolio): fromDt = date.from_yyyymmdd(portfolio['fromDate']) toDt = date.from_yyyymmdd(portfolio['toDate']) elements = portfolio['elements'] self.iMax = len(elements) tradable_ = [Feature(element['tradable'], fromDt, toDt) for element in elements] observable__ = [[Feature(observable, fromDt, toDt) for observable in element['observables']] for element in elements] self.c_ = np.array([element['cost'] for element in elements], float) self.jLen_ = np.array([len(observable_) + 2 for observable_ in observable__]) self.jLen = np.sum(self.jLen_) jTo_ = np.cumsum(self.jLen_) jFrom_ = np.roll(jTo_, 1) jFrom_[0] = 0 self.jFromTo_ = zip(jFrom_, jTo_) feature_ = tradable_ + [item for sublist in observable__ for item in sublist] commonDate_ = Feature.common_dates(feature_) for feature in feature_: feature.filter_dates(commonDate_) self.date_Orig = commonDate_ self.observation__ = [[observable.value_ for observable in observable_] for observable_ in observable__] price__ = np.swapaxes(np.array([tradable.value_ for tradable in tradable_], float), 0, 1) self.r__Orig = price__ - np.roll(price__, 1, 0) self.r__Orig[0] = 0
def __init__(self, key, fromDt, toDt): path = util.featurePath("%s.feature" % key) yyyymmdd_, self.value_ = np.loadtxt(open(path, 'r'), delimiter = ',', unpack = True) yyyymmdd_ = yyyymmdd_.astype(int) self.date_ = [date.from_yyyymmdd(yyyymmdd) for yyyymmdd in yyyymmdd_] self.range_dates(fromDt, toDt)