|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# Copyright (c) Henry Spatial Analysis. All rights reserved. |
| 3 | +# Licensed under the MIT License. See LICENSE in project root for information. |
| 4 | +# ------------------------------------------------------------- |
| 5 | + |
| 6 | +""" |
| 7 | +This module contains the EventRate class, which is used to store and calculate event rates |
| 8 | +for a given model of a Poisson process. |
| 9 | +""" |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +class EventRate: |
| 14 | + """ |
| 15 | + Stores and calculates event rates (lambda) for a given model of a Poisson process. |
| 16 | + If the event rate is constant, the event rate is a scalar, and change probabilities |
| 17 | + can be calculated directly. If the event rate is time-varying, then change |
| 18 | + probabilities have to be integrated over the time period. |
| 19 | + """ |
| 20 | + |
| 21 | + VALID_TYPES = ['constant', 'varying'] |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + fun: callable, |
| 26 | + type: str = 'constant', |
| 27 | + delta: float = 0.02 |
| 28 | + ): |
| 29 | + """ |
| 30 | + Args: |
| 31 | + fun: Function to calculate the event rate. If `type` is "constant", this |
| 32 | + function will return a scalar. If `type` is "varying", this function will |
| 33 | + return a function `f(t)` that gives the event rate at time `t`. |
| 34 | + type: Type of event rate. Must be one of {self.VALID_TYPES}. |
| 35 | + delta: Time step for a time-varying event rate function. Only used if `type` |
| 36 | + is "varying". |
| 37 | + """ |
| 38 | + if type not in self.VALID_TYPES: |
| 39 | + raise ValueError( |
| 40 | + f"Invalid event rate type: {type}. Must be one of {self.VALID_TYPES}" |
| 41 | + ) |
| 42 | + self.type = type |
| 43 | + self.fun = fun |
| 44 | + self.delta = delta |
| 45 | + |
| 46 | + def calculate_change_constant(self, t1: torch.Tensor, t2: torch.Tensor, **kwargs): |
| 47 | + """ |
| 48 | + Calculate the change probability for a constant event rate. Given lambda and a |
| 49 | + time period (t1, t2], the change probability is `lambda * (t2 - t1)` |
| 50 | + """ |
| 51 | + return (t2 - t1).reshape(-1, 1) * self.fun(**kwargs) |
| 52 | + |
| 53 | + def calculate_change_varying(self, t1: torch.Tensor, t2: torch.Tensor, **kwargs): |
| 54 | + """ |
| 55 | + Calculate the change probability for a varying event rate. Given a function `f(t)` |
| 56 | + that gives the event rate at time `t`, the change probability is the approximate |
| 57 | + integral from t1 to t2 of `f(t)` |
| 58 | + """ |
| 59 | + f = self.fun(**kwargs) |
| 60 | + t_range = torch.linspace(t1, t2, steps = max(2, int((t2 - t1) / self.delta) + 1)) |
| 61 | + return torch.trapz(y = f(t_range), x = t_range) |
| 62 | + |
| 63 | + def calculate_change(self, t1: torch.Tensor, t2: torch.Tensor, **kwargs): |
| 64 | + """ |
| 65 | + Calculate the change probability between two time periods for this event rate. |
| 66 | + """ |
| 67 | + if self.type == 'constant': |
| 68 | + return self.calculate_change_constant(t1 = t1, t2 = t2, **kwargs) |
| 69 | + elif self.type == 'varying': |
| 70 | + return self.calculate_change_varying(t1 = t1, t2 = t2, **kwargs) |
| 71 | + else: |
| 72 | + raise ValueError(f"Invalid event rate type: {self.type}") |
0 commit comments