예제 #1
0
def plot_power(effect_sizes, empirical_power, ax=None):
    """Function for plotting empirical power as a function of effect size.

    Input arguments:
    ================
    effect_sizes : ndarray [n_effect_sizes, ]
        The evaluated effect sizes.

    empirical_power : ndarray[n_effect_sizes, ]
        The empirical power at each effect size.

    ax : Axes
        (Optional) Axes to plot to. If not specified, a new figure with new
        axes will be created.
    """
    """Fit a logistic function to the data."""
    logistic_k, logistic_x0 = curve_fit(logistic_function, effect_sizes,
                                        empirical_power)[0]
    logistic_x = np.linspace(effect_sizes[0], effect_sizes[-1], 100)
    logistic_y = logistic_function(logistic_x, logistic_k, logistic_x0)
    """Plot the data and fitted line."""
    if (ax is None):
        fig = plt.figure(figsize=(8, 5))
        ax = fig.add_subplot(121)
    ax.plot(effect_sizes, empirical_power, '.', markersize=9)
    ax.plot(logistic_x, logistic_y, '-', linewidth=1.5)
    """Label the axes etc."""
    ax.set_xlim([effect_sizes[0] - 0.05, effect_sizes[-1] + 0.05])
    ax.set_ylim([-0.05, 1.05])
    ax.set_xlabel('Effect size $\Delta$', fontsize=14)
    ax.set_ylabel('Empirical power', fontsize=14)
예제 #2
0
def plot_two_group_reproducibility(effect_sizes, emphasis_primary,
                                   reproducibility):
    """Function for visualizing reproducibility in the two-group model.

    Input arguments:
    ================
    effect_sizes : ndarray [n_effect_sizes, ]
        The tested effect sizes.

    emphasis_primary : ndarray [n_emphasis_values, ]
        The tested primary study emphases.

    reproducibility ndarray [n_effect_sizes, n_emphasis_values]
        The observed reproducibility at each combination of effect size and
        emphasis of primary study.

    Output arguments:
    =================
    fig : Figure
        Instance of matplotlib Figure class.
    """
    n_emphs = len(emphasis_primary)

    """Fit logistic functions to the data."""
    logistic_k, logistic_x0 = np.zeros(n_emphs), np.zeros(n_emphs)
    for i in np.arange(0, n_emphs):
        params = curve_fit(logistic_function, effect_sizes,
                           reproducibility[:, i])[0]
        logistic_k[i], logistic_x0[i] = params

    """Visualize the results."""
    sns.set_style('darkgrid')
    fig = plt.figure(figsize=(8, 5))

    ax = fig.add_subplot(111)
    ax.plot(effect_sizes, reproducibility, '.')

    for i in np.arange(0, n_emphs):
        logistic_x = np.linspace(effect_sizes[0], effect_sizes[-1], 100)
        logistic_y = logistic_function(logistic_x, logistic_k[i],
                                       logistic_x0[i])
        plt.plot(logistic_x, logistic_y, '-')

    ax.set_xlim([effect_sizes[0]-0.05, effect_sizes[-1]+0.05])
    ax.set_ylim([0.0, 1.0])
    ax.set_xlabel('Effect size')
    ax.set_ylabel('Reproducibility rate')
    ax.set_title('Two-stage FDR')
    ax.legend(emphasis_primary, loc='lower right')

    fig.tight_layout()
    return fig
예제 #3
0
def plot_logistic(x,
                  y,
                  ax,
                  legend=None,
                  xlabel=None,
                  ylabel=None,
                  xlim=None,
                  ylim=None,
                  **kwargs):
    """Function for drawing a scatter plot of the (x, y) tuples and
    fitting a logistic function.

    Input arguments:
    ================
    x, y : ndarray
        The visualize data points.
    ax : Matplotlib Axes
        The axes to draw to.
    legend : ndarray
        Legend added to the axes, e.g. different parameter selection when
        drawing several overlaid datasets at once.
    xlabel, ylabel : str
        Labels for the x- and y-axes.
    xlim, ylim : ndarray
        Limits for the axes.
    **kwargs : keyword arguments
        Possible additional keyword arguments for plotting (ax.plot).
    """
    """Find number of samples and variables."""
    if (len(np.shape(x)) == 1):
        x = x[:, None]
    if (len(np.shape(y)) == 1):
        y = y[:, None]
    print np.shape(x), np.shape(y)
    n_samples, n_vars = np.shape(y)
    """Fit a logistic function to the data."""
    for i in np.arange(0, n_vars):
        xdata, ydata = np.squeeze(x), np.squeeze(y[:, i])
        logistic_k, logistic_x0 = curve_fit(logistic_function, xdata, ydata)[0]
        logistic_x = np.linspace(x[0], x[-1], 100)
        logistic_y = logistic_function(logistic_x, logistic_k, logistic_x0)
        """Plot the data and the fitted line."""
        ax.plot(x, y[:, i], '.', markersize=9, **kwargs)
        ax.plot(logistic_x, logistic_y, '-', linewidth=1.5, **kwargs)
    """Label the axes etc."""
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    ax.set_xlabel(xlabel, fontsize=14)
    ax.set_ylabel(ylabel, fontsize=14)
    return ax
예제 #4
0
##########################################################
########## Predict population data
#######################
prev_pop = pop_data[2014]
mort_fixed = mort_data[2014]
imm_fixed = imm_rate[2014]
NUM_COLORS = 2500 + 1 - 2015
cm = plt.get_cmap('Blues')
cNorm  = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_prop_cycle(color=[scalarMap.to_rgba(i) for i in range(NUM_COLORS)])

for year in range(2015, 2500):
    beta = util.logistic_function(year - 1, L_MLE_beta, k_MLE_beta, x_MLE_beta) + min(betas)
    m = util.logistic_function(year - 1, L_MLE_m, k_MLE_m, x_MLE_m) + min(ms)
    year_adj_alpha = - (year - 1 - x_MLE_alpha) + x_MLE_alpha
    alpha = util.logistic_function(year_adj_alpha, L_MLE_alpha, k_MLE_alpha, x_MLE_alpha) + min(alphas)
    year_adj_scale = - (year - 1 - x_MLE_scale) + x_MLE_scale
    scale = util.logistic_function(year_adj_scale, L_MLE_scale, k_MLE_scale, x_MLE_scale) + min(scales)
    ages = np.linspace(14, 50, 37)
    fert = util.gen_gamma_fun_pdf(ages, alpha, beta, m)

    fert = fert_data[2014]

    births = fert * prev_pop[14:51]
    births = np.sum(births)

    deaths = mort_fixed * prev_pop
    deaths = np.roll(deaths, 1)