示例#1
0
def returns_plot(dates: List[str], time_step: str) -> None:
    """Plots the returns of five stocks.

    :param dates: List of the interval of dates to be analyzed
     (i.e. ['1980-01', '2020-12']).
    :param time_step: time step of the data (i.e. '1m', '2m', '5m', ...).
    :return: None -- The function saves the plot in a file and does not return
     a value.
    """

    function_name: str = returns_plot.__name__
    exact_distributions_correlation_tools.function_header_print_plot(
        function_name, dates, time_step
    )

    try:

        # Load data
        returns_data: pd.DataFrame = pickle.load(
            open(
                f"../data/exact_distributions_correlation/returns_data"
                + f"_{dates[0]}_{dates[1]}_step_{time_step}.pickle",
                "rb",
            )
        ).iloc[:, :5]

        plot: np.ndarray = returns_data.plot(
            subplots=True, sharex=True, figsize=(16, 16), grid=True, sort_columns=True
        )

        _ = [ax.set_ylabel("Returns", fontsize=20) for ax in plot]
        _ = [plot.legend(loc=1, fontsize=20) for plot in plt.gcf().axes]
        plt.xlabel(f"Date - {time_step}", fontsize=20)
        plt.tight_layout(pad=0.5)
        figure: plt.Figure = plot[0].get_figure()

        # Plotting
        exact_distributions_correlation_tools.save_plot(
            figure, function_name, dates, time_step
        )

        plt.close()
        del returns_data
        del figure
        gc.collect()

    except FileNotFoundError as error:
        print("No data")
        print(error)
        print()
示例#2
0
def aggregated_dist_returns_market_plot(dates: List[str], time_step: str) -> None:
    """Plots the aggregated distribution of returns for a market.

    :param dates: List of the interval of dates to be analyzed
     (i.e. ['1980-01', '2020-12']).
    :param time_step: time step of the data (i.e. '1m', '2m', '5m', ...).
    :return: None -- The function saves the plot in a file and does not return
     a value.
    """

    function_name: str = aggregated_dist_returns_market_plot.__name__
    exact_distributions_correlation_tools.function_header_print_plot(
        function_name, dates, time_step
    )

    try:

        # Load data
        agg_returns_data: pd.Series = pickle.load(
            open(
                "../data/exact_distributions_correlation/aggregated_dist_returns"
                + f"_market_data_{dates[0]}_{dates[1]}_step_{time_step}.pickle",
                "rb",
            )
        )

        agg_returns_data = agg_returns_data.rename("Agg. returns")

        x_gauss: np.ndarray = np.arange(-10, 10, 0.1)
        gaussian: np.ndarray = (
            exact_distributions_correlation_tools.gaussian_distribution(0, 1, x_gauss)
        )

        # Log plot
        plot_log = agg_returns_data.plot(
            kind="density", style="-", logy=True, figsize=(16, 9), legend=True, lw=3
        )

        plt.semilogy(x_gauss, gaussian, "o", lw=3, label="Gaussian")

        plt.legend(fontsize=20)
        plt.title(
            f"Aggregated distribution returns from {dates[0]} to"
            + f" {dates[1]} - {time_step}",
            fontsize=30,
        )
        plt.xlabel("Aggregated returns", fontsize=25)
        plt.ylabel("PDF", fontsize=25)
        plt.xticks(fontsize=15)
        plt.yticks(fontsize=15)
        plt.xlim(-10, 10)
        plt.ylim(10 ** -9, 1)
        plt.grid(True)
        plt.tight_layout()
        figure_log: plt.Figure = plot_log.get_figure()

        left, bottom, width, height = [0.36, 0.13, 0.35, 0.3]
        ax2 = figure_log.add_axes([left, bottom, width, height])
        agg_returns_data.plot(kind="density", style="-", legend=False, lw=3)
        ax2.plot(x_gauss, gaussian, "o")
        plt.xlim(-2, 2)
        plt.ylim(0, 0.6)
        plt.grid(True)

        # Plotting
        exact_distributions_correlation_tools.save_plot(
            figure_log, function_name + "_log", dates, time_step
        )

        plt.close()
        del agg_returns_data
        del figure_log
        del plot_log
        gc.collect()

    except FileNotFoundError as error:
        print("No data")
        print(error)
        print()
示例#3
0
def pdf_log_all_distributions_plot(dates: List[str], time_step: str) -> None:
    """Plots all the distributions and compares with agg. returns of a market.

    :param dates: List of the interval of dates to be analyzed
     (i.e. ['1980-01', '2020-12']).
    :param time_step: time step of the data (i.e. '1m', '2m', '5m', ...).
    :return: None -- The function saves the plot in a file and does not return
     a value.
    """

    function_name: str = pdf_log_all_distributions_plot.__name__
    exact_distributions_correlation_tools.function_header_print_plot(
        function_name, dates, time_step
    )

    try:

        # Load data
        agg_returns_data: pd.Series = pickle.load(
            open(
                "../data/exact_distributions_correlation/aggregated_dist_returns"
                + f"_market_data_{dates[0]}_{dates[1]}_step_{time_step}.pickle",
                "rb",
            )
        )

        agg_returns_data = agg_returns_data.rename("Agg. returns")

        x_val: np.ndarray = np.arange(-10, 10, 0.1)

        # Log plot
        plot_log = agg_returns_data.plot(
            kind="density", style="-", logy=True, figsize=(16, 9), legend=True, lw=3
        )

        if dates[0] == "1972-01":
            N = 5.5
            N_aa = 6
            K = 23
            L = 55
            l = 55

            gg_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_gaussian_gaussian(x_val, N, 1)
            )
            plt.semilogy(x_val, gg_distribution, "o", lw=3, label=f"GG - N = {N}")

            ga_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_gaussian_algebraic(
                    x_val, K, L, N, 1
                )
            )
            plt.semilogy(
                x_val,
                ga_distribution,
                "o",
                lw=3,
                label=f"GA - N = {N} - K = {K} - L = {L}",
            )

            ag_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_algebraic_gaussian(
                    x_val, K, l, N, 1
                )
            )
            plt.semilogy(
                x_val,
                ag_distribution,
                "o",
                lw=3,
                label=f"AG - N = {N} - K = {K} - l = {l}",
            )

            aa_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_algebraic_algebraic(
                    x_val, K, L, l, N_aa, 1
                )
            )
            plt.semilogy(
                x_val,
                aa_distribution,
                "o",
                lw=3,
                label=f"AA - N = {N_aa} - K = {K} - L = {L}" + f" - l = {l}",
            )

        elif dates[0] == "1992-01":
            N = 6
            N_gg = 5
            N_aa = 9
            K = 277
            L = 150
            l = 150

            gg_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_gaussian_gaussian(
                    x_val, N_gg, 1
                )
            )
            plt.semilogy(x_val, gg_distribution, "o", lw=3, label=f"GG - N = {N_gg}")

            ga_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_gaussian_algebraic(
                    x_val, K, L, N, 1
                )
            )
            plt.semilogy(
                x_val,
                ga_distribution,
                "o",
                lw=3,
                label=f"GA - N = {N} - K = {K} - L = {L}",
            )

            ag_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_algebraic_gaussian(
                    x_val, K, l, N, 1
                )
            )
            plt.semilogy(
                x_val,
                ag_distribution,
                "o",
                lw=3,
                label=f"AG - N = {N} - K = {K} - l = {l}",
            )

            aa_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_algebraic_algebraic(
                    x_val, K, L, l, N_aa, 1
                )
            )
            plt.semilogy(
                x_val,
                aa_distribution,
                "o",
                lw=3,
                label=f"AA - N = {N_aa} - K = {K} - L = {L}" + f" - l = {l}",
            )

        else:
            N = 7
            N_aa = 8
            K = 461
            L = 280
            l = 280

            gg_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_gaussian_gaussian(x_val, N, 1)
            )
            plt.semilogy(x_val, gg_distribution, "o", lw=3, label=f"GG - N = {N}")

            ga_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_gaussian_algebraic(
                    x_val, K, L, N, 1
                )
            )
            plt.semilogy(
                x_val,
                ga_distribution,
                "o",
                lw=3,
                label=f"GA - N = {N} - K = {K} - L = {L}",
            )

            ag_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_algebraic_gaussian(
                    x_val, K, l, N, 1
                )
            )
            plt.semilogy(
                x_val,
                ag_distribution,
                "o",
                lw=3,
                label=f"AG - N = {N} - K = {K} - l = {l}",
            )

            aa_distribution: np.ndarray = (
                exact_distributions_correlation_tools.pdf_algebraic_algebraic(
                    x_val, K, L, l, N_aa, 1
                )
            )
            plt.semilogy(
                x_val,
                aa_distribution,
                "o",
                lw=3,
                label=f"AA - N = {N_aa} - K = {K} - L = {L}" + f" - l = {l}",
            )

        plt.legend(fontsize=20)
        plt.title(
            f"Aggregated distribution returns from {dates[0]} to"
            + f" {dates[1]} - {time_step}",
            fontsize=30,
        )
        plt.xlabel("Aggregated returns", fontsize=25)
        plt.ylabel("PDF", fontsize=25)
        plt.xticks(fontsize=15)
        plt.yticks(fontsize=15)
        plt.xlim(-10, 10)
        plt.ylim(10 ** -9, 1)
        plt.grid(True)
        plt.tight_layout()
        figure_log: plt.Figure = plot_log.get_figure()

        # Plotting
        exact_distributions_correlation_tools.save_plot(
            figure_log, function_name, dates, time_step
        )

        plt.close()
        del agg_returns_data
        del figure_log
        del plot_log
        gc.collect()

    except FileNotFoundError as error:
        print("No data")
        print(error)
        print()