Пример #1
0
def plot_outcomes(datafiles):
    """Plot the outcome of each scenario against the average assurance monitor value"""

    # Compile AM values and outcomes of all data files with NO FAULT condition
    no_fault_dfs = []
    for df in datafiles:
        # if len(df.fault_set) == 0 or fault_modes.SingularFaultModes.NO_FAULT in df.fault_set:
        #     no_fault_dfs.append(df)
        no_fault_dfs.append(df)
    top_occurred = np.zeros(len(no_fault_dfs))
    collision_occurred = np.zeros(len(no_fault_dfs))
    average_am = np.zeros(len(no_fault_dfs))
    for i, df in enumerate(no_fault_dfs):
        top_occurred[i] = df.top_occurred
        collision_occurred[i] = df.collision_occurred
        average_am[i] = df.am_avg_before_top
    avg_prob = ((np.sum(top_occurred) + 1) / float(len(top_occurred) + 2))
    print("Average TOP Probability: %f" % avg_prob)

    # Fit a conditional binomial distribution to the data with max-likelihood
    fit_results, _ = function_fitting.max_likelihood_conditional_fit(average_am, top_occurred)
    x_range = np.linspace(np.min(average_am), np.max(average_am), 100)
    y_sig_max_likelihood = function_fitting.bounded_sigmoid(x_range, *fit_results.x)
    print("Sigmoid Fit: ", fit_results)

    # Plot collisions and top events on same figure
    # Plot conditional probability P(B | AM) and adjusted probability P(B | AM) / P(B)
    fig = plt.figure(dpi=300)
    ax1 = fig.add_subplot(1, 1, 1)
    # ax1.scatter(average_am, top_occurred, label="Top Events")
    # ax1.scatter(average_am, collision_occurred, label="Collision Events")
    ax1.plot(x_range, y_sig_max_likelihood, label="MLE fit")
    # ax1.plot(x_range, y_sig_max_likelihood/avg_prob, label="Relative to baseline")
    ax1.axhline(avg_prob, linestyle="-.", label="baseline", color="green")
    # ax1.axhline(1.0, linestyle="--", color="purple")
    # ax1.set_xlabel("Average AM Log Martingale")
    # ax1.set_ylabel("Outcome (True/False)")
    ax1.set_xlabel("Assurance Monitor output")
    ax1.set_ylabel("Barrier Failure Probability")
    ax1.set_title("P(B1) vs. Assurance Monitor output")
    ax1.legend(loc="best")
    ax1.set_ylim([0.0, np.max(y_sig_max_likelihood/avg_prob) + 0.1])
    plt.show()
Пример #2
0
def plot_outcomes(datafiles):
    """Plot the outcome of each scenario against the average assurance monitor value"""

    # Compile outcomes of each sim run
    top_occurred = np.zeros(len(datafiles))
    collision_occurred = np.zeros(len(datafiles))
    thruster_degradation = np.zeros(len(datafiles))
    closest_approach = np.zeros(len(datafiles))
    for i, df in enumerate(datafiles):
        top_occurred[i] = df.top_occurred.astype(int)
        collision_occurred[i] = df.consequence_occurred.astype(int)
        thruster_degradation[i] = df.thruster_degradation_amount
        closest_approach[i] = df.closest_approach

    # Datasets with no top events or collisions can cause issues with max likelihood estimation. Print warning.
    if np.sum(top_occurred) == 0:
        print("WARNING: No Top events occurred in the provided datasets.")
    if np.sum(collision_occurred) == 0:
        print("WARNING: No Collision events occurred in the provided datasets.")

    # Calculate some simple statistics
    avg_top_prob = ((np.sum(top_occurred) + 1) / float(len(top_occurred) + 2))
    avg_col_prob = ((np.sum(collision_occurred) + 1) / float(len(collision_occurred) + 2))
    avg_col_prob_after_top = ((np.sum(collision_occurred) + 1) / float(np.sum(top_occurred) + 2))
    avg_ca = np.sum(closest_approach) / float(len(closest_approach))
    print("Average TOP Probability: %f" % avg_top_prob)
    print("Average Collision Probability: %f" % avg_col_prob)
    print("Average Collision Probability given TOP occurred: %f" % avg_col_prob_after_top)
    print("Average closest approach (m): %f" % avg_ca)
    print("\n\n")

    # Fit a conditional binomial distribution to the data with max-likelihood for the TOP event
    x_range = np.linspace(np.min(thruster_degradation), np.max(thruster_degradation), 100)
    fit_results = None
    try:
        fit_results, _ = function_fitting.max_likelihood_conditional_fit(thruster_degradation, top_occurred)
    except ValueError as e:
        print("Exception occurred when fitting sigmoid for TOP event:\n%s" % str(e))
    else:
        y_sig_max_likelihood = function_fitting.bounded_sigmoid(x_range, *fit_results.x)
        adjusted_likelihood = y_sig_max_likelihood/avg_top_prob
        print("TOP Sigmoid Fit: ", fit_results)
        print("\n\n")

    # Divide results into bins and average each bin
    bins = {"x": [], "y": {"top": [], "col": []}}
    bins_avg = {"x": [], "y": {"top": [], "col": []}}
    step_size = 0.2
    allowed_skew = 0.01
    for i in range(4):
        bin_x = step_size * i
        indices = np.logical_and(thruster_degradation > bin_x - allowed_skew, thruster_degradation < bin_x + allowed_skew)
        bins["x"].append(thruster_degradation[indices])
        bins["y"]["top"].append(top_occurred[indices])
        bins["y"]["col"].append(collision_occurred[indices])
        avg_deg = np.average(thruster_degradation[indices])
        avg_top_prob = (np.sum(top_occurred[indices]) + 1) / float(len(top_occurred[indices]) + 2)
        avg_col_prob = (np.sum(collision_occurred[indices]) + 1) / float(len(collision_occurred[indices]) + 2)
        bins_avg["x"].append(avg_deg)
        bins_avg["y"]["top"].append(avg_top_prob)
        bins_avg["y"]["col"].append(avg_col_prob)
        print("Bin %.2f contains %d entries.\n\tAverage probability of TOP: %.2f\n\tAverage probability of Collision: %.2f"
              % (avg_deg, len(thruster_degradation[indices]), avg_top_prob, avg_col_prob))
        print("\n\n")

    # Plot conditional probability P(B | D) and adjusted probability P(B | D) / P(B) for TOP barrier
    fig1 = plt.figure(dpi=300)
    ax1 = fig1.add_subplot(1, 1, 1)
    # ax1.scatter(thruster_degradation, top_occurred, label="Top Events")
    # ax1.scatter(thruster_degradation, collision_occurred, label="Collision Events")
    ax1.scatter(bins_avg["x"], bins_avg["y"]["top"], label="Top Event Prob.")
    ax1.scatter(np.average(thruster_degradation), avg_top_prob, label="Average")
    if fit_results is not None:
        ax1.plot(x_range, y_sig_max_likelihood, label="Sigmoid MLE fit")
        ax1.plot(x_range, adjusted_likelihood, label="Adjusted fit")
        ax1.set_ylim([0.0, np.max(adjusted_likelihood)])
    else:
        ax1.set_ylim([0.0, 1.0])
    ax1.set_xlabel("Thruster Degradation Percentage")
    ax1.set_ylabel("TOP Event Probability")
    ax1.legend(loc="best")


    # Fit a conditional binomial distribution to the data with max-likelihood for the TOP event
    fit_results = None
    try:
        fit_results, _ = function_fitting.max_likelihood_conditional_fit(thruster_degradation, collision_occurred)
    except ValueError as e:
        print("Exception occurred when fitting sigmoid for Collision event:\n%s" % str(e))
    else:
        collision_y_sigmoid = function_fitting.bounded_sigmoid(x_range, *fit_results.x)
        collision_adj_likelihood = collision_y_sigmoid / avg_col_prob
        print("Collision Sigmoid Fit: ", fit_results)
        print("\n\n")

    # Plot conditional probability P(B | D) and adjusted probability P(B | D) / P(B) for Collision barrier
    fig2 = plt.figure(dpi=300)
    ax2 = fig2.add_subplot(1, 1, 1)
    # ax2.scatter(thruster_degradation, top_occurred, label="Top Events")
    # ax2.scatter(thruster_degradation, collision_occurred, label="Collision Events")
    ax2.scatter(bins_avg["x"], bins_avg["y"]["col"], label="Collision Event Prob.")
    ax2.scatter(np.average(thruster_degradation), avg_col_prob, label="Average")
    if fit_results is not None:
        ax2.plot(x_range, collision_y_sigmoid, label="Sigmoid MLE fit")
        ax2.plot(x_range, collision_adj_likelihood, label="Adjusted fit")
        ax2.set_ylim([0.0, np.max(collision_adj_likelihood)])
    else:
        ax2.set_ylim([0.0, 1.0])
    ax2.set_xlabel("Thruster Degradation Percentage")
    ax2.set_ylabel("Collision Probability")
    ax2.legend(loc="best")

    # Show plots
    plt.show()
Пример #3
0
def cont_analysis(cd):

    # Calculate some simple statistics
    avg_top_prob = ((np.sum(cd.top_events) + 1) /
                    float(len(cd.top_events) + 2))
    avg_col_prob = ((np.sum(cd.consequences) + 1) /
                    float(len(cd.consequences) + 2))
    avg_col_prob_after_top = ((np.sum(cd.consequences) + 1) /
                              float(np.sum(cd.top_events) + 2))
    print("Average TOP Probability: %f" % avg_top_prob)
    print("Average Consequence Probability: %f" % avg_col_prob)
    print("Average Consequence Probability given TOP occurred: %f" %
          avg_col_prob_after_top)
    print("\n\n")

    for key, values in cd.list_independent_vars_cont.items():

        values_np = np.array(values)
        top_np = np.array(cd.top_events)
        cons_np = np.array(cd.consequences)

        # # Fit a conditional binomial distribution to the data with max-likelihood for the TOP event
        x_range = np.linspace(np.min(values), np.max(values), 100)
        fit_results = None
        try:
            fit_results, _ = function_fitting.max_likelihood_conditional_fit(
                values_np, top_np)
        except ValueError as e:
            print(
                "Exception occurred when fitting sigmoid for TOP event:\n%s" %
                str(e))
        else:
            y_sig_max_likelihood = function_fitting.bounded_sigmoid(
                x_range, *fit_results.x)
            adjusted_likelihood = y_sig_max_likelihood / avg_top_prob
            print("TOP Sigmoid Fit: ", fit_results)
            print("\n\n")

        #todo add this back for plots

        # Plot conditional probability P(B | D) and adjusted probability P(B | D) / P(B) for TOP barrier
        fig1 = plt.figure(dpi=300)
        ax1 = fig1.add_subplot(1, 1, 1)
        ax1.scatter(values_np, top_np, label="Top Events")
        ax1.scatter(values_np, cons_np, label="Collision Events")
        # ax1.scatter(bins_avg["x"], bins_avg["y"]["top"], label="Top Event Prob.")
        ax1.scatter(values_np, top_np, label="Top Events")
        ax1.scatter(np.average(values_np), avg_top_prob, label="Average")
        if fit_results is not None:
            ax1.plot(x_range, y_sig_max_likelihood, label="Sigmoid MLE fit")
            ax1.plot(x_range, adjusted_likelihood, label="Adjusted fit")
            ax1.set_ylim([0.0, np.max(adjusted_likelihood)])
        else:
            ax1.set_ylim([0.0, 1.0])
        ax1.set_xlabel("Indep. Var Percentage")
        ax1.set_ylabel("TOP Event Probability")
        ax1.legend(loc="best")

        # Fit a conditional binomial distribution to the data with max-likelihood for the TOP event
        fit_results = None
        try:
            fit_results, _ = function_fitting.max_likelihood_conditional_fit(
                values_np, cons_np)
        except ValueError as e:
            print(
                "Exception occurred when fitting sigmoid for Collision event:\n%s"
                % str(e))
        else:
            collision_y_sigmoid = function_fitting.bounded_sigmoid(
                x_range, *fit_results.x)
            collision_adj_likelihood = collision_y_sigmoid / avg_col_prob
            print("Collision Sigmoid Fit: ", fit_results)
            print("\n\n")

        # Plot conditional probability P(B | D) and adjusted probability P(B | D) / P(B) for Collision barrier
        fig2 = plt.figure(dpi=300)
        ax2 = fig2.add_subplot(1, 1, 1)
        # ax2.scatter(thruster_degradation, top_occurred, label="Top Events")
        ax2.scatter(values_np, cons_np, label="Collision Events")
        # ax2.scatter(bins_avg["x"], bins_avg["y"]["col"], label="Collision Event Prob.")
        ax2.scatter(np.average(values_np), avg_col_prob, label="Average")
        if fit_results is not None:
            ax2.plot(x_range, collision_y_sigmoid, label="Sigmoid MLE fit")
            ax2.plot(x_range, collision_adj_likelihood, label="Adjusted fit")
            ax2.set_ylim([0.0, np.max(collision_adj_likelihood)])
        else:
            ax2.set_ylim([0.0, 1.0])
        ax2.set_xlabel("Indep. Var Percentage")
        ax2.set_ylabel("Collision Probability")
        ax2.legend(loc="best")

        # Show plots
        plt.show()