Skip to content

laser.core.demographics

laser.core.demographics

laser.core.demographics.AliasedDistribution(counts)

A class to generate samples from a distribution using the Vose alias method.

laser.core.demographics.AliasedDistribution.sample(count=1, dtype=np.int32)

Generate samples from the distribution.

Parameters:

Name Type Description Default
count int

The number of samples to generate. Default is 1.

1

Returns:

Type Description
int

int or numpy.ndarray: A single integer if count is 1, otherwise an array of integers representing the generated samples.

laser.core.demographics.KaplanMeierEstimator(source)

Initializes the KMEstimator with the given source data.

Parameters:

Name Type Description Default
source (ndarray, list, Path, str)

The source data for the KMEstimator.

  • A numpy array of unsigned 32-bit integers.
  • A list of integers.
  • A Path object pointing to a file containing the data.
  • A string representing the file path.
required

Raises:

Type Description
FileNotFoundError

If the provided file path does not exist or is not a file.

TypeError

If the source type is not one of the accepted types (np.ndarray, list, Path, str).

ValueError

If the source inputs contain negative values or are not monotonically non-decreasing.

Notes
  • If the source is a file path, the file should contain comma-separated values with the data in the second column.
  • The source data is converted to a numpy array of unsigned 32-bit integers.

laser.core.demographics.KaplanMeierEstimator.cumulative_deaths property

Returns the original source data.

laser.core.demographics.KaplanMeierEstimator.predict_age_at_death(ages_days, max_year=None)

Calculate the predicted age at death (in days) based on the given ages in days.

Parameters:

Name Type Description Default
ages_days ndarray

The ages of the individuals in days.

required
max_year int

The maximum year to consider for calculating the predicted year of death. Default is None, which uses the maximum year from the source data.

None

Returns:

Name Type Description
age_at_death ndarray

The predicted days of death.

Example
1
predict_age_at_death(np.array([40*365, 50*365, 60*365]), max_year=80) # returns something like array([22732, 26297, 29862])

laser.core.demographics.KaplanMeierEstimator.predict_year_of_death(ages_years, max_year=None)

Calculate the predicted year of death based on the given ages in years.

Parameters:

Name Type Description Default
ages_years ndarray

The ages of the individuals in years.

required
max_year int

The maximum year to consider for calculating the predicted year of death. Default is None, which uses the maximum year from the source data.

None

Returns:

Name Type Description
year_of_death ndarray

The predicted years of death.

Example
1
predict_year_of_death(np.array([40, 50, 60]), max_year=80) # returns something like array([62, 72, 82])

laser.core.demographics.KaplanMeierEstimator.sample(current, max_index=None)

Similar to predict_year_of_death, but operates on indices rather than years. This method predicts the expiration (death) index for each individual, given their current index.

Parameters:

Name Type Description Default
current ndarray

The current indices of the individuals.

required
max_index int

The maximum index to consider for calculating the predicted expiration. Default is None, which uses the maximum index from the source data.

None

Returns:

Name Type Description
predictions ndarray

The predicted expiration indices for each individual.

laser.core.demographics.load_pyramid_csv(file, verbose=False)

Load a CSV file with population pyramid data and return it as a NumPy array.

The CSV file is expected to have the following schema:

  • The first line is a header: "Age,M,F"
  • Subsequent lines contain age ranges and population counts for males and females:

1
2
3
"low-high,#males,#females"
...
"max+,#males,#females"
Where low, high, males, females, and max are integer values >= 0.

The function processes the CSV file to create a NumPy array with the following columns:

  • Start age of the range
  • End age of the range
  • Number of males
  • Number of females

Parameters:

Name Type Description Default
file Path

The path to the CSV file.

required
verbose bool

If True, prints the file reading status. Default is False.

False

Returns:

Type Description
ndarray

np.ndarray: A NumPy array with the processed population pyramid data.