def plot(covariates,
         cases,
         fit_params,
         filename,
         definitions,
         discharges=None):
    '''Plot the given cases and the '''
    dist_params = {
        'self_excitation_shape': 2.6,
        'self_excitation_scale': 2.5,
        'discharge_excitation_shape': 2.6,
        'discharge_excitation_scale': 2.5
    }

    intensity = likelihood.carehome_intensity(cases=cases,
                                              fit_params=fit_params,
                                              covariates=covariates,
                                              dist_params=dist_params,
                                              discharges=discharges)

    fig, ax = plt.subplots()
    ax.set_xlabel('Days')
    ax.set_ylabel('Intensity')
    ax_right = ax.twinx()
    ax_right.set_ylabel('Number of events')

    markers = 'ox+*^'
    linestyles = ['-', '--', ':', '-.', (0, (4, 1, 1, 1, 1, 1))]

    for column, (marker, linestyle, (label, original_cases)) in enumerate(
            zip(markers, linestyles, definitions.items())):
        colour = f'C{column}'
        ax.plot(intensity[:, column], color=colour, ls=linestyle)
        if original_cases:
            ax_right.scatter(*zip(*original_cases),
                             color=colour,
                             marker=marker,
                             ls='None')
        else:
            marker = 'None'
        ax.plot(nan, color=colour, marker=marker, label=label, ls=linestyle)

    ax.set_ylim((0, None))
    ax_right.set_ylim((0, None))
    max_num_events = int(ax_right.get_ylim()[1])
    num_num_event_ticks = 1 + min(5, max_num_events)
    num_event_tick_spacing = max_num_events // (num_num_event_ticks - 1)
    num_event_ticks = arange(0, max_num_events + 1, num_event_tick_spacing)
    ax_right.set_yticks(num_event_ticks)

    if len(definitions) > 4:
        legend_columns = 2
    else:
        legend_columns = 1

    ax.legend(loc='best', frameon=False, ncol=legend_columns)
    fig.tight_layout()
    fig.savefig(filename)
    plt.close(fig)
示例#2
0
def test_likelihood_performance(large_test_data, benchmark):
    '''
    Test the performance of the calculation of likelihood from the intensity
    and case distribution.'''

    _, cases, covariates, discharges = large_test_data
    intensity = likelihood.carehome_intensity(fit_params=LARGE_FIT_PARAMS,
                                              covariates=covariates,
                                              cases=cases,
                                              discharges=discharges,
                                              dist_params=FULL_DIST_PARAMS)

    benchmark(likelihood.likelihood, intensity, cases)
示例#3
0
def test_carehome_intensity_no_discharges(small_cases, small_covariates):
    '''Test that the behaviour of carehome_intensity in the case where
    discharges are not considered.'''
    _, cases = small_cases
    _, covariates = small_covariates
    fit_params_no_rh = {**SMALL_FIT_PARAMS, 'r_h': None}
    intensity = likelihood.carehome_intensity(covariates=covariates,
                                              cases=cases,
                                              fit_params=fit_params_no_rh,
                                              dist_params=SIMPLE_DIST_PARAMS)
    assert_almost_equal(
        intensity, [[1, 2, 2], [1.736, 2.184, 3.104], [2.277, 2.135, 3.364]],
        decimal=3)
示例#4
0
def test_intensity_performance_self(large_test_data, benchmark, use_cache):
    '''
    Test the performance of the intensity function with self-excitation
    '''

    _, cases, covariates, _ = large_test_data

    if not use_cache:
        # Writeable arrays are not cached
        cases.flags.writeable = True
        covariates.flags.writeable = True

    kwargs = {
        'fit_params': {
            **LARGE_FIT_PARAMS, 'r_h': None
        },
        'covariates': covariates,
        'cases': cases,
        'dist_params': FULL_DIST_PARAMS
    }
    # Ensure that numba can jit the function before timing it
    likelihood.carehome_intensity(**kwargs)
    benchmark(likelihood.carehome_intensity, **kwargs)
示例#5
0
def test_carehome_intensity_with_discharges(small_cases, small_covariates):
    '''Test that the behaviour of carehome_intensity is correct in the case
    where discharges are considered.'''
    _, cases = small_cases
    _, covariates = small_covariates
    discharges = cases[::-1]
    intensity = likelihood.carehome_intensity(covariates=covariates,
                                              cases=cases,
                                              fit_params=SMALL_FIT_PARAMS,
                                              dist_params=SIMPLE_DIST_PARAMS,
                                              discharges=discharges)
    assert_almost_equal(
        intensity, [[1, 2, 2], [2.077, 5.937, 3.217], [3.332, 11.240, 3.810]],
        decimal=3)
示例#6
0
    def perform(self, node, inputs, outputs):
        """
        Perform the Op; get the log-likelihood of the data given the inputs.
        """

        start_index = 0
        if self.fixed_r_c is None:
            r_c = inputs[0][0]
            start_index += 1
        else:
            r_c = self.fixed_r_c

        if self.fixed_r_h is None:
            r_h = inputs[0][start_index]
            start_index += 1
        else:
            r_h = self.fixed_r_h

        fit_params = {
            'baseline_intensities': np.asarray(
                inputs[0][start_index:]
            ),
            'r_c': r_c,
            'r_h': r_h
        }

        if (r_c == 0) and (r_h == 0):
            intensity = likelihood.carehome_intensity_null(
                covariates=self.covariates,
                cases=self.cases,
                fit_params=fit_params
            )
        else:
            intensity = likelihood.carehome_intensity(
                covariates=self.covariates,
                cases=self.cases,
                discharges=self.discharges,
                fit_params=fit_params,
                dist_params=self.dist_params
            )

        logl = likelihood.likelihood(intensity, self.cases)
        if outputs is not None:
            outputs[0][0] = np.array(logl)
        else:
            return logl