public static double[] performRegression(Matrix x,Matrix y,double[] weights,double ridge){
  Matrix weightedIndependent=new Matrix(x.getRowDimension(),x.getColumnDimension());
  Matrix weightedDependent=new Matrix(x.getRowDimension(),1);
  for (int i=0; i < weights.length; i++) {
    double sqrtWeight=Math.sqrt(weights[i]);
    for (int j=0; j < x.getColumnDimension(); j++) {
      weightedIndependent.set(i,j,x.get(i,j) * sqrtWeight);
    }
    weightedDependent.set(i,0,y.get(i,0) * sqrtWeight);
  }
  return performRegression(weightedIndependent,weightedDependent,ridge);
}

public static double [ ] performRegression ( Matrix x , Matrix y , double [ ] weights , double ridge ) { Matrix weightedIndependent = new Matrix ( x . getRowDimension ( ) , x . getColumnDimension ( ) ) ; Matrix weightedDependent = new Matrix ( x . getRowDimension ( ) , 1 ) ; for ( int i = 0 ; i < weights . length ; i ++ ) { double sqrtWeight = Math . sqrt ( weights [ i ] ) ; for ( int j = 0 ; j < x . getColumnDimension ( ) ; j ++ ) { weightedIndependent . set ( i , j , x . get ( i , j ) * sqrtWeight ) ; } weightedDependent . set ( i , 0 , y . get ( i , 0 ) * sqrtWeight ) ; } return performRegression ( weightedIndependent , weightedDependent , ridge ) ; }
performs a weighted linear ridge regression .
