1 /*
2 * The baseCode project
3 *
4 * Copyright (c) 2006 University of British Columbia
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 package ubic.basecode.math;
20
21 import cern.colt.matrix.DoubleMatrix1D;
22 import cern.colt.matrix.DoubleMatrix2D;
23 import cern.colt.matrix.impl.DenseDoubleMatrix1D;
24 import ubic.basecode.dataStructure.matrix.DoubleMatrix;
25 import cern.colt.list.DoubleArrayList;
26
27 /**
28 * Convenience functions for getting row statistics from matrices.
29 *
30 * @author Paul Pavlidis
31 * @todo Have min() and max() throw an EmptyMatrixException -- this exception class does not yet exist and needs to be
32 * defined somewhere.
33 */
34 public class MatrixRowStats {
35
36 /**
37 * Calculates the means of a matrix's rows.
38 *
39 * @param M DoubleMatrixNamed
40 * @return DoubleArrayList
41 */
42 public static <R, C> DoubleArrayList means(DoubleMatrix<R, C> M) {
43 DoubleArrayList r = new DoubleArrayList();
44 for (int i = 0; i < M.rows(); i++) {
45 r.add(DescriptiveWithMissing.mean(new DoubleArrayList(M.getRow(i))));
46 }
47 return r;
48 }
49
50
51 /**
52 * Calculate the means of a matrix's rows
53 *
54 * @param e matrix. Missing values are ignored.
55 * @return vector of length equal to number of e's rows
56 */
57 public static DoubleMatrix1D means(DoubleMatrix2D e) {
58 DoubleMatrix1D result = new DenseDoubleMatrix1D(e.rows());
59 for (int i = 0; i < e.rows(); i++) {
60 double mean = 0.0;
61 int denominator = 0;
62 for (int j = 0; j < e.columns(); j++) {
63 double v = e.getQuick(i, j);
64 if (!Double.isNaN(v)) {
65 mean += v;
66 denominator++;
67 }
68 }
69 result.set(i, mean / (double) denominator);
70 }
71 return result;
72 }
73
74 /**
75 * Calculates the sample standard deviation of each row of a matrix
76 *
77 * @param M DoubleMatrixNamed
78 * @return DoubleArrayList
79 */
80 public static <R, C> DoubleArrayList sampleStandardDeviations(DoubleMatrix<R, C> M) {
81 DoubleArrayList r = new DoubleArrayList();
82 for (int i = 0; i < M.rows(); i++) {
83 DoubleArrayList row = new DoubleArrayList(M.getRow(i));
84 double mean = DescriptiveWithMissing.mean(row);
85 r.add(Math.sqrt(DescriptiveWithMissing.sampleVariance(row, mean)));
86 }
87 return r;
88 }
89
90 /**
91 * Calculates the sum of squares for each row of a matrix
92 *
93 * @param M DoubleMatrixNamed
94 * @return DoubleArrayList
95 */
96 public static <R, C> DoubleArrayList sumOfSquares(DoubleMatrix<R, C> M) {
97 DoubleArrayList r = new DoubleArrayList();
98
99 for (int i = 0; i < M.rows(); i++) {
100 DoubleArrayList row = new DoubleArrayList(M.getRow(i));
101 r.add(DescriptiveWithMissing.sumOfSquares(row));
102 }
103
104 return r;
105 }
106
107 /**
108 * Calculate the sums of a matrix's rows.
109 *
110 * @param M DoubleMatrixNamed
111 * @return DoubleArrayList
112 */
113 public static <R, C> DoubleArrayList sums(DoubleMatrix<R, C> M) {
114 DoubleArrayList r = new DoubleArrayList();
115 for (int i = 0; i < M.rows(); i++) {
116 r.add(DescriptiveWithMissing.sum(new DoubleArrayList(M.getRow(i))));
117 }
118 return r;
119 }
120
121 private MatrixRowStats() { /* keep us from instantiating this */
122 }
123
124 }