def plot_lb_w_time(num_sims: int = 20) -> None:
    """Plot lower bounds with sampling times.

    Args:
        num_sims: Number of simulations.

    Returns:
        Save plot.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = 0
    num_genes = 20
    prob_conn = 0.5
    spec_rad = 0.8
    time_list = [2, 4, 5, 8, 10, 20, 25, 40]
    lower_bounds = []
    for samp_time in time_list:
        num_cond = 200 / samp_time
        sim_net.samp_times = samp_time
        lower_bounds.append(
            sim_net.sim_er_genie_bhatta_lb(num_genes, prob_conn, spec_rad,
                                           num_sims, num_cond)[1])
    lower_bounds = np.asarray(lower_bounds)
    plt.figure()
    plt.errorbar(time_list, lower_bounds[:, 0], yerr=lower_bounds[:, 1])
    plt.xlabel("number of sampling times")
    plt.ylabel("lower bound on average error probability")
    plt.savefig(
        "/Users/veggente/Documents/workspace/python/sampcomp/pe_v_time_n{}.eps"
        .format(num_sims))
def plot_lb_w_network_size(num_sims: int = 20, sigma_te_sq: float = 0) -> None:
    """Plot lower bounds with network size.

    Args:
        num_sims: Number of simulations.
        sigma_te_sq: Technical variation.

    Returns:
        Save plot.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = sigma_te_sq
    num_genes_list = [20, 40, 60, 80, 100, 120, 140, 160, 180, 200]
    prob_conn = [10 / num_genes for num_genes in num_genes_list]
    spec_rad = 0.8
    samp_time = 10
    num_cond = 20
    lower_bounds = []
    for counter, num_genes in enumerate(num_genes_list):
        sim_net.samp_times = samp_time
        lower_bounds.append(
            sim_net.sim_er_genie_bhatta_lb(num_genes, prob_conn[counter],
                                           spec_rad, num_sims, num_cond)[1])
    lower_bounds = np.asarray(lower_bounds)
    plt.figure()
    plt.errorbar(num_genes_list, lower_bounds[:, 0], yerr=lower_bounds[:, 1])
    plt.xlabel("network size")
    plt.ylabel("lower bound on average error probability")
    plt.savefig(
        "/Users/veggente/Documents/workspace/python/sampcomp/pe_v_network_size{}_n{}_t{}.eps"
        .format(  # pylint: disable=line-too-long
            num_genes_list[-1], num_sims, sim_net.sigma_te_sq))
def plot_bhatta_w_samp_rate(num_sims: int = 20) -> None:
    """Plot Bhattacharyya coefficient with sampling rate.

    Args:
        num_sims: Number of simulations.

    Returns:
        Save plot.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = 0
    sim_net.one_shot = False
    num_genes = 200
    prob_conn = 0.05
    spec_rad = 0.8
    rate_list = [1, 2, 3, 4, 6]
    bhatta_list = []
    for rate in rate_list:
        sim_net.samp_times = int(12 / rate)
        bhatta_list.append(
            sim_net.sim_er_bhatta(num_genes,
                                  prob_conn,
                                  spec_rad,
                                  num_sims,
                                  stationary=True,
                                  skip=rate - 1)[0])
    plt.figure()
    plt.plot(rate_list, bhatta_list)
    plt.xlabel("sampling rate")
    plt.ylabel("Bhattacharyya coefficient")
    plt.savefig(
        "/Users/veggente/Documents/workspace/python/sampcomp/bhatta_v_rate_n{}.eps"
        .format(num_sims))
def plot_bhatta_w_time(num_sims: int = 20) -> None:
    """Plot Bhattacharyya coefficient with sampling times.

    Args:
        num_sims: Number of simulations.

    Returns:
        Save plot.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = 0
    sim_net.one_shot = False
    num_genes = 200
    prob_conn = 0.05
    spec_rad = 0.8
    time_list = [2, 4, 6, 8, 10]
    bhatta_list = []
    for samp_time in time_list:
        sim_net.samp_times = samp_time
        bhatta_list.append(
            sim_net.sim_er_bhatta(num_genes,
                                  prob_conn,
                                  spec_rad,
                                  num_sims,
                                  stationary=True)[0])
    plt.figure()
    plt.semilogy(time_list, bhatta_list)
    plt.xlabel("number of sampling times")
    plt.ylabel("Bhattacharyya coefficient")
    plt.savefig(
        "/Users/veggente/Documents/workspace/python/sampcomp/bhatta_v_time_n{}.eps"
        .format(num_sims))
def plot_lb_w_spec_rad(num_sims: int = 20, sigma_te_sq: float = 0) -> None:
    """Plot lower bounds with spectral radius.

    Args:
        num_sims: Number of simulations.
        sigma_te_sq: Technical variation.

    Returns:
        Save plot.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = sigma_te_sq
    num_genes = 200
    prob_conn = 10 / num_genes
    spec_rad_list = [0.1, 0.2, 0.4, 0.8]
    samp_time = 10
    num_cond = 200
    lower_bounds = []
    sim_net.samp_times = samp_time
    for spec_rad in spec_rad_list:
        lower_bounds.append(
            sim_net.sim_er_genie_bhatta_lb(num_genes, prob_conn, spec_rad,
                                           num_sims, num_cond)[1])
    lower_bounds = np.asarray(lower_bounds)
    plt.figure()
    plt.errorbar(spec_rad_list, lower_bounds[:, 0], yerr=lower_bounds[:, 1])
    plt.xlabel("spectral radius")
    plt.ylabel("lower bound on average error probability")
    plt.savefig(
        "/Users/veggente/Documents/workspace/python/sampcomp/pe_v_spec_rad_n{}_c{}_t{}.eps"
        .format(num_sims, num_cond, sim_net.sigma_te_sq))
def bhatta_vs_skipped_step_size(sims: int, samp_times: int, total_time: float,
                                base_eta: float):
    """Computes Bhattacharyya coefficient with varying skipped step sizes.

    Follows the setting in [Bento, Ibrahimi, Montanari 2010].

    Args:
        sims: Number of simulations.
        samp_times: Number of actual samples for each skip value.
        total_time: Total time interval.
        base_eta: Base step size.

    Returns:
        Prints Bhattacharyya coefficients.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = 0
    sim_net.one_shot = False
    sim_net.samp_times = samp_times
    skips = list(range(10))
    bhatta_fixed_duration = []
    for this_skip in skips:
        bhatta = sim_net.sim_er_bhatta(
            16,
            1 / 4,
            None,
            sims,
            stationary=True,
            step_size=base_eta,
            memory=True,
            skip=this_skip,
        )[0]
        bhatta_fixed_duration.append(bhatta**(total_time / base_eta /
                                              samp_times / (this_skip + 1)))
    output_prefix = "/Users/veggente/Data/research/flowering/soybean-rna-seq-data/sampcomp/bhatta_v_skipped_step_unscaled_t{}_n{}_s{}_e{}".format(  # pylint: disable=line-too-long
        total_time,
        sims,
        samp_times,
        base_eta,
    )
    with open(output_prefix + ".data", "w") as f:
        for bhatta in bhatta_fixed_duration:
            f.write(str(bhatta) + "\n")
    plt.figure()
    plt.plot(skips, bhatta_fixed_duration, "-o")
    plt.xlabel("skips")
    plt.ylabel("Bhattacharyya coefficient")
    plt.savefig(output_prefix + ".eps")
def stb_lb(spec_rad: float,
           samp_times: int,
           sigma_te_sq: float,
           and_upper: bool = False,
           **kwargs) -> Union[float, Tuple[float, float]]:
    """Calculates lower bound on average error rate.

    Average is on the cross edges.

    Args:
        spec_rad: Spectral radius.
        samp_times: Number of sampling times.
        sigma_te_sq: Technical variance.
        and_upper: Also compute upper bounds.
        **stationary: bool
            Start from stationary distribution.

    Returns:
        Bhattacharyya lower bound (or lower and upper bounds) on average error rate.
    """
    num_genes = 200
    sigma_en_sq = 1
    sigma_in_sq = 0
    num_rep = 1
    prob_conn = 0.05
    num_sims = 10
    num_cond = 2000 / samp_times
    network_ht = sampcomp.NetworkHypothesisTesting()
    network_ht.sigma_en_sq = sigma_en_sq
    network_ht.sigma_in_sq = sigma_in_sq
    network_ht.sigma_te_sq = sigma_te_sq
    network_ht.samp_times = samp_times
    network_ht.num_rep = num_rep
    network_ht.one_shot = False
    sim_lower_bound = network_ht.sim_er_genie_bhatta_lb(
        num_genes,
        prob_conn,
        spec_rad,
        num_sims,
        num_cond,
        bayes=False,
        and_upper=and_upper,
        **filter_kwargs(kwargs, network_ht.sim_er_genie_bhatta_lb))
    if and_upper:
        return sim_lower_bound[1][0], sim_lower_bound[3][0]
    return sim_lower_bound[1][0]
def bhatta_vs_step_size(sims: int):
    """Computes Bhattacharyya coefficient with varying step sizes.

    Roughly follows the setting in [Bento, Ibrahimi, Montanari 2010].

    Args:
        sims: Number of simulations.

    Returns:
        Prints Bhattacharyya coefficients.
    """
    sim_net = sampcomp.NetworkHypothesisTesting()
    sim_net.sigma_in_sq = 0
    sim_net.sigma_te_sq = 0
    sim_net.one_shot = False
    sim_net.samp_times = 10
    eta_list = np.linspace(0.02, 0.2, 10)
    bhatta_list = []
    for eta in eta_list:
        bhatta_list.append(
            sim_net.sim_er_bhatta(16,
                                  1 / 4,
                                  None,
                                  sims,
                                  stationary=True,
                                  step_size=eta,
                                  memory=True))
    total_time = 1
    bhatta_fixed_duration = [
        bhatta**(total_time / eta_list[0] / 10) for bhatta, _ in bhatta_list
    ]
    with open(
            "/Users/veggente/Data/workspace/python/sampcomp/bhatta_v_step_unscaled.data",
            "w",
    ) as f:
        for bhatta in bhatta_fixed_duration:
            f.write(str(bhatta))
    plt.figure()
    plt.plot(eta_list, bhatta_fixed_duration, "-o")
    plt.xlabel(r"$\eta$")
    plt.ylabel("Bhattacharyya coefficient")
    plt.savefig(
        "/Users/veggente/Data/workspace/python/sampcomp/bhatta_v_step_unscaled.eps"
    )
def plot_sub_samp(seed: int = 0):
    """Plot subsampling results."""
    np.random.seed(seed)
    er_graph, weight = sampcomp.erdos_renyi(10, 0.2, 0.8)
    network_ht = sampcomp.NetworkHypothesisTesting()
    adj_mat_pair = list(
        network_ht.genie_hypotheses(er_graph, (0, 1), weight, 0.8))
    bhatta_list = []
    for skip in range(30):
        cov_mat = [
            sampcomp.gen_cov_mat(adj_mat, 0, 1, 10, 1, False, 1, skip)
            for adj_mat in adj_mat_pair
        ]
        bhatta_list.append(sampcomp.bhatta_coeff(*cov_mat))
    print(adj_mat_pair)
    plt.figure()
    plt.plot(np.arange(1, 31), bhatta_list)
    plt.xlabel("sampling interval T")
    plt.ylabel("Bhattacharyya coefficient")
    plt.savefig("subsampling_s{}.eps".format(seed))