1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
29
30
31
32
33
34 public class MatrixRowStats {
35
36
37
38
39
40
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
53
54
55
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
76
77
78
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
92
93
94
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
109
110
111
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() {
122 }
123
124 }