Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider evaluating predictive cdfs at a vector of category boundaries rather than looping over them #4

Open
elray1 opened this issue Jan 30, 2024 · 1 comment

Comments

@elray1
Copy link
Contributor

elray1 commented Jan 30, 2024

looking at this code:

for (i in 1:(num_cat-1)) {
truth_df_filtered[["crit_current"]] <- truth_df_filtered[[paste0("crit", i, sep="")]]
train_temp <- truth_df_filtered |>
dplyr::group_by(model_id, date, location, horizon, target, target_end_date) |>
dplyr::summarize(
cdf_crit_current = distfromq::make_p_fn(
ps = output_type_id,
qs = value)(unique(crit_current), log = FALSE)
) |>
dplyr::ungroup() |>
dplyr::select(-c(target, target_end_date))
train_forecasts[[paste0("cdf_crit", i)]] <- train_temp[["cdf_crit_current"]]
}
#calculate percentages, correcting for negative numbers
exp_forecast <- train_forecasts |>
dplyr::ungroup() |>
dplyr::rename(reference_date=date, target=target_variable) |>
dplyr::mutate(cdf_crit0=1, .before=cdf_crit1)
exp_forecast[[paste0("cdf_crit", num_cat)]] <- exp_forecast[[paste0("crit", num_cat)]] <- 0
cdf_crit_sum <- 0
for (i in 1:(num_cat)) {
if (cdf_crit_sum < 1) {
exp_forecast[[categories[i]]] <- exp_forecast[[paste0("cdf_crit", i-1)]] - ifelse(exp_forecast[[paste0("crit", i)]] > 0, exp_forecast[[paste0("cdf_crit", i)]], 0)
} else {
exp_forecast[[categories[i]]] <- 0
}
cdf_crit_sum <- cdf_crit_sum + mean(exp_forecast[[categories[i]]])
}

It seems like we should be able to avoid the for loops here by:

  • evaluating cdfs that come from distfromq at the vector of all endpoints in columns like truth_df_filtered[[paste0("crit", seq_len(num_cat - 1), sep="")]]
  • appending a 1 to the end of that vector (and maybe a 0 to the beginning)
  • taking the diff of the result to get from "cumulative category probabilities" to the pmf values
  • you may need to unnest the results at the end

It may be helpful to split all of that functionality out into a helper function

@elray1
Copy link
Contributor Author

elray1 commented Feb 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant