def distributions_plot(N: float, K: float, L: float, l: float) -> None:
    """Plots the distributions in linear and logarithmic scale.

    :return: None -- The function saves the plot in a file and does not return
     a value.
    """

    x_vals_lin: np.ndarray = np.arange(-10, 10, 0.2)
    x_vals_log: np.ndarray = np.arange(-10, 10, 0.5)

    gg_distribution_lin: np.ndarray = exact_distributions_tools.pdf_gaussian_gaussian(
        x_vals_lin, N, 1
    )
    ga_distribution_lin: np.ndarray = exact_distributions_tools.pdf_gaussian_algebraic(
        x_vals_lin, K, L, N, 1
    )
    ag_distribution_lin: np.ndarray = exact_distributions_tools.pdf_algebraic_gaussian(
        x_vals_lin, K, l, N, 1
    )
    aa_distribution_lin: np.ndarray = exact_distributions_tools.pdf_algebraic_algebraic(
        x_vals_lin, K, L, l, N, 1
    )

    gg_distribution_log: np.ndarray = exact_distributions_tools.pdf_gaussian_gaussian(
        x_vals_log, N, 1
    )
    ga_distribution_log: np.ndarray = exact_distributions_tools.pdf_gaussian_algebraic(
        x_vals_log, K, L, N, 1
    )
    ag_distribution_log: np.ndarray = exact_distributions_tools.pdf_algebraic_gaussian(
        x_vals_log, K, l, N, 1
    )
    aa_distribution_log: np.ndarray = exact_distributions_tools.pdf_algebraic_algebraic(
        x_vals_log, K, L, l, N, 1
    )

    markers: List[str] = ["-o", "-^", "-s", "-P"]

    figure, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 9))

    # Linear plot
    ax1.plot(x_vals_lin, gg_distribution_lin, markers[0], ms=10, label=f"GG")
    ax1.plot(x_vals_lin, ga_distribution_lin, markers[1], ms=10, label=f"GA")
    ax1.plot(x_vals_lin, ag_distribution_lin, markers[2], ms=10, label=f"AG")
    ax1.plot(x_vals_lin, aa_distribution_lin, markers[3], ms=10, label=f"AA")

    ax1.set_xlabel(r"$\tilde{r}$", fontsize=20)
    ax1.set_ylabel(r"PDF", fontsize=20)
    ax1.tick_params(axis="x", labelsize=15)
    ax1.tick_params(axis="y", labelsize=15)
    ax1.set_xlim(-2, 2)
    ax1.set_ylim(0, 0.65)
    ax1.grid(True)

    # Logarithmic plot
    ax2.semilogy(x_vals_log, gg_distribution_log, markers[0], ms=10, label=f"GG")
    ax2.semilogy(x_vals_log, ga_distribution_log, markers[1], ms=10, label=f"GA")
    ax2.semilogy(x_vals_log, ag_distribution_log, markers[2], ms=10, label=f"AG")
    ax2.semilogy(x_vals_log, aa_distribution_log, markers[3], ms=10, label=f"AA")

    ax2.legend(loc="upper center", bbox_to_anchor=(1.13, 0.6), ncol=1, fontsize=20)
    ax2.set_xlabel(r"$\tilde{r}$", fontsize=20)
    ax2.set_ylabel(r"PDF", fontsize=20)
    ax2.tick_params(axis="both", which="both", labelsize=15)
    ax2.set_xlim(-6, 6)
    ax2.set_ylim(10 ** -4, 1)
    ax2.grid(True)

    plt.tight_layout()

    # Save Plot
    figure.savefig(f"../plot/07_distributions_comparison.png")
def pdf_aa_distributions_plot(
    dates: List[str],
    time_step: str,
    N_values: List[int],
    K_value: int,
    L_values: List[int],
    l_values: List[int],
) -> 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', ...).
    :param N_values: fit parameter (i.e. [3, 4, 5, 6]).
    :param K_value: number of companies.
    :param L_values: shape parameter (i.e. [3, 4, 5, 6]).
    :param l_values: shape parameter (i.e. [3, 4, 5, 6]).
    :return: None -- The function saves the plot in a file and does not return
     a value.
    """

    figure, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 9))

    markers: List[str] = ["o", "^", "s", "P"]

    try:

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

        agg_returns_data = agg_returns_data.rename(r"$\tilde{r}$")

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

    # Log plot
    plot_1 = agg_returns_data.plot(
        kind="density", style=markers[0], logy=True, ax=ax1, legend=False, ms=7
    )
    plot_2 = agg_returns_data.plot(
        kind="density", style=markers[0], loglog=True, ax=ax2, legend=False, ms=7
    )

    x_val_log: np.ndarray = np.arange(-10, 10, 0.05)

    for N_value in N_values:
        for L_value in L_values:
            for l_value in l_values:
                aa_distribution_log: np.ndarray = (
                    exact_distributions_tools.pdf_algebraic_algebraic(
                        x_val_log, K_value, L_value, l_value, N_value, 1
                    )
                )
                ax1.semilogy(
                    x_val_log,
                    aa_distribution_log,
                    "-",
                    lw=5,
                    label=f"N = {N_value} - L = {L_value}" + f" - l = {l_value}",
                )
                ax2.loglog(
                    x_val_log,
                    aa_distribution_log,
                    "-",
                    lw=5,
                    label=f"N = {N_value} - L = {L_value}" + f" - l = {l_value}",
                )

    ax1.set_xlabel(r"$\tilde{r}$", fontsize=25)
    ax1.set_ylabel("PDF", fontsize=25)
    ax1.tick_params(axis="both", which="both", labelsize=15)
    ax1.set_xlim(-6, 6)
    ax1.set_ylim(10 ** -4, 1)
    ax1.grid(True)

    ax2.legend(loc="upper right", fontsize=18)
    ax2.set_xlabel(r"$\tilde{r}$", fontsize=20)
    ax2.set_ylabel("PDF", fontsize=20)
    ax2.tick_params(axis="both", which="both", labelsize=15)
    ax2.set_xlim(3, 5)
    ax2.set_ylim(0.5 * 10 ** -3, 10 ** -2)
    ax2.grid(True)

    plt.tight_layout()

    # Save plot
    figure.savefig(f"../plot/08_aa.png")

    plt.close()
    del agg_returns_data
    del figure
    del plot_1
    del plot_2
コード例 #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_covariance_tools.function_header_print_plot(
        function_name, dates, time_step)

    try:

        # Load data
        agg_returns_data: pd.Series = pickle.load(
            open(
                "../data/exact_distributions_covariance/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_covariance_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_covariance_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_covariance_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_covariance_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 = 5
            N_gg = 4
            N_aa = 6
            K = 277
            L = 150
            l = 150

            gg_distribution: np.ndarray = (
                exact_distributions_covariance_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_covariance_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_covariance_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_covariance_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_gg = 6
            K = 461
            L = 280
            l = 280

            gg_distribution: np.ndarray = (
                exact_distributions_covariance_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_covariance_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_covariance_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_covariance_tools.pdf_algebraic_algebraic(
                    x_val, K, L, l, N, 1))
            plt.semilogy(
                x_val,
                aa_distribution,
                "o",
                lw=3,
                label=f"AA - N = {N} - K = {K} - L = {L} - 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_covariance_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()