computeRainfall: Compute the amount of precipitation given the parameters of a...

View source: R/computeRainfall.R

computeRainfallR Documentation

Compute the amount of precipitation given the parameters of a Gamma distribution.

Description

Compute the amount of precipitation given the parameters of a Gamma distribution as returned by downscalePredict.keras on a trained model (see downscaleTrain.keras) that optimized the loss function bernouilliGammaLoss.

Usage

computeRainfall(log_alpha, log_beta, simulate = FALSE, bias = NULL)

Arguments

log_alpha

A grid. The log of the shape parameter ("log_alpha") as returned by downscalePredict.keras when we use the bernouilliGamma loss function.

log_beta

A grid. The log of the scale parameter ("log_beta") as returned by downscalePredict.keras when we use the bernouilliGamma loss function.

simulate

A logical value. If TRUE then the output is an stochastic sample for the given distribution parameters (p,alpha and beta).

bias

An integer. Added to the quantity of rain after its computation, either deterministically or stochastically. It basically displaces the distribution. Default is NULL.

Value

A climate4R grid with the deterministic or stochastic temporal serie depending on whether the simulate parameter is FALSE or TRUE.

Author(s)

J. Bano-Medina

See Also

bernouilliGammaLoss a custom loss function for Bernouilli-Gamma distributions gaussianLoss a custom loss function for gaussian distributions downscaleTrain.keras for training a downscaling deep model with keras downscalePredict.keras for predicting with a keras model prepareNewData.keras for predictor preparation with new (test) data

Examples


require(climate4R.datasets)
require(transformeR)
data("NCEP_Iberia_hus850", "NCEP_Iberia_psl", "NCEP_Iberia_ta850")
x <- makeMultiGrid(NCEP_Iberia_hus850, NCEP_Iberia_psl, NCEP_Iberia_ta850)
data("VALUE_Iberia_pr")
y <- VALUE_Iberia_pr
inputs <- layer_input(shape = c(getShape(x,"lat"),getShape(x,"lon"),getShape(x,"var")))
hidden <- inputs %>% 
  layer_conv_2d(filters = 25, kernel_size = c(3,3), activation = 'relu') %>%  
  layer_conv_2d(filters = 10, kernel_size = c(3,3), activation = 'relu') %>% 
  layer_flatten() %>% 
  layer_dense(units = 10, activation = "relu")
outputs1 <- layer_dense(hidden,units = getShape(y,"loc"), activation = "sigmoid")
outputs2 <- layer_dense(hidden,units = getShape(y,"loc"))
outputs3 <- layer_dense(hidden,units = getShape(y,"loc"))
outputs <- layer_concatenate(list(outputs1,outputs2,outputs3))
model <- keras_model(inputs = inputs, outputs = outputs)
y <- gridArithmetics(y,0.99,operator = "-") %>% binaryGrid("GT",0,partial = TRUE) 
pred <- downscaleCV.keras(x, y, model,
             sampling.strategy = "kfold.chronological", folds = 4, 
             scaleGrid.args = list(type = "standardize"),
             prepareData.keras.args = list(first.connection = "conv",
                                           last.connection = "dense",
                                           channels = "last"),
             compile.args = list(loss = bernouilliGammaLoss(last.connection = "dense"), 
                                 optimizer = optimizer_adam()),
             fit.args = list(batch_size = 100, epochs = 10, validation_split = 0.1),
             loss = "bernouilliGammaLoss",
             binarySerie = TRUE, condition = "GE", threshold = 1)
# Deterministic
pred_amo <- computeRainfall(log_alpha = subsetGrid(pred,var = "log_alpha"),
                            log_beta = subsetGrid(pred,var = "log_beta"),
                            bias = 0.99)
# Stochastic
pred_amo <- computeRainfall(log_alpha = subsetGrid(pred,var = "log_alpha"),
                            log_beta = subsetGrid(pred,var = "log_beta"),
                            simulate = TRUE,
                            bias = 0.99)
                             

SantanderMetGroup/downscaleR.keras documentation built on July 7, 2023, 1:22 p.m.