gaturchenko commented on code in PR #2499:
URL: https://github.com/apache/systemds/pull/2499#discussion_r3756450165


##########
scripts/builtin/powerTransform.dml:
##########
@@ -0,0 +1,383 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+# Power transformation using the selected method.
+# Reduces feature skewness by estimating and applying an optimal 
transformation parameter for each column.
+#
+# INPUT:
+# 
-------------------------------------------------------------------------------------
+#   X            Input feature matrix of shape n-by-m
+#   method       Power transformation method: "yeo-johnson" (default) or 
"box-cox"
+#   standardize  Whether to normalize transformed columns to zero mean and 
unit variance

Review Comment:
   Please, remove 2 spaces after `#` such that you have 1 space in between `#` 
and the text that follows



##########
scripts/builtin/powerTransformApply.dml:
##########
@@ -0,0 +1,131 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+# Applies a fitted power transformation and optional standardization.
+# Transforms each feature using its previously estimated lambda and scaling 
parameters.
+#
+# INPUT:
+# 
-------------------------------------------------------------------------------------
+#   X        Input feature matrix of shape n-by-m
+#   lambdas  Precomputed lambda parameters of shape 1-by-m, one per column
+#   means    Transformed column means of shape 1-by-m; empty to skip 
standardization
+#   scales   Transformed column scales of shape 1-by-m; empty to skip 
standardization
+#   method   Power transformation method: "yeo-johnson" (default) or "box-cox"

Review Comment:
   Please, remove 2 spaces after `#` such that you have 1 space in between `#` 
and the text that follows



##########
scripts/builtin/powerTransform.dml:
##########
@@ -0,0 +1,383 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+# Power transformation using the selected method.
+# Reduces feature skewness by estimating and applying an optimal 
transformation parameter for each column.
+#
+# INPUT:
+# 
-------------------------------------------------------------------------------------
+#   X            Input feature matrix of shape n-by-m
+#   method       Power transformation method: "yeo-johnson" (default) or 
"box-cox"
+#   standardize  Whether to normalize transformed columns to zero mean and 
unit variance
+# 
-------------------------------------------------------------------------------------
+#
+# OUTPUT:
+# 
-------------------------------------------------------------------------------------
+#   Y        Power-transformed matrix of shape n-by-m
+#   lambdas  Estimated lambda parameters of shape 1-by-m, one per column
+#   means    Transformed column means of shape 1-by-m, or an empty matrix when 
not standardized
+#   scales   Transformed column scales of shape 1-by-m, or an empty matrix 
when not standardized

Review Comment:
   Please, remove 2 spaces after `#` such that you have 1 space in between `#` 
and the text that follows



##########
scripts/builtin/powerTransform.dml:
##########
@@ -0,0 +1,383 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+# Power transformation using the selected method.
+# Reduces feature skewness by estimating and applying an optimal 
transformation parameter for each column.
+#
+# INPUT:
+# 
-------------------------------------------------------------------------------------
+#   X            Input feature matrix of shape n-by-m
+#   method       Power transformation method: "yeo-johnson" (default) or 
"box-cox"
+#   standardize  Whether to normalize transformed columns to zero mean and 
unit variance
+# 
-------------------------------------------------------------------------------------
+#
+# OUTPUT:
+# 
-------------------------------------------------------------------------------------
+#   Y        Power-transformed matrix of shape n-by-m
+#   lambdas  Estimated lambda parameters of shape 1-by-m, one per column
+#   means    Transformed column means of shape 1-by-m, or an empty matrix when 
not standardized
+#   scales   Transformed column scales of shape 1-by-m, or an empty matrix 
when not standardized
+# 
-------------------------------------------------------------------------------------
+
+m_powerTransform = function(
+    Matrix[Double] X,
+    String method="yeo-johnson",
+    Boolean standardize=TRUE)
+  return (
+    Matrix[Double] Y,
+    Matrix[Double] lambdas,
+    Matrix[Double] means,
+    Matrix[Double] scales)
+{
+  if (method != "yeo-johnson" & method != "box-cox") {
+    stop("powerTransform: unsupported method '" + method +
+      "'; expected 'yeo-johnson' or 'box-cox'")
+  }
+
+  validatedX = replace(target=X, pattern=NaN, replacement=1.0)
+  if (method == "box-cox" & min(validatedX) <= 0.0) {
+    stop("powerTransform: Box-Cox requires strictly positive input")
+  }
+
+  m = ncol(X)
+  lambdas = matrix(1.0, rows=1, cols=m) # Initialize first, then replace each 
column with the best lambdas
+
+  # Estimate lambda for each column separately
+  for (j in 1:m){
+    x = X[,j]
+    xObserved = removeEmpty(target=x, margin="rows", select=(is.na(x) == 0))
+    observedN = nrow(xObserved)
+
+    # Yeo-Johnson leaves constant columns unchanged; Box-Cox rejects them
+    if (observedN == 0) {
+      lambdas[1,j] = 1.0
+    }
+    else if (max(xObserved) == min(xObserved)) {
+      if (method == "yeo-johnson") {
+        lambdas[1,j] = 1.0;
+      }
+      else {
+        stop("powerTransform: Box-Cox does not support constant columns")
+      }
+    }
+    else{
+      lambdas[1,j] = ptEstimateLambda(xObserved, method);
+    }
+  }
+
+  # Apply the fitted transformation before optional standardization
+  emptyStats = matrix(0.0, rows=0, cols=0)
+  Y = powerTransformApply(X, lambdas, emptyStats, emptyStats, method);
+
+  means = matrix(0.0, rows=0, cols=0)
+  scales = matrix(0.0, rows=0, cols=0)
+
+  if (standardize) {
+    means = matrix(0.0, rows=1, cols=m)
+    scales = matrix(1.0, rows=1, cols=m)
+
+    for (j in 1:m) {
+      y = Y[,j]
+      yObserved = removeEmpty(target=y, margin="rows", select=(is.na(y) == 0))
+      observedN = nrow(yObserved)
+
+      if (observedN > 0) {
+        means[1,j] = mean(yObserved)
+        scale = sqrt(sum((yObserved - means[1,j])^2) / observedN)
+        if (!is.na(scale) & !is.infinite(scale) & scale != 0.0) {
+          scales[1,j] = scale
+        }
+      }
+
+      Y[,j] = ifelse(is.na(y), NaN, (y - means[1,j]) / scales[1,j])
+    }
+  }
+}
+ptEstimateLambda = function(Matrix[Double] x, String method)
+    return (Double lambda)
+{
+    lower = -2.0;
+    upper = 2.0;
+
+    if (method == "box-cox") {
+      jacTerm = sum(log(x))
+    }
+    else {
+      jacTerm = sum(sign(x) * log(abs(x) + 1.0))
+    }
+
+    lambda = ptBrentSearch(x, lower, upper, method, jacTerm);
+}
+
+# Compute negative log likelihood; lower lambda score is better
+
+ptNegLogLikelihood = function(
+    Matrix[Double] x,
+    Double lambda,
+    String method,
+    Double jacTerm)
+  return (Double negLogLikelihood)
+{
+    eps = 1e-12
+    if (method == "box-cox") {
+      if (abs(lambda) < eps) {
+        y = log(x)
+      }
+      else {
+        y = (x^lambda - 1.0) / lambda
+      }
+    }
+    else {
+      nonnegative = x >= 0
+      xPos = ifelse(nonnegative, x, 0.0)
+      xNeg = ifelse(nonnegative, 0.0, x)
+
+      if (abs(lambda) < eps) {
+        yPos = log(xPos + 1.0)
+      }
+      else {
+        yPos = ((xPos + 1.0)^lambda - 1.0) / lambda
+      }
+
+      if (abs(lambda - 2.0) < eps) {
+        yNeg = -log(1.0 - xNeg)
+      }
+      else {
+        yNeg = -((1.0 - xNeg)^(2.0 - lambda) - 1.0) / (2.0 - lambda)
+      }
+
+      y = ifelse(nonnegative, yPos, yNeg)
+    }
+
+    n = nrow(x);
+    yMean = mean(y);
+    yVariance = sum((y - yMean)^2) / n;
+
+    if (sum(is.na(y)) > 0 | sum(is.infinite(y)) > 0 |
+        is.na(yVariance) | is.infinite(yVariance) | yVariance <= 0.0) {

Review Comment:
   Just `if (is.na(yVariance) | is.infinite(yVariance) | yVariance <= 0.0)` 
will suffice



##########
scripts/builtin/powerTransformApply.dml:
##########
@@ -0,0 +1,131 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+# Applies a fitted power transformation and optional standardization.
+# Transforms each feature using its previously estimated lambda and scaling 
parameters.
+#
+# INPUT:
+# 
-------------------------------------------------------------------------------------
+#   X        Input feature matrix of shape n-by-m
+#   lambdas  Precomputed lambda parameters of shape 1-by-m, one per column
+#   means    Transformed column means of shape 1-by-m; empty to skip 
standardization
+#   scales   Transformed column scales of shape 1-by-m; empty to skip 
standardization
+#   method   Power transformation method: "yeo-johnson" (default) or "box-cox"
+# 
-------------------------------------------------------------------------------------
+#
+# OUTPUT:
+# 
-------------------------------------------------------------------------------------
+#   Y    Power-transformed matrix of shape n-by-m

Review Comment:
   Please, remove 2 spaces after `#` such that you have 1 space in between `#` 
and the text that follows



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to