diff --git a/core/src/main/java/smile/regression/OLS.java b/core/src/main/java/smile/regression/OLS.java
index <HASH>..<HASH> 100644
--- a/core/src/main/java/smile/regression/OLS.java
+++ b/core/src/main/java/smile/regression/OLS.java
@@ -177,6 +177,10 @@ public class OLS implements Regression<double[]> {
 
         int n = x.length;
         p = x[0].length;
+        
+        if (n <= p) {
+            throw new IllegalArgumentException(String.format("The input matrix is not over determined: %d rows, %d columns", n, p));
+        }
 
         // weights and intercept
         double[] w1 = new double[p+1];
diff --git a/core/src/main/java/smile/regression/RidgeRegression.java b/core/src/main/java/smile/regression/RidgeRegression.java
index <HASH>..<HASH> 100644
--- a/core/src/main/java/smile/regression/RidgeRegression.java
+++ b/core/src/main/java/smile/regression/RidgeRegression.java
@@ -189,6 +189,11 @@ public class RidgeRegression implements Regression<double[]> {
 
         int n = x.length;
         p = x[0].length;
+
+        if (n <= p) {
+            throw new IllegalArgumentException(String.format("The input matrix is not over determined: %d rows, %d columns", n, p));
+        }
+
         ym = Math.mean(y);                
         center = Math.colMean(x);
add early detection of input matrix size for linear and ridge regression