Skip to content

Commit

Permalink
fix simulations in Regressor
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymoudiki committed Feb 22, 2024
1 parent f883c55 commit 4a103e7
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 134 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.html
.Rbuildignore
*.Rproj
.Rproj.user
Expand Down
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Package: learningmachine
Type: Package
Title: Machine Learning with uncertainty quantification
Title: Machine Learning with uncertainty quantification and explanations
Version: 1.0.0
Date: 2024-02-15
Date: 2024-02-24
Author: T. Moudiki
Maintainer: T. Moudiki <[email protected]>
Description: Machine Learning Models with uncertainty quantification.
License: MIT
Description: Machine Learning Models with uncertainty quantification and explanations.
License: BSD_3_clause + file LICENSE
Imports: Rcpp (>= 1.0.10), R6
Depends: tseries
LinkingTo: Rcpp
RoxygenNote: 7.3.0
Encoding: UTF-8
Expand Down
33 changes: 29 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
Copyright 2023 T. Moudiki | The MIT License
The Clear BSD License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Copyright (c) [2023-2024] [Thierry Moudiki]
All rights reserved.

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ importFrom(Rcpp, evalCpp)
importFrom("stats", "median")
importFrom("stats", "predict")
importFrom("stats", "quantile")
importFrom("tseries", "tsbootstrap")
importFrom("tseries", "surrogate")
importFrom("utils", "installed.packages")
export(Base)
export(Regressor)
Expand Down
165 changes: 74 additions & 91 deletions R/Regressor.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ Regressor <-
predict = function(X,
level = NULL,
...) {
if (identical(self$pi_method, "none") &&
(is.null(self$model) || is.null(self$engine)))
if (is.null(self$model) || is.null(self$engine))
stop(
paste0(
self$name,
Expand All @@ -238,7 +237,7 @@ Regressor <-
# no prediction interval
return(preds)
} else {
# prediction intervals
# prediction intervals and predictive simulations
if (!is.null(self$level) &&
!is.null(level) && self$level != level)
{
Expand All @@ -256,103 +255,79 @@ Regressor <-
self$set_level(level)
}

if (self$pi_method %in% c("jackknifeplus", "kdejackknifeplus"))
if (self$pi_method %in% c("splitconformal",
"jackknifeplus"))
{
if (identical(self$pi_method, "jackknifeplus"))
{
quantile_absolute_residuals <-
quantile_scp(private$abs_calib_resids,
alpha = (1 - self$level / 100))
return(
list(
preds = preds,
lower = preds - quantile_absolute_residuals,
upper = preds + quantile_absolute_residuals
)
quantile_absolute_residuals <-
quantile_scp(private$abs_calib_resids,
alpha = (1 - self$level / 100))
return(
list(
preds = preds,
lower = preds - quantile_absolute_residuals,
upper = preds + quantile_absolute_residuals
)
}

if (identical(self$pi_method, "kdejackknifeplus"))
{
scaled_raw_residuals <-
scale(private$calib_resids,
center = TRUE,
scale = TRUE)
sd_raw_residuals <- sd(private$calib_resids)
simulated_raw_calibrated_residuals <-
rgaussiandens(
private$calib_resids,
n = length(preds),
p = self$B,
seed = self$seed
)
sims <-
replicate(self$B, preds) + sd_raw_residuals * simulated_raw_calibrated_residuals
preds_lower <-
apply(sims, 1, function(x)
quantile(x, probs = (1 - self$level / 100) / 2))
preds_upper <-
apply(sims, 1, function(x)
quantile(x, probs = 1 - (1 - self$level / 100) / 2))
return(list(
preds = apply(sims, 1, mean),
sims = sims,
lower = preds_lower,
upper = preds_upper
))
}
)
}

if (self$pi_method %in% c("splitconformal", "kdesplitconformal"))
if (self$pi_method %in% c("kdejackknifeplus",
"bootjackknifeplus",
"surrjackknifeplus",
"kdesplitconformal",
"bootsplitconformal",
"surrsplitconformal"))
{
if (identical(self$pi_method, "splitconformal"))
scaled_raw_residuals <- scale(private$calib_resids,
center = TRUE,
scale = TRUE)
sd_raw_residuals <- sd(private$calib_resids)

set.seed(self$seed)

if (self$pi_method %in% c("kdejackknifeplus", "kdesplitconformal"))
{
quantile_absolute_residuals <-
quantile_scp(private$abs_calib_resids,
alpha = (1 - self$level / 100))
return(
list(
preds = preds,
lower = preds - quantile_absolute_residuals,
upper = preds + quantile_absolute_residuals
)
simulated_raw_calibrated_residuals <- rgaussiandens(
private$calib_resids,
n = length(preds),
p = self$B,
seed = self$seed
)
}

if (identical(self$pi_method, "kdesplitconformal"))
if (self$pi_method %in% c("bootjackknifeplus", "bootsplitconformal"))
{
scaled_calibrated_residuals <-
base::scale(private$calib_resids,
center = TRUE,
scale = TRUE)
sd_calibrated_residuals <- sd(private$calib_resids)
simulated_scaled_calibrated_residuals <-
rgaussiandens(
scaled_calibrated_residuals,
n =
length(preds),
p =
self$B,
seed =
self$seed
)
matrix_preds <- replicate(self$B, preds)
sims <-
matrix_preds + sd_calibrated_residuals * simulated_scaled_calibrated_residuals
preds_lower <-
apply(sims, 1, function(x)
quantile(x, probs = (1 - self$level / 100) / 2))
preds_upper <-
apply(sims, 1, function(x)
quantile(x, probs = 1 - (1 - self$level / 100) / 2))
return(list(
preds = apply(sims, 1, mean),
sims = sims,
lower = preds_lower,
upper = preds_upper
))
simulated_raw_calibrated_residuals <- replicate(self$B,
base::sample(x = private$calib_resids,
size=length(preds),
replace = TRUE))
}

if (self$pi_method %in% c("surrsplitconformal", "surrjackknifeplus"))
{
if (length(preds) > length(private$calib_resids))
{
stop("For surrogates, must have number of predictions < number of training observations")
}
simulated_raw_calibrated_residuals <- tseries::surrogate(
x = private$calib_resids,
ns = self$B
)[seq_along(preds), ]
}

sims <-
replicate(self$B, preds) + sd_raw_residuals * simulated_raw_calibrated_residuals
preds_lower <-
apply(sims, 1, function(x)
quantile(x, probs = (1 - self$level / 100) / 2))
preds_upper <-
apply(sims, 1, function(x)
quantile(x, probs = 1 - (1 - self$level / 100) / 2))
return(list(
preds = apply(sims, 1, mean),
sims = sims,
lower = preds_lower,
upper = preds_upper
))
}
}
},
Expand All @@ -369,9 +344,9 @@ Regressor <-
level = NULL,
pi_method = c(
"splitconformal",
"jackknifeplus",
"kdesplitconformal",
"bootsplitconformal",
"jackknifeplus",
"kdejackknifeplus",
"bootjackknifeplus",
"surrsplitconformal",
Expand All @@ -383,6 +358,7 @@ Regressor <-
...) {
stopifnot(pct_train >= 0.4 && pct_train < 1)
stopifnot(length(y) == nrow(X))

if (!is.null(level) && level != self$level)
{
warning(paste0(
Expand All @@ -393,6 +369,12 @@ Regressor <-
))
self$level <- level
}

if (is.null(self$level) && !is.null(level))
{
self$level <- level
}

self$params <- list(...)
pi_method <- match.arg(pi_method)
set.seed(seed)
Expand All @@ -413,11 +395,12 @@ Regressor <-
if (!is.null(self$level))
# prediction intervals requested
{
res <- fit_obj$predict(X_test)
res <- fit_obj$predict(X_test, level = self$level)

if ((graph == TRUE) &&
(!is.factor(y))) {
y_values <- c(y_train, res$preds)
y_values <- c(y_train,
res$preds)
y_upper <-
c(y_train, res$upper)
y_lower <-
Expand Down
13 changes: 0 additions & 13 deletions Read-and-delete-me

This file was deleted.

19 changes: 0 additions & 19 deletions renv/settings.json

This file was deleted.

Loading

0 comments on commit 4a103e7

Please sign in to comment.