blob: 392c6b6d2450574028edc16d73129f726839b898 [file]
#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------
source("scripts/builtin/stepGLM.dml") as stepGLM;
N = 1000;
P = 10;
X = rand(rows=N, cols=P, min=-1.0, max=1.0, pdf="uniform", seed=123);
beta_true = matrix(0, rows=P, cols=1);
beta_true[2,1] = 3.5;
beta_true[5,1] = -2.0;
beta_true[8,1] = 1.5;
Z = X %*% beta_true;
P_y = 1.0 / (1.0 + exp(-Z));
Y = (rand(rows=N, cols=1, min=0.0, max=1.0, seed=456) < P_y) * 1.0;
[AIC, B, S] = stepGLM::m_stepGLM(X=X, Y=Y, link=2, yneg=0.0, icpt=0, tol=1e-6, disp=0.0, moi=200, mii=0, thr=0.01);
beta_est = matrix(0, rows=P, cols=1);
for (i in 1:nrow(B)) {
idx = as.scalar(S[1, i]);
beta_est[idx, 1] = B[i, 1];
}
# if beta_est and beta_true have the same sparsity
if (nrow(B) != 3 | sum(beta_est != 0 & beta_true == 0) > 0 | sum(beta_est == 0 & beta_true != 0) > 0) {
stop("Test failed: Unexpected non-zero element in beta_est");
}
# if maximal element divergence remains below epsilon bound
epsilon = 0.5;
if (max(abs(beta_est - beta_true)) > epsilon) {
stop("Test failed: Element divergence exceeds epsilon=" + epsilon +" tolerance.");
}