Turn values non-negative while preserving their sample mean
Source:R/to_non_negative_with_identical_mean.R
to_non_negative_with_identical_mean.Rd
Postprocess function turning forecast samples non-negative while keeping the mean at the same value as it was before through scaling of the samples (unless the sample mean was originally negative).
Details
While the identical sample mean can be enforced, this will generally decrease the sample variance as this is the only way to maintain the mean while turning negative samples to zero to enforce the non-negativity constraint.
The returned samples will always be non-negative. The sample mean will not be identical if it cannot be achieved given the non-negative samples.
Examples
to_non_negative_with_identical_mean(x = c(-1, 0, 1, 2))
#> [1] 0.0000000 0.0000000 0.6666667 1.3333333
x <- rnorm(100, 0.5)
summary(x)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -1.8913 -0.1308 0.5679 0.4928 1.1706 3.0221
summary(to_non_negative_with_identical_mean(x))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.0000 0.0000 0.3899 0.4928 0.8036 2.0745
x <- rnorm(100, -1)
summary(x)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -4.0423 -1.8373 -1.1560 -1.1432 -0.3465 0.9701
# can't keep identical mean as original mean is negative
summary(to_non_negative_with_identical_mean(x))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00000 0.00000 0.00000 0.06034 0.00000 0.97015