Priesemann-Group / covid19_inference

Bayesian python toolbox for inference and forecast of the spread of the Coronavirus
GNU General Public License v3.0
73 stars 70 forks source link

[Draft] Refactor functions to be more generally usable #64

Closed semohr closed 1 year ago

semohr commented 1 year ago

Reusing existing functions in our package can be hard from time to time. Especially if you want to pass another distribution to a lower level function.

For example, if you want to change the delay-width distribution in the delay_cases function it is difficult or even impossible if you do not want to jump into the source code of our package!

Currently, the kwargs of delay_cases look as follows

def delay_cases(
    cases,
    name_delay="delay",
    name_cases=None,
    name_width="delay-width",
    pr_mean_of_median=10,
    pr_sigma_of_median=0.2,
    pr_median_of_width=0.3,
    pr_sigma_of_width=None,
    ...
):
    ...

Here I propose to change the kwargs to incorporate for more general usage! Giving users the possibility to pass pymc distributions to the functions (like Gamma, or anything else ). This applies to nearly all functions in the model submodule.

Taking the example from before this will look as follows:

def delay_cases(
    cases,
    median_delay=None,
    median_delay_kwargs={
        "name": "delay",
        "mu": np.log(10),
        "sigma": 0.2,
    },
    # Scale of delay
    scale_delay=None,
    scale_delay_kwargs={"name": "delay-width", "mu": 0.3, "sigma": None},
    ...
):
    ...

This allows a user to pass a custom median delay distribution or specify other kwargs for the default distribution. This also makes the code more readable in my opinion.

Todo

Update model submodule

Update examples:

Other:

Considerations

This will most likely break all legacy code! But as the legacy code most likely uses pymc3 i.e. covid19_inference~0.3 this should not be a big problem.