コード例 #1
0
ファイル: gem_tests.py プロジェクト: vup1120/hamlet
def mfd_empirical_likelihood_test(cfg: dict, bin_gdf: GeoDataFrame) -> None:
    """
    Calculates the (log)likelihood of observing the earthquakes in the seismic
    catalog in each :class:`~openquake.hme.utils.bins.SpacemagBin` as the
    geometric mean of the Poisson likelihoods of observing the earthquakes
    within each :class:`~openquake.hme.utils.bins.MagBin` of the
    :class:`~openquake.hme.utils.bins.SpacemagBin`.

    The likelihoods are calculated using the empirical likelihood of observing
    the number of events that occurred in each
    :class:`~openquake.hme.utils.bins.MagBin` given the occurrence rate for 
    that :class:`~openquake.hme.utils.bins.MagBin`. This is done through a Monte
    Carlo simulation, which returns the fraction of the total Monte Carlo
    samples had the same number of events as observed.

    The likelihoods for each :class:`~openquake.hme.utils.bins.SpacemagBin` are
    then log-transformed and appended as a new column to the `bin_gdf`
    :class:`GeoDataFrame` hosting the bins.
    """

    test_config = cfg["config"]["model_framework"]["gem"]["likelihood"]
    source_bin_gdf = get_source_bins(bin_gdf)

    logging.info("calculating empirical MFDs for source bins")

    if cfg["config"]["parallel"] is False:
        source_bin_mfds = source_bin_gdf["SpacemagBin"].apply(
            get_stochastic_mfd,
            n_iters=test_config["n_iters"],
            interval_length=test_config["investigation_time"],
        )
    else:
        source_bin_mfds = get_stochastic_mfds_parallel(
            source_bin_gdf["SpacemagBin"],
            n_iters=test_config["n_iters"],
            interval_length=test_config["investigation_time"],
        )

    def calc_row_log_like(row, mfd_df=source_bin_mfds):
        obs_eqs = row.SpacemagBin.observed_earthquakes
        mfd_dict = mfd_df.loc[row._name]

        return calc_mfd_log_likelihood_independent(
            obs_eqs,
            mfd_dict,
            not_modeled_val=test_config["not_modeled_val"],
            likelihood_method="empirical",
        )

    logging.info("calculating log likelihoods for sources")
    source_bin_log_likes = source_bin_gdf.apply(calc_row_log_like, axis=1)

    bin_gdf["log_like"] = test_config["default_likelihood"]
    bin_gdf["log_like"].update(source_bin_log_likes)
コード例 #2
0
def mfd_poisson_likelihood_test(cfg: dict, bin_gdf: GeoDataFrame) -> None:
    """
    Calculates the (log)likelihood of observing the earthquakes in the seismic
    catalog in each :class:`~openquake.hme.utils.bins.SpacemagBin` as the
    geometric mean of the Poisson likelihoods of observing the earthquakes
    within each :class:`~openquake.hme.utils.bins.MagBin` of the
    :class:`~openquake.hme.utils.bins.SpacemagBin`.

    The likelihoods are calculated using the Poisson likelihood of observing
    the number of events that occurred in each
    :class:`~openquake.hme.utils.bins.MagBin` given the occurrence rate for
    that :class:`~openquake.hme.utils.bins.MagBin`.  See
    :func:`~openquake.hme.utils.stats.poisson_likelihood` for more information.

    The likelihoods for each :class:`~openquake.hme.utils.bins.SpacemagBin` are
    then log-transformed and appended as a new column to the `bin_gdf`
    :class:`GeoDataFrame` hosting the bins.
    """

    test_config = cfg["config"]["model_framework"]["gem"]["likelihood"]
    source_bin_gdf = get_source_bins(bin_gdf)

    logging.info("calculating empirical MFDs for source bins")

    source_bin_mfds = source_bin_gdf["SpacemagBin"].apply(
        lambda x: x.get_rupture_mfd(cumulative=False)
    )

    def calc_row_log_like(row, mfd_df=source_bin_mfds):
        obs_eqs = row.SpacemagBin.observed_earthquakes
        mfd_dict = mfd_df.loc[row._name]

        return calc_mfd_log_likelihood_independent(
            obs_eqs,
            mfd_dict,
            time_interval=test_config["investigation_time"],
            not_modeled_val=test_config["not_modeled_val"],
            likelihood_method="poisson",
        )

    logging.info("calculating log likelihoods for sources")
    source_bin_log_likes = source_bin_gdf.apply(calc_row_log_like, axis=1)

    bin_gdf["log_like"] = test_config["default_likelihood"]
    bin_gdf["log_like"].update(source_bin_log_likes)