Example #1
0
def test_plot_fert_rates_save_fig(tmpdir):
    totpers = base_params.S
    min_yr = 20
    max_yr = 100
    fert_data = (np.array([0.0, 0.0, 0.3, 12.3, 47.1, 80.7, 105.5, 98.0,
                           49.3, 10.4, 0.8, 0.0, 0.0]) / 2000)
    age_midp = np.array([9, 10, 12, 16, 18.5, 22, 27, 32, 37, 42, 47,
                         55, 56])
    fert_func = si.interp1d(age_midp, fert_data, kind='cubic')
    fert_rates = np.random.uniform(size=totpers)
    parameter_plots.plot_fert_rates(
        fert_func, age_midp, totpers, min_yr, max_yr, fert_data,
        fert_rates, output_dir=tmpdir)
    img = mpimg.imread(os.path.join(tmpdir, 'fert_rates.png'))

    assert isinstance(img, np.ndarray)
Example #2
0
def test_plot_fert_rates():
    totpers = base_params.S
    min_yr = 20
    max_yr = 100
    fert_data = (np.array([0.0, 0.0, 0.3, 12.3, 47.1, 80.7, 105.5, 98.0,
                           49.3, 10.4, 0.8, 0.0, 0.0]) / 2000)
    age_midp = np.array([9, 10, 12, 16, 18.5, 22, 27, 32, 37, 42, 47,
                         55, 56])
    fert_func = si.interp1d(age_midp, fert_data, kind='cubic')
    fert_rates = np.random.uniform(size=totpers)
    fig = parameter_plots.plot_fert_rates(
        fert_func, age_midp, totpers, min_yr, max_yr, fert_data,
        fert_rates)
    assert fig
def get_fert(totpers, min_yr, max_yr, graph=False):
    '''
    This function generates a vector of fertility rates by model period
    age that corresponds to the fertility rate data by age in years
    (Source: National Vital Statistics Reports, Volume 64, Number 1,
    January 15, 2015, Table 3, final 2013 data
    http://www.cdc.gov/nchs/data/nvsr/nvsr64/nvsr64_01.pdf)

    Args:
        totpers (int): total number of agent life periods (E+S), >= 3
        min_yr (int): age in years at which agents are born, >= 0
        max_yr (int): age in years at which agents die with certainty,
            >= 4
        graph (bool): =True if want graphical output

    Returns:
        fert_rates (Numpy array): fertility rates for each model period
            of life

    '''
    # Get current population data (2013) for weighting
    pop_file = os.path.join(
        CUR_PATH, 'data', 'demographic', 'pop_data.csv')
    pop_data = pd.read_csv(pop_file, thousands=',')
    pop_data_samp = pop_data[(pop_data['Age'] >= min_yr - 1) &
                             (pop_data['Age'] <= max_yr - 1)]
    curr_pop = np.array(pop_data_samp['2013'], dtype='f')
    curr_pop_pct = curr_pop / curr_pop.sum()
    # Get fertility rate by age-bin data
    fert_data = (np.array([0.0, 0.0, 0.3, 12.3, 47.1, 80.7, 105.5, 98.0,
                           49.3, 10.4, 0.8, 0.0, 0.0]) / 2000)
    # Mid points of age bins
    age_midp = np.array([9, 10, 12, 16, 18.5, 22, 27, 32, 37, 42, 47,
                         55, 56])
    # Generate interpolation functions for fertility rates
    fert_func = si.interp1d(age_midp, fert_data, kind='cubic')
    # Calculate average fertility rate in each age bin using trapezoid
    # method with a large number of points in each bin.
    binsize = (max_yr - min_yr + 1) / totpers
    num_sub_bins = float(10000)
    len_subbins = (np.float64(100 * num_sub_bins)) / totpers
    age_sub = (np.linspace(np.float64(binsize) / num_sub_bins,
                           np.float64(max_yr),
                           int(num_sub_bins*max_yr)) - 0.5 *
               np.float64(binsize) / num_sub_bins)
    curr_pop_sub = np.repeat(np.float64(curr_pop_pct) / num_sub_bins,
                             num_sub_bins)
    fert_rates_sub = np.zeros(curr_pop_sub.shape)
    pred_ind = (age_sub > age_midp[0]) * (age_sub < age_midp[-1])
    age_pred = age_sub[pred_ind]
    fert_rates_sub[pred_ind] = np.float64(fert_func(age_pred))
    fert_rates = np.zeros(totpers)
    end_sub_bin = 0
    for i in range(totpers):
        beg_sub_bin = int(end_sub_bin)
        end_sub_bin = int(np.rint((i + 1) * len_subbins))
        fert_rates[i] = ((
            curr_pop_sub[beg_sub_bin:end_sub_bin] *
            fert_rates_sub[beg_sub_bin:end_sub_bin]).sum() /
            curr_pop_sub[beg_sub_bin:end_sub_bin].sum())

    if graph:
        pp.plot_fert_rates(fert_func, age_midp, totpers, min_yr, max_yr,
                           fert_data, fert_rates, output_dir=OUTPUT_DIR)

    return fert_rates