X-Git-Url: https://git.auder.net/%3C?a=blobdiff_plain;f=pkg%2FR%2Fm_ExponentialWeights.R;fp=pkg%2FR%2Fm_ExponentialWeights.R;h=e59e28f524d016837e8f0747bb64977a83e58963;hb=357b8be00388d07c04c10d4bf7f503fd947185d2;hp=0000000000000000000000000000000000000000;hpb=a961f8a15492bf4b18d24bc117358d1f412dd078;p=aggexp.git diff --git a/pkg/R/m_ExponentialWeights.R b/pkg/R/m_ExponentialWeights.R new file mode 100644 index 0000000..e59e28f --- /dev/null +++ b/pkg/R/m_ExponentialWeights.R @@ -0,0 +1,55 @@ +#' @include b_LinearAlgorithm.R + +#' @title Exponential Weights Algorithm +#' +#' @description Exponential Weights Algorithm. +#' Inherits \code{\link{LinearAlgorithm}} +#' +#' @field alpha Importance of weights redistribution, in [0,1]. Default: 0 +#' @field grad Whether to use or not the (sub)gradient trick. Default: FALSE +#' +ExponentialWeights = setRefClass( + Class = "ExponentialWeights", + + fields = c( + alpha = "numeric", + grad = "logical" + ), + + contains = "LinearAlgorithm", + + methods = list( + initialize = function(...) + { + callSuper(...) + if (length(alpha) == 0 || alpha < 0. || alpha > 1.) + alpha <<- 0. #no redistribution + if (length(grad) == 0) + grad <<- FALSE + }, + predict_noNA = function(XY, x) + { + K = ncol(XY) - 1 + if (K == 1) + { + #shortcut: nothing to combine + finalWeight = 1. + } + + else + { + X = XY[,names(XY) != "Measure"] + Y = XY[,"Measure"] + finalWeight = .C("ew_predict_noNA", X = as.double(t(X)), Y = as.double(Y), n = as.integer(nrow(XY)), + K = as.integer(K), alpha=as.double(alpha), grad = as.integer(grad), weight=double(K))$weight + } + + appendWeight(finalWeight) + return (matricize(x) %*% finalWeight) +# M = matricize(x) +# M[M<=30] = -1 +# M[M>30] = 1 +# return (30 + M%*%finalWeight) + } + ) +)