public static PrimitiveMatrix toVolatilities(Access2D<?> covariances, boolean clean) {

        int size = Math.toIntExact(Math.min(covariances.countRows(), covariances.countColumns()));

        PrimitiveMatrix.DenseReceiver retVal = PrimitiveMatrix.FACTORY.makeDense(size);

        if (clean) {

            MatrixStore<Double> covarianceMtrx = MatrixStore.PRIMITIVE.makeWrapper(covariances).get();

            double largest = covarianceMtrx.aggregateDiagonal(Aggregator.LARGEST);
            double limit = largest * size * PrimitiveMath.RELATIVELY_SMALL;
            double smallest = PrimitiveMath.SQRT.invoke(limit);

            for (int ij = 0; ij < size; ij++) {
                double variance = covariances.doubleValue(ij, ij);

                if (variance < limit) {
                    retVal.set(ij, smallest);
                } else {
                    retVal.set(ij, PrimitiveMath.SQRT.invoke(variance));
                }
            }

        } else {

            for (int ij = 0; ij < size; ij++) {
                double variance = covariances.doubleValue(ij, ij);

                if (variance <= PrimitiveMath.ZERO) {
                    retVal.set(ij, PrimitiveMath.ZERO);
                } else {
                    retVal.set(ij, PrimitiveMath.SQRT.invoke(variance));
                }
            }
        }

        return retVal.get();
    }
public static PrimitiveMatrix toVolatilities ( Access2D < ? > covariances , boolean clean ) { int size = Math . toIntExact ( Math . min ( covariances . countRows ( ) , covariances . countColumns ( ) ) ) ; PrimitiveMatrix . DenseReceiver retVal = PrimitiveMatrix . FACTORY . makeDense ( size ) ; if ( clean ) { MatrixStore < Double > covarianceMtrx = MatrixStore . PRIMITIVE . makeWrapper ( covariances ) . get ( ) ; double largest = covarianceMtrx . aggregateDiagonal ( Aggregator . LARGEST ) ; double limit = largest * size * PrimitiveMath . RELATIVELY_SMALL ; double smallest = PrimitiveMath . SQRT . invoke ( limit ) ; for ( int ij = 0 ; ij < size ; ij ++ ) { double variance = covariances . doubleValue ( ij , ij ) ; if ( variance < limit ) { retVal . set ( ij , smallest ) ; } else { retVal . set ( ij , PrimitiveMath . SQRT . invoke ( variance ) ) ; } } } else { for ( int ij = 0 ; ij < size ; ij ++ ) { double variance = covariances . doubleValue ( ij , ij ) ; if ( variance <= PrimitiveMath . ZERO ) { retVal . set ( ij , PrimitiveMath . ZERO ) ; } else { retVal . set ( ij , PrimitiveMath . SQRT . invoke ( variance ) ) ; } } } return retVal . get ( ) ; }
Will extract the standard deviations (volatilities) from the input covariance matrix. If "cleaning" is
enabled small variances will be replaced with a new minimal value.