Skip to content

Commit

Permalink
Load and use trend boxcox param.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesturner246 committed Aug 21, 2024
1 parent d302c88 commit 34b3be2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/HealthGPS.Input/model_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ load_staticlinear_risk_model_definition(const nlohmann::json &opt, const Configu
auto trend_models = std::make_unique<std::vector<LinearModelParams>>();
auto trend_ranges = std::make_unique<std::vector<core::DoubleInterval>>();
auto expected_trend = std::make_unique<std::unordered_map<core::Identifier, double>>();
auto expected_trend_boxcox = std::make_unique<std::unordered_map<core::Identifier, double>>();

size_t i = 0;
for (const auto &[key, json_params] : opt["RiskFactorModels"].items()) {
Expand Down Expand Up @@ -257,6 +258,7 @@ load_staticlinear_risk_model_definition(const nlohmann::json &opt, const Configu

// Load expected value trends.
(*expected_trend)[key] = json_params["ExpectedTrend"].get<double>();
(*expected_trend_boxcox)[key] = json_params["ExpectedTrendBoxCox"].get<double>();

// Increment table column index.
i++;
Expand Down Expand Up @@ -343,11 +345,11 @@ load_staticlinear_risk_model_definition(const nlohmann::json &opt, const Configu
const double physical_activity_stddev = opt["PhysicalActivityStdDev"].get<double>();

return std::make_unique<StaticLinearModelDefinition>(
std::move(expected), std::move(expected_trend), std::move(names), std::move(models),
std::move(ranges), std::move(lambda), std::move(stddev), std::move(cholesky),
std::move(policy_models), std::move(policy_ranges), std::move(policy_cholesky),
std::move(trend_models), std::move(trend_ranges), info_speed, std::move(rural_prevalence),
std::move(income_models), physical_activity_stddev);
std::move(expected), std::move(expected_trend), std::move(expected_trend_boxcox),
std::move(names), std::move(models), std::move(ranges), std::move(lambda),
std::move(stddev), std::move(cholesky), std::move(policy_models), std::move(policy_ranges),
std::move(policy_cholesky), std::move(trend_models), std::move(trend_ranges), info_speed,
std::move(rural_prevalence), std::move(income_models), physical_activity_stddev);
}

std::unique_ptr<hgps::RiskFactorModelDefinition>
Expand Down
28 changes: 19 additions & 9 deletions src/HealthGPS/static_linear_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace hgps {
StaticLinearModel::StaticLinearModel(
std::shared_ptr<RiskFactorSexAgeTable> expected,
std::shared_ptr<std::unordered_map<core::Identifier, double>> expected_trend,
std::shared_ptr<std::unordered_map<core::Identifier, double>> expected_trend_boxcox,
const std::vector<core::Identifier> &names, const std::vector<LinearModelParams> &models,
const std::vector<core::DoubleInterval> &ranges, const std::vector<double> &lambda,
const std::vector<double> &stddev, const Eigen::MatrixXd &cholesky,
Expand All @@ -21,8 +22,9 @@ StaticLinearModel::StaticLinearModel(
&rural_prevalence,
const std::unordered_map<core::Income, LinearModelParams> &income_models,
double physical_activity_stddev)
: RiskFactorAdjustableModel{std::move(expected), std::move(expected_trend)}, names_{names},
models_{models}, ranges_{ranges}, lambda_{lambda}, stddev_{stddev}, cholesky_{cholesky},
: RiskFactorAdjustableModel{std::move(expected), std::move(expected_trend)},
expected_trend_boxcox_{expected_trend_boxcox}, names_{names}, models_{models},
ranges_{ranges}, lambda_{lambda}, stddev_{stddev}, cholesky_{cholesky},
policy_models_{policy_models}, policy_ranges_{policy_ranges},
policy_cholesky_{policy_cholesky}, trend_models_{std::move(trend_models)},
trend_ranges_{std::move(trend_ranges)}, info_speed_{info_speed},
Expand Down Expand Up @@ -205,9 +207,12 @@ void StaticLinearModel::update_trends(RuntimeContext &context, Person &person) c
// Apply trends (should exist).
for (size_t i = 0; i < names_.size(); i++) {

// Load trend.
// Load trend and apply inverse BoxCox transform.
auto trend_name = core::Identifier{names_[i].to_string() + "_trend"};
double trend = person.risk_factors.at(trend_name);
// TODO: use trend_lambda_[i] below
// double boxcox_factor = pow(expected_trend_boxcox_->at(names_[i]), elapsed_time);
// trend = boxcox_factor * inverse_box_cox(trend, trend_lambda_[i]);

// Apply trend to risk factor.
double factor = person.risk_factors.at(names_[i]);
Expand Down Expand Up @@ -424,6 +429,7 @@ void StaticLinearModel::initialise_physical_activity(RuntimeContext &context, Pe
StaticLinearModelDefinition::StaticLinearModelDefinition(
std::unique_ptr<RiskFactorSexAgeTable> expected,
std::unique_ptr<std::unordered_map<core::Identifier, double>> expected_trend,
std::unique_ptr<std::unordered_map<core::Identifier, double>> expected_trend_boxcox,
std::vector<core::Identifier> names, std::vector<LinearModelParams> models,
std::vector<core::DoubleInterval> ranges, std::vector<double> lambda,
std::vector<double> stddev, Eigen::MatrixXd cholesky,
Expand All @@ -434,8 +440,9 @@ StaticLinearModelDefinition::StaticLinearModelDefinition(
std::unordered_map<core::Income, LinearModelParams> income_models,
double physical_activity_stddev)
: RiskFactorAdjustableModelDefinition{std::move(expected), std::move(expected_trend)},
names_{std::move(names)}, models_{std::move(models)}, ranges_{std::move(ranges)},
lambda_{std::move(lambda)}, stddev_{std::move(stddev)}, cholesky_{std::move(cholesky)},
expected_trend_boxcox_{std::move(expected_trend_boxcox)}, names_{std::move(names)},
models_{std::move(models)}, ranges_{std::move(ranges)}, lambda_{std::move(lambda)},
stddev_{std::move(stddev)}, cholesky_{std::move(cholesky)},
policy_models_{std::move(policy_models)}, policy_ranges_{std::move(policy_ranges)},
policy_cholesky_{std::move(policy_cholesky)}, trend_models_{std::move(trend_models)},
trend_ranges_{std::move(trend_ranges)}, info_speed_{info_speed},
Expand Down Expand Up @@ -483,16 +490,19 @@ StaticLinearModelDefinition::StaticLinearModelDefinition(
}
for (const auto &name : names_) {
if (!expected_trend_->contains(name)) {
throw core::HgpsException("One or more risk factor expected trend value is missing");
throw core::HgpsException("One or more expected trend value is missing");
}
if (!expected_trend_boxcox_->contains(name)) {
throw core::HgpsException("One or more expected trend BoxCox value is missing");
}
}
}

std::unique_ptr<RiskFactorModel> StaticLinearModelDefinition::create_model() const {
return std::make_unique<StaticLinearModel>(
expected_, expected_trend_, names_, models_, ranges_, lambda_, stddev_, cholesky_,
policy_models_, policy_ranges_, policy_cholesky_, trend_models_, trend_ranges_, info_speed_,
rural_prevalence_, income_models_, physical_activity_stddev_);
expected_, expected_trend_, expected_trend_boxcox_, names_, models_, ranges_, lambda_,
stddev_, cholesky_, policy_models_, policy_ranges_, policy_cholesky_, trend_models_,
trend_ranges_, info_speed_, rural_prevalence_, income_models_, physical_activity_stddev_);
}

} // namespace hgps
6 changes: 6 additions & 0 deletions src/HealthGPS/static_linear_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class StaticLinearModel final : public RiskFactorAdjustableModel {
/// @brief Initialises a new instance of the StaticLinearModel class
/// @param expected The expected risk factor values by sex and age
/// @param expected_trend The expected trend of risk factor values
/// @param expected_trend_boxcox The expected boxcox factor
/// @param names The risk factor names
/// @param models The linear models used to compute a person's risk factor values
/// @param ranges The value range of each risk factor
Expand All @@ -43,6 +44,7 @@ class StaticLinearModel final : public RiskFactorAdjustableModel {
StaticLinearModel(
std::shared_ptr<RiskFactorSexAgeTable> expected,
std::shared_ptr<std::unordered_map<core::Identifier, double>> expected_trend,
std::shared_ptr<std::unordered_map<core::Identifier, double>> expected_trend_boxcox,
const std::vector<core::Identifier> &names, const std::vector<LinearModelParams> &models,
const std::vector<core::DoubleInterval> &ranges, const std::vector<double> &lambda,
const std::vector<double> &stddev, const Eigen::MatrixXd &cholesky,
Expand Down Expand Up @@ -112,6 +114,7 @@ class StaticLinearModel final : public RiskFactorAdjustableModel {
void initialise_physical_activity(RuntimeContext &context, Person &person,
Random &random) const;

std::shared_ptr<std::unordered_map<core::Identifier, double>> expected_trend_boxcox_;
const std::vector<core::Identifier> &names_;
const std::vector<LinearModelParams> &models_;
const std::vector<core::DoubleInterval> &ranges_;
Expand All @@ -136,6 +139,7 @@ class StaticLinearModelDefinition : public RiskFactorAdjustableModelDefinition {
/// @brief Initialises a new instance of the StaticLinearModelDefinition class
/// @param expected The expected risk factor values by sex and age
/// @param expected_trend The expected trend of risk factor values
/// @param expected_trend_boxcox The expected boxcox factor
/// @param names The risk factor names
/// @param models The linear models used to compute a person's risk factors
/// @param ranges The value range of each risk factor
Expand All @@ -155,6 +159,7 @@ class StaticLinearModelDefinition : public RiskFactorAdjustableModelDefinition {
StaticLinearModelDefinition(
std::unique_ptr<RiskFactorSexAgeTable> expected,
std::unique_ptr<std::unordered_map<core::Identifier, double>> expected_trend,
std::unique_ptr<std::unordered_map<core::Identifier, double>> expected_trend_boxcox,
std::vector<core::Identifier> names, std::vector<LinearModelParams> models,
std::vector<core::DoubleInterval> ranges, std::vector<double> lambda,
std::vector<double> stddev, Eigen::MatrixXd cholesky,
Expand All @@ -172,6 +177,7 @@ class StaticLinearModelDefinition : public RiskFactorAdjustableModelDefinition {
std::unique_ptr<RiskFactorModel> create_model() const override;

private:
std::shared_ptr<std::unordered_map<core::Identifier, double>> expected_trend_boxcox_;
std::vector<core::Identifier> names_;
std::vector<LinearModelParams> models_;
std::vector<core::DoubleInterval> ranges_;
Expand Down

0 comments on commit 34b3be2

Please sign in to comment.