Skip to content

Commit

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

Expand Down Expand Up @@ -255,6 +256,7 @@ load_staticlinear_risk_model_definition(const nlohmann::json &opt, const Configu
// Write time trend data structures.
trend_models->emplace_back(std::move(trend_model));
trend_ranges->emplace_back(trend_json_params["Range"].get<core::DoubleInterval>());
trend_lambda->emplace_back(trend_json_params["Lambda"].get<double>());

// Load expected value trends.
(*expected_trend)[key] = json_params["ExpectedTrend"].get<double>();
Expand Down Expand Up @@ -348,8 +350,9 @@ load_staticlinear_risk_model_definition(const nlohmann::json &opt, const Configu
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::move(policy_cholesky), std::move(trend_models), std::move(trend_ranges),
std::move(trend_lambda), info_speed, std::move(rural_prevalence), std::move(income_models),
physical_activity_stddev);
}

std::unique_ptr<hgps::RiskFactorModelDefinition>
Expand Down
26 changes: 16 additions & 10 deletions src/HealthGPS/static_linear_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ StaticLinearModel::StaticLinearModel(
const std::vector<LinearModelParams> &policy_models,
const std::vector<core::DoubleInterval> &policy_ranges, const Eigen::MatrixXd &policy_cholesky,
std::shared_ptr<std::vector<LinearModelParams>> trend_models,
std::shared_ptr<std::vector<core::DoubleInterval>> trend_ranges, double info_speed,
std::shared_ptr<std::vector<core::DoubleInterval>> trend_ranges,
std::shared_ptr<std::vector<double>> trend_lambda, double info_speed,
const std::unordered_map<core::Identifier, std::unordered_map<core::Gender, double>>
&rural_prevalence,
const std::unordered_map<core::Income, LinearModelParams> &income_models,
Expand All @@ -27,8 +28,8 @@ StaticLinearModel::StaticLinearModel(
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},
rural_prevalence_{rural_prevalence}, income_models_{income_models},
trend_ranges_{std::move(trend_ranges)}, trend_lambda_{std::move(trend_lambda)},
info_speed_{info_speed}, rural_prevalence_{rural_prevalence}, income_models_{income_models},
physical_activity_stddev_{physical_activity_stddev} {}

RiskFactorModelType StaticLinearModel::type() const noexcept { return RiskFactorModelType::Static; }
Expand Down Expand Up @@ -210,9 +211,8 @@ void StaticLinearModel::update_trends(RuntimeContext &context, Person &person) c
// 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]);
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 @@ -435,7 +435,8 @@ StaticLinearModelDefinition::StaticLinearModelDefinition(
std::vector<double> stddev, Eigen::MatrixXd cholesky,
std::vector<LinearModelParams> policy_models, std::vector<core::DoubleInterval> policy_ranges,
Eigen::MatrixXd policy_cholesky, std::unique_ptr<std::vector<LinearModelParams>> trend_models,
std::unique_ptr<std::vector<core::DoubleInterval>> trend_ranges, double info_speed,
std::unique_ptr<std::vector<core::DoubleInterval>> trend_ranges,
std::unique_ptr<std::vector<double>> trend_lambda, double info_speed,
std::unordered_map<core::Identifier, std::unordered_map<core::Gender, double>> rural_prevalence,
std::unordered_map<core::Income, LinearModelParams> income_models,
double physical_activity_stddev)
Expand All @@ -445,8 +446,9 @@ StaticLinearModelDefinition::StaticLinearModelDefinition(
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},
rural_prevalence_{std::move(rural_prevalence)}, income_models_{std::move(income_models)},
trend_ranges_{std::move(trend_ranges)}, trend_lambda_{std::move(trend_lambda)},
info_speed_{info_speed}, rural_prevalence_{std::move(rural_prevalence)},
income_models_{std::move(income_models)},
physical_activity_stddev_{physical_activity_stddev} {

if (names_.empty()) {
Expand Down Expand Up @@ -482,6 +484,9 @@ StaticLinearModelDefinition::StaticLinearModelDefinition(
if (trend_ranges_->empty()) {
throw core::HgpsException("Time trend ranges list is empty");
}
if (trend_lambda_->empty()) {
throw core::HgpsException("Time trend lambda list is empty");
}
if (rural_prevalence_.empty()) {
throw core::HgpsException("Rural prevalence mapping is empty");
}
Expand All @@ -502,7 +507,8 @@ std::unique_ptr<RiskFactorModel> StaticLinearModelDefinition::create_model() con
return std::make_unique<StaticLinearModel>(
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_);
trend_ranges_, trend_lambda_, info_speed_, rural_prevalence_, income_models_,
physical_activity_stddev_);
}

} // namespace hgps
10 changes: 8 additions & 2 deletions src/HealthGPS/static_linear_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class StaticLinearModel final : public RiskFactorAdjustableModel {
/// @param policy_cholesky Cholesky decomposition of the intervention policy covariance matrix
/// @param trend_models The linear models used to compute a person's risk factor trends
/// @param trend_ranges The value range of each risk factor trend
/// @param trend_lambda The lambda values of the risk factor trends
/// @param info_speed The information speed of risk factor updates
/// @param rural_prevalence Rural sector prevalence for age groups and sex
/// @param income_models The income models for each income category
Expand All @@ -52,7 +53,8 @@ class StaticLinearModel final : public RiskFactorAdjustableModel {
const std::vector<core::DoubleInterval> &policy_ranges,
const Eigen::MatrixXd &policy_cholesky,
std::shared_ptr<std::vector<LinearModelParams>> trend_models,
std::shared_ptr<std::vector<core::DoubleInterval>> trend_ranges, double info_speed,
std::shared_ptr<std::vector<core::DoubleInterval>> trend_ranges,
std::shared_ptr<std::vector<double>> trend_lambda, double info_speed,
const std::unordered_map<core::Identifier, std::unordered_map<core::Gender, double>>
&rural_prevalence,
const std::unordered_map<core::Income, LinearModelParams> &income_models,
Expand Down Expand Up @@ -126,6 +128,7 @@ class StaticLinearModel final : public RiskFactorAdjustableModel {
const Eigen::MatrixXd &policy_cholesky_;
std::shared_ptr<std::vector<LinearModelParams>> trend_models_;
std::shared_ptr<std::vector<core::DoubleInterval>> trend_ranges_;
std::shared_ptr<std::vector<double>> trend_lambda_;
const double info_speed_;
const std::unordered_map<core::Identifier, std::unordered_map<core::Gender, double>>
&rural_prevalence_;
Expand All @@ -151,6 +154,7 @@ class StaticLinearModelDefinition : public RiskFactorAdjustableModelDefinition {
/// @param policy_cholesky Cholesky decomposition of the intervention policy covariance matrix
/// @param trend_models The linear models used to compute a person's risk factor trends
/// @param trend_ranges The value range of each risk factor trend
/// @param trend_lambda The lambda values of the risk factor trends
/// @param info_speed The information speed of risk factor updates
/// @param rural_prevalence Rural sector prevalence for age groups and sex
/// @param income_models The income models for each income category
Expand All @@ -166,7 +170,8 @@ class StaticLinearModelDefinition : public RiskFactorAdjustableModelDefinition {
std::vector<LinearModelParams> policy_models,
std::vector<core::DoubleInterval> policy_ranges, Eigen::MatrixXd policy_cholesky,
std::unique_ptr<std::vector<LinearModelParams>> trend_models,
std::unique_ptr<std::vector<core::DoubleInterval>> trend_ranges, double info_speed,
std::unique_ptr<std::vector<core::DoubleInterval>> trend_ranges,
std::unique_ptr<std::vector<double>> trend_lambda, double info_speed,
std::unordered_map<core::Identifier, std::unordered_map<core::Gender, double>>
rural_prevalence,
std::unordered_map<core::Income, LinearModelParams> income_models,
Expand All @@ -189,6 +194,7 @@ class StaticLinearModelDefinition : public RiskFactorAdjustableModelDefinition {
Eigen::MatrixXd policy_cholesky_;
std::shared_ptr<std::vector<LinearModelParams>> trend_models_;
std::shared_ptr<std::vector<core::DoubleInterval>> trend_ranges_;
std::shared_ptr<std::vector<double>> trend_lambda_;
double info_speed_;
std::unordered_map<core::Identifier, std::unordered_map<core::Gender, double>>
rural_prevalence_;
Expand Down

0 comments on commit 92aadd9

Please sign in to comment.