コード例 #1
0
def main(tickers=['AAPL'], start=None, end=None):
    data = OrderedDict()

    for ticker in tickers:
        data[ticker] = fc.get_time_series(ticker, start, end)

        # log_returns
        data[ticker]['log_returns'] = np.log(
            data[ticker]['adj_close'] / data[ticker]['adj_close'].shift(1))

        data[ticker]['log_returns'].dropna(inplace=True)

        # plotting the histogram of returns
        fc.plot_histogram(y=data[ticker]['log_returns'], ticker=ticker)

        fc.plot_time_series(y=data[ticker]['log_returns'],
                            lags=30,
                            ticker=ticker)
コード例 #2
0
def main(tickers='AAPL', start=None, end=None, n_steps=21):
    data = OrderedDict()
    pred_data = OrderedDict()
    forecast_data = OrderedDict()

    for ticker in tickers:
        data[ticker] = fc.get_time_series(ticker, start, end)

        # log_returns
        data[ticker]['log_returns'] = np.log(
            data[ticker]['adj_close'] / data[ticker]['adj_close'].shift(1))

        data[ticker]['log_returns'].dropna(inplace=True)

        # plotting the histogram of returns
        fc.plot_histogram(data[ticker]['log_returns'])

        fc.plot_time_series(data[ticker]['log_returns'], lags=30)

        print("{} Series\n"
              "-------------\n"
              "mean: {:.3f}\n"
              "median: {:.3f}\n"
              "maximum: {:.3f}\n"
              "minimum: {:.3f}\n"
              "variance: {:.3f}\n"
              "standard deviation: {:.3f}\n"
              "skewness: {:.3f}\n"
              "kurtosis: {:.3f}".format(ticker,
                                        data[ticker]['adj_close'].mean(),
                                        data[ticker]['adj_close'].median(),
                                        data[ticker]['adj_close'].max(),
                                        data[ticker]['adj_close'].min(),
                                        data[ticker]['adj_close'].var(),
                                        data[ticker]['adj_close'].std(),
                                        data[ticker]['adj_close'].skew(),
                                        data[ticker]['adj_close'].kurtosis()))

        adfstat, pvalue, critvalues, resstore, dagostino_results, shapiro_results, ks_results, anderson_results, kpss_results = fc.get_stationarity_statistics(
            data[ticker]['log_returns'].values)

        print(
            "{} Stationarity Statistics\n"
            "-------------\n"
            "Augmented Dickey-Fuller unit root test: {}\n"
            "MacKinnon’s approximate p-value: {}\n"
            "Critical values for the test statistic at the 1 %, 5 %, and 10 % levels: {}\n"
            "D’Agostino and Pearson’s normality test: {}\n"
            "Shapiro-Wilk normality test: {}\n"
            "Kolmogorov-Smirnov goodness of fit test: {}\n"
            "Anderson-Darling test: {}\n"
            "Kwiatkowski, Phillips, Schmidt, and Shin (KPSS) stationarity test: {}"
            .format(ticker, adfstat, pvalue, critvalues, dagostino_results,
                    shapiro_results, ks_results, anderson_results,
                    kpss_results))

        # Fit ARMA model to AAPL returns
        res_tup = fc.get_best_arma_model(data[ticker]['log_returns'])

        res_tup[2].summary()

        # verify stationarity
        adfstat, pvalue, critvalues, resstore, dagostino_results, shapiro_results, ks_results, anderson_results, kpss_results = fc.get_stationarity_statistics(
            res_tup[2].resid.values)

        print(
            "Stationarity Statistics\n"
            "-------------\n"
            "Augmented Dickey-Fuller unit root test: {}\n"
            "MacKinnon’s approximate p-value: {}\n"
            "Critical values for the test statistic at the 1 %, 5 %, and 10 % levels: {}\n"
            "D’Agostino and Pearson’s normality test: {}\n"
            "Shapiro-Wilk normality test: {}\n"
            "Kolmogorov-Smirnov goodness of fit test: {}\n"
            "Anderson-Darling test: {}\n"
            "Kwiatkowski, Phillips, Schmidt, and Shin (KPSS) stationarity test: {}"
            .format(adfstat, pvalue, critvalues, dagostino_results,
                    shapiro_results, ks_results, anderson_results,
                    kpss_results))

        fc.plot_histogram(y=res_tup[2].resid, ticker=ticker, title='ARMA')

        fc.plot_time_series(y=res_tup[2].resid,
                            lags=30,
                            ticker=ticker,
                            title='ARMA')

        # cross-validation testing
        split = rand.uniform(0.60, 0.80)

        train_size = int(len(data[ticker]) * split)

        train, test = data[ticker][0:train_size], data[ticker][
            train_size:len(data[ticker])]

        # in-sample prediction
        pred_data[ticker] = res_tup[2].predict(start=len(train),
                                               end=len(train) + len(test) - 1)

        pred_results = pd.DataFrame(data=dict(
            original=test['log_returns'], prediction=pred_data[ticker].values),
                                    index=test.index)

        print('{} Original Sharpe Ratio:'.format(ticker),
              fc.get_sharpe_ratio(returns=pred_results['original']))
        print('{} Prediction Sharpe Ratio:'.format(ticker),
              fc.get_sharpe_ratio(returns=pred_results['prediction']))

        # prediction plot
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(pred_results['original'])
        ax.plot(pred_results['prediction'])
        ax.set(title='{} ARMA{} In-Sample Return Prediction'.format(
            ticker, res_tup[1]),
               xlabel='time',
               ylabel='$')
        ax.legend(['Original', 'Prediction'])
        fig.tight_layout()
        fig.savefig(
            'charts/{}-ARMA-In-Sample-Return-Prediction'.format(ticker))

        # out-of-sample forecast
        forecast_data[ticker] = res_tup[2].forecast(steps=n_steps)

        # forecast plot
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(forecast_data[ticker][0])
        ax.set(
            title='{} Day {} ARMA{} Out-of-Sample Return Forecast.png'.format(
                n_steps, ticker, res_tup[1]),
            xlabel='time',
            ylabel='$')
        ax.legend(['Forecast'])
        fig.tight_layout()
        fig.savefig(
            'charts/{}-Day-{}-ARMA-Out-of-Sample-Return-Forecast.png'.format(
                n_steps, ticker))

    # end of day plot of all tickers
    fig = plt.figure()
    ax = fig.add_subplot(111)
    for ticker in tickers:
        ax.plot(data[ticker]['adj_close'])
    ax.set(title='Time series plot', xlabel='time', ylabel='$')
    ax.legend(tickers)
    fig.tight_layout()
    fig.savefig('charts/stocks.png')

    return forecast_data
コード例 #3
0
def get_features_and_positions(image_rgb,
                               rotation_angle=0,
                               classifier=1,
                               rotation_invariance=1,
                               class_names=[]):
    """
    Returns the position of the objects in the given image, with their meaning (its class label)

    Parameters
    ----------
    image_rgb : RBG image as np.array
        Input image where to look for the objects.
    rotation_angle : float, optional
        Every image is randomly rotated by a angle within this range (for assessment of the robustness). The default is 0.
    classifier : int, optional, either 1 or 2
        Index of the classifier to use. The default is 1. 
    rotation_invariance : Bool, optional
        if 1 or 'True', then f-folds with 37 rotated images are averaged to classify more robustly. The default is 1.
    class_names : [String], optional
        Just for debugging. The default is [].

    Returns
    -------
    list
        position of the objects in the given image, with their meaning (its class label).

    """

    fc.plot_image(image_rgb)
    image = rgb2gray(image_rgb)
    t = skimage.filters.threshold_otsu(image) + 0.05
    bw = image < t
    fc.plot_image(bw, name="debuging/bw.png")
    fc.plot_histogram(image)

    # label image regions
    label_image = label(bw)
    regions = regionprops(label_image)
    fc.plot_image(label_image, name="debuging/label_image.png")

    # remove from the labels the 'useless' ones
    for i, region in enumerate(regions):
        minr, minc, maxr, maxc = region.bbox
        if maxr - minr > 200 or region.eccentricity > 0.99:
            coords = region.coords
            label_image[coords[:, 0], coords[:, 1]] = 0
    regions = np.array(regionprops(label_image))

    # Find the position of the robot
    robot_region = None
    for i, region in enumerate(regions):
        minr, minc, maxr, maxc = region.bbox
        if region.area > 400:
            robot_region = region

    # Remove from the label map the robot
    region_centers = [np.array(r.centroid) for r in regions]
    robot_center = np.array(robot_region.centroid)
    distances_from_robot = np.array(
        [np.linalg.norm(c - robot_center) for c in region_centers])
    regions_to_remove = regions[distances_from_robot < 60]
    for region in regions_to_remove:
        coords = region.coords
        label_image[coords[:, 0], coords[:, 1]] = 0
    regions = np.array(regionprops(label_image))

    # Merge labels that are really close
    # (So we create a distance matrix, for all the left regions...)
    region_centers = [np.array(r.centroid) for r in regions]
    distance_matrix = np.array(
        [[np.linalg.norm(x - y) for y in region_centers]
         for x in region_centers])
    close_regions = np.logical_and(distance_matrix > 0, distance_matrix < 30)
    close_regions = np.triu(close_regions)
    regions_to_merge = np.argwhere(close_regions)
    for set in regions_to_merge:
        r1, r2 = regions[set[0]], regions[set[1]]
        coords = r1.coords
        label_image[coords[:, 0], coords[:, 1]] = r2.label
    regions = np.array(regionprops(label_image))

    # Remove the regions with not enough points
    for i, region in enumerate(regions):
        if region.area < 40:
            coords = region.coords
            label_image[coords[:, 0], coords[:, 1]] = 0
    regions = np.array(regionprops(label_image))

    # Draw all of this on a single figure
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(label_image, cmap='jet')
    for region in regions:
        minr, minc, maxr, maxc = region.bbox
        rect = mpatches.Rectangle((minc, minr),
                                  maxc - minc,
                                  maxr - minr,
                                  fill=False,
                                  edgecolor='red',
                                  linewidth=2)
        ax.add_patch(rect)

    # Extract the images (and check if it is blue or not)
    fig, axs = plt.subplots(1, len(regions))
    imgs = []
    imgs_rgb = []
    blue_images = np.zeros(len(regions))
    for i, region in enumerate(regions):
        img = fc.rescale_feature(region.image)
        bb = region.bbox
        img_rgb = image_rgb[bb[0]:bb[2], bb[1]:bb[3], :]
        if fc.blue_detect(image_rgb[region.coords[:, 0], region.coords[:,
                                                                       1], :]):
            blue_images[i] = 1
        imgs.append(img)
        imgs_rgb.append(img_rgb)
        axs[i].imshow(img)
        axs[i].axis('off')
    imgs = np.array(imgs)
    np.save("imgs", imgs)

    # Randomly rotate the images obtained (for rotation invariance)
    for i, img in enumerate(imgs):
        angle = (np.random.random() - 0.5) * rotation_angle
        imgs[i] = rotate(img, angle)

    # Classification
    print("- classification with classifier {}".format(classifier))
    if classifier == 1:
        model = keras.models.load_model('cnn_classifier_with_operators')
        classes = classifiers.classifier_1(
            imgs, model=model, rotation_invariance=rotation_invariance)
    if classifier == 2:
        model = keras.models.load_model('cnn_classifier_without_operator')
        classes = classifiers.classifier_2(
            imgs,
            blue_images,
            model=model,
            rotation_invariance=rotation_invariance)

    # Plotting classification results
    fig, axs = plt.subplots(1, len(imgs))
    for i, img in enumerate(imgs):
        axs[i].imshow(img[:, :])
        axs[i].axis('off')
        title = '{}'.format(class_names[int(classes[i])])
        axs[i].set_title(title)
    fig.savefig("debuging/objects.png")

    return [(np.array(r.centroid), int(c)) for r, c in zip(regions, classes)]