| KLDivergence | R Documentation |
Implements the Kullback-Leibler Divergence (KLD) calculation between two probability distributions using histograms. The class can detect drift by comparing the divergence to a predefined threshold.
The Kullback-Leibler Divergence (KLD) is a measure of how one probability distribution diverges from a second, expected probability distribution. This class uses histograms to approximate the distributions and calculates the KLD to detect changes over time. If the divergence exceeds a predefined threshold, it signals a detected drift.
epsilonValue to add to small probabilities to avoid log(0) issues.
baseThe base of the logarithm used in KLD calculation.
binsNumber of bins used for the histogram.
drift_levelThe threshold for detecting drift.
drift_detectedBoolean indicating if drift has been detected.
pInitial distribution.
kl_resultThe result of the KLD calculation.
new()Initializes the KLDivergence class.
KLDivergence$new(epsilon = 1e-10, base = exp(1), bins = 10, drift_level = 0.2)
epsilonValue to add to small probabilities to avoid log(0) issues.
baseThe base of the logarithm used in KLD calculation.
binsNumber of bins used for the histogram.
drift_levelThe threshold for detecting drift.
reset()Resets the internal state of the detector.
KLDivergence$reset()
set_initial_distribution()Sets the initial distribution.
KLDivergence$set_initial_distribution(initial_p)
initial_pThe initial distribution.
add_distribution()Adds a new distribution and calculates the KLD.
KLDivergence$add_distribution(q)
qThe new distribution.
calculate_kld()Calculates the KLD between two distributions.
KLDivergence$calculate_kld(p, q)
pThe initial distribution.
qThe new distribution.
The KLD value.
get_kl_result()Returns the current KLD result.
KLDivergence$get_kl_result()
The current KLD value.
is_drift_detected()Checks if drift has been detected.
KLDivergence$is_drift_detected()
TRUE if drift is detected, otherwise FALSE.
clone()The objects of this class are cloneable with this method.
KLDivergence$clone(deep = FALSE)
deepWhether to make a deep clone.
Kullback, S., and Leibler, R.A. (1951). On Information and Sufficiency. Annals of Mathematical Statistics, 22(1), 79-86.
set.seed(123) # Setting a seed for reproducibility
initial_data <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
kld <- KLDivergence$new(bins = 10, drift_level = 0.2)
kld$set_initial_distribution(initial_data)
new_data <- c(0.2, 0.2, 0.3, 0.4, 0.4, 0.5, 0.6, 0.7, 0.7, 0.8)
kld$add_distribution(new_data)
kl_result <- kld$get_kl_result()
message(paste("KL Divergence:", kl_result))
if (kld$is_drift_detected()) {
message("Drift detected.")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.