Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in IterationControll for algo Cue and IT #1

Open
cedricjuessen opened this issue Dec 16, 2022 · 2 comments
Open

Error in IterationControll for algo Cue and IT #1

cedricjuessen opened this issue Dec 16, 2022 · 2 comments

Comments

@cedricjuessen
Copy link

Dear Colleagues,
for "Due" and "IT" in the IterationControl only the first value from the theta vector is compared with the value from the previous iteration. Therefore, the following warning is generated for the following sample code:

Code:
StableEstim::Estim_Simulation(rbind(c(0.5,1)),c(4), 4, "GMM", HandleError = TRUE, FctsToApply = StatFcts, saveOutput = TRUE, StatSummary = FALSE, CheckMat = TRUE, tolFailCheck = tolFailure, SeedOptions=NULL,algo = "CueGMM", regularization = "Tikhonov", WeightingMatrix = "OptAsym", t_scheme = "free", alphaReg = 0.005, t_free = seq(0.1,2,length.out=12))

Warning:
Warning messages: 1: In (iter < Control$NbIter) && (RelativeErr > Control$RelativeErrMax) : 'length(x) = 4 > 1' in coercion to 'logical(1)'

Since the functions are all similar in structure, I will explain the error on one example. Let's have a look at the function "ComputeITGMMParametersEstim" in the file "GMMParamsEstim.R".
In line 78 while((iter < Control$NbIter) && (RelativeErr > Control$RelativeErrMax) ) RelativeErr is compared to a numeric value. Before, the value of RelativeErr is also set to a numeric value: RelativeErr=Control$RelativeErrMax+5 . In the first iteration, the When condition works fine while two numeric values are compared. No warning is generated.

However, during the first iteration RelativeErr is assinged a vector: RelativeErr <- abs(CurrentEstimParVal-PrevEstimParVal).

To fix the error, I would suggest the following solution:
Set RelativeErr directly as a vector with length 4:
RelativeErr <- rep(Control$RelativeErrMax + 5, times = 4)

Compare for each value from RelativeErr that it satisfies the iteration condition:
while ((iter < Control$NbIter) && ((RelativeErr[1] > RelativeErrMaxArray) || (RelativeErr[2] > RelativeErrMaxArray) || (RelativeErr[3] > RelativeErrMaxArray) || (RelativeErr[4] > RelativeErrMaxArray) )) {
Yours faithfully,
Cedric Jüssen

@GeoBosh
Copy link
Owner

GeoBosh commented Oct 24, 2024

Sorry, I missed that. Many thanks for the report.

Your example doesn't demonstrate the problem but the following modification from the examples for GMMParametersEstim does:

theta <- c(1.5, 0.5, 1, 0)
pm <- 0
set.seed(345);
x <- rstable(100, theta[1], theta[2], theta[3], theta[4], pm)
##---------------- 2S free ----------------
## method specific arguments
regularization <- "cut-off"
WeightingMatrix <- "OptAsym"
alphaReg <- 0.005
t_seq <- seq(0.1, 2, length.out = 12)

## If you are just interested by the value
## of the 4 estimated parameters
t_scheme = "free"

GMMParametersEstim(
    x = x, algo = "CueGMM", alphaReg = alphaReg, regularization = regularization, 
    WeightingMatrix = WeightingMatrix, t_scheme = t_scheme, pm = pm, 
    PrintTime = TRUE, t_free = t_seq)

> Error in (iter < Control$NbIter) && (RelativeErr > Control$RelativeErrMax) (from GMMParamsEstim.R#110) : 
> 'length = 4' in coercion to 'logical(1)'

We see that R.4.4.1 throws error (two years ago it was a warning).

Your analysis is correct. I think that it is better to just replace

RelativeErr <- abs(CurrentEstimParVal-PrevEstimParVal)

with

RelativeErr <- max(abs(CurrentEstimParVal-PrevEstimParVal))

i.e., take the max error and leave RelativeErr scalar.

GeoBosh added a commit that referenced this issue Oct 24, 2024
@GeoBosh
Copy link
Owner

GeoBosh commented Oct 24, 2024

Fixed on github; will be in StableEstim v2.4 (unfortunately, I noticed this issue after submitting a minor fix to CRAN; will need to wait aa month or so before a new release).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants