#| label: Latin Hypercube Koella
# Load necessary libraries
library(reticulate)
library(lhs) # For Latin Hypercube Sampling
library(ggplot2) # For visualization
# Define target HQ value (e.g., from observations)
target_HQ <- 120.0
# Define the Latin-Hypercube Sampling (LHS) function to generate samples
lhs_samples <- randomLHS(10000, 2) # Generate 10000 samples for kFaktor and kGang
# Rescale the LHS samples into the appropriate ranges for kFaktor and kGang
kFaktor_vals <- lhs_samples[, 1] * (3.0 - 0.1) + 0.1 # Range: 0.1 to 3.0
kGang_vals <- lhs_samples[, 2] * (2.0 - 0.5) + 0.5 # Range: 0.5 to 2.0
# Combine into a data frame for easier processing
samples_df <- data.frame(kFaktor = kFaktor_vals, kGang = kGang_vals)
# Run the function for each sample and compute the HQ
HQ_vals <- sapply(1:nrow(samples_df), function(i) {
result <- py$koella_verfahren(
x = 20,
LGe_km = 50,
Vo20 = 35,
Gletscherflaeche_km2 = 1.2,
rs = 4,
schneeschmelze = TRUE,
kGang = samples_df$kGang[i],
kFaktor = samples_df$kFaktor[i]
)
return(result$HQ) # Return HQ value from the function
})
# Add the computed HQ values to the data frame
samples_df$HQ <- HQ_vals
# Create a 2D scatter plot of kFaktor vs. kGang colored by HQ
ggplot(samples_df, aes(x = kFaktor, y = kGang, color = HQ)) +
geom_point(alpha = 0.5) +
scale_color_viridis_c() + # Color scale for HQ
labs(title = "HQ Values as Function of kFaktor and kGang",
x = "kFaktor", y = "kGang", color = "HQ Value") +
theme_minimal()
# Load necessary libraries
library(reticulate)
library(lhs) # For Latin Hypercube Sampling
library(ggplot2) # For visualization
library(viridis) # For color scales
use_python(Sys.which("python")) # Ensure Python environment is initialized
py_run_string("") # Initialize Python access
# Define the Latin-Hypercube Sampling (LHS) function to generate samples
lhs_samples <- randomLHS(10000, 3) # Generate 10000 samples for kFaktor, kGang, and Vo20
# Rescale the LHS samples into the appropriate ranges
kFaktor_vals <- lhs_samples[, 1] * (3.0 - 0.1) + 0.1 # kFaktor: Range 0.1 to 3.0
kGang_vals <- lhs_samples[, 2] * (2.0 - 0.5) + 0.5 # kGang: Range 0.5 to 2.0
Vo20_vals <- lhs_samples[, 3] * (100 - 10) + 10 # Vo20: Range 10 to 100
# Combine into a data frame for easier processing
samples_df <- data.frame(kFaktor = kFaktor_vals, kGang = kGang_vals, Vo20 = Vo20_vals)
# Run the function for each sample and compute the HQ
HQ_vals <- sapply(1:nrow(samples_df), function(i) {
result <- py$koella_verfahren(
x = 20, # Fixed x value
LGe_km = 50,
Vo20 = samples_df$Vo20[i],
Gletscherflaeche_km2 = 1.2,
rs = 4,
schneeschmelze = TRUE,
kGang = samples_df$kGang[i],
kFaktor = samples_df$kFaktor[i]
)
return(result$HQ) # Return HQ value from the function
})
# Add the computed HQ values to the data frame
samples_df$HQ <- HQ_vals
# Now, we create 2D scatter plots for all pairs of parameters
# Function to create 2D scatter plot
plot_2d <- function(df, x_var, y_var) {
ggplot(df, aes_string(x = x_var, y = y_var, color = "HQ")) +
geom_point(alpha = 0.5) +
scale_color_viridis_c() + # Color scale for HQ
labs(title = paste("HQ vs.", x_var, "and", y_var),
x = x_var, y = y_var, color = "HQ Value") +
theme_minimal()
}
# List of all parameter pairs to visualize
pairs_to_plot <- list(
c("kFaktor", "kGang"),
c("kFaktor", "Vo20"),
c("kGang", "Vo20")
)
# Generate plots for each pair and display
for (pair in pairs_to_plot) {
print(plot_2d(samples_df, pair[1], pair[2]))
}
📦 Use R to optimize kFaktor
library(reticulate)
library(ParBayesianOptimization)
use_python(Sys.which("python")) # or use_virtualenv(), use_condaenv() if needed
py_run_string("") # Initialize Python access
# Define objective function for calibration
obj_fun <- function(kFaktor) {
result <- py$koella_verfahren(
x = 20,
LGe_km = 50,
Vo20 = 35,
Gletscherflaeche_km2 = 1.2,
rs = 4,
schneeschmelze = TRUE,
kGang = 1.0,
kFaktor = kFaktor
)
# Minimize the squared error to target HQ
score <- (result$HQ - target_HQ)^2
return(list(Score = score))
}
# Run Bayesian Optimization
opt_results <- bayesOpt(
FUN = obj_fun,
bounds = list(kFaktor = c(0.1, 3.0)),
initPoints = 5,
iters.n = 15,
acq = "ucb"
)
opt_results$scoreSummary
opt_results$scoreSummary