|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.mllib.stat |
| 19 | + |
| 20 | +import breeze.linalg.{DenseVector => BDV} |
| 21 | + |
| 22 | +import org.apache.spark.annotation.DeveloperApi |
| 23 | +import org.apache.spark.mllib.linalg.{Vectors, Vector} |
| 24 | + |
| 25 | +/** |
| 26 | + * :: DeveloperApi :: |
| 27 | + * MultivariateOnlineSummarizer implements [[MultivariateStatisticalSummary]] to compute the mean, |
| 28 | + * variance, minimum, maximum, counts, and nonzero counts for samples in sparse or dense vector |
| 29 | + * format in a online fashion. |
| 30 | + * |
| 31 | + * Two MultivariateOnlineSummarizer can be merged together to have a statistical summary of |
| 32 | + * the corresponding joint dataset. |
| 33 | + * |
| 34 | + * A numerically stable algorithm is implemented to compute sample mean and variance: |
| 35 | + * Reference: [[http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance variance-wiki]] |
| 36 | + * Zero elements (including explicit zero values) are skipped when calling add(), |
| 37 | + * to have time complexity O(nnz) instead of O(n) for each column. |
| 38 | + */ |
| 39 | +@DeveloperApi |
| 40 | +class MultivariateOnlineSummarizer extends MultivariateStatisticalSummary with Serializable { |
| 41 | + |
| 42 | + private var n = 0 |
| 43 | + private var currMean: BDV[Double] = _ |
| 44 | + private var currM2n: BDV[Double] = _ |
| 45 | + private var totalCnt: Long = 0 |
| 46 | + private var nnz: BDV[Double] = _ |
| 47 | + private var currMax: BDV[Double] = _ |
| 48 | + private var currMin: BDV[Double] = _ |
| 49 | + |
| 50 | + /** |
| 51 | + * Add a new sample to this summarizer, and update the statistical summary. |
| 52 | + * |
| 53 | + * @param sample The sample in dense/sparse vector format to be added into this summarizer. |
| 54 | + * @return This MultivariateOnlineSummarizer object. |
| 55 | + */ |
| 56 | + def add(sample: Vector): this.type = { |
| 57 | + if (n == 0) { |
| 58 | + require(sample.toBreeze.length > 0, s"Vector should have dimension larger than zero.") |
| 59 | + n = sample.toBreeze.length |
| 60 | + |
| 61 | + currMean = BDV.zeros[Double](n) |
| 62 | + currM2n = BDV.zeros[Double](n) |
| 63 | + nnz = BDV.zeros[Double](n) |
| 64 | + currMax = BDV.fill(n)(Double.MinValue) |
| 65 | + currMin = BDV.fill(n)(Double.MaxValue) |
| 66 | + } |
| 67 | + |
| 68 | + require(n == sample.toBreeze.length, s"Dimensions mismatch when adding new sample." + |
| 69 | + s" Expecting $n but got ${sample.toBreeze.length}.") |
| 70 | + |
| 71 | + sample.toBreeze.activeIterator.foreach { |
| 72 | + case (_, 0.0) => // Skip explicit zero elements. |
| 73 | + case (i, value) => |
| 74 | + if (currMax(i) < value) { |
| 75 | + currMax(i) = value |
| 76 | + } |
| 77 | + if (currMin(i) > value) { |
| 78 | + currMin(i) = value |
| 79 | + } |
| 80 | + |
| 81 | + val tmpPrevMean = currMean(i) |
| 82 | + currMean(i) = (currMean(i) * nnz(i) + value) / (nnz(i) + 1.0) |
| 83 | + currM2n(i) += (value - currMean(i)) * (value - tmpPrevMean) |
| 84 | + |
| 85 | + nnz(i) += 1.0 |
| 86 | + } |
| 87 | + |
| 88 | + totalCnt += 1 |
| 89 | + this |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Merge another MultivariateOnlineSummarizer, and update the statistical summary. |
| 94 | + * (Note that it's in place merging; as a result, `this` object will be modified.) |
| 95 | + * |
| 96 | + * @param other The other MultivariateOnlineSummarizer to be merged. |
| 97 | + * @return This MultivariateOnlineSummarizer object. |
| 98 | + */ |
| 99 | + def merge(other: MultivariateOnlineSummarizer): this.type = { |
| 100 | + if (this.totalCnt != 0 && other.totalCnt != 0) { |
| 101 | + require(n == other.n, s"Dimensions mismatch when merging with another summarizer. " + |
| 102 | + s"Expecting $n but got ${other.n}.") |
| 103 | + totalCnt += other.totalCnt |
| 104 | + val deltaMean: BDV[Double] = currMean - other.currMean |
| 105 | + var i = 0 |
| 106 | + while (i < n) { |
| 107 | + // merge mean together |
| 108 | + if (other.currMean(i) != 0.0) { |
| 109 | + currMean(i) = (currMean(i) * nnz(i) + other.currMean(i) * other.nnz(i)) / |
| 110 | + (nnz(i) + other.nnz(i)) |
| 111 | + } |
| 112 | + // merge m2n together |
| 113 | + if (nnz(i) + other.nnz(i) != 0.0) { |
| 114 | + currM2n(i) += other.currM2n(i) + deltaMean(i) * deltaMean(i) * nnz(i) * other.nnz(i) / |
| 115 | + (nnz(i) + other.nnz(i)) |
| 116 | + } |
| 117 | + if (currMax(i) < other.currMax(i)) { |
| 118 | + currMax(i) = other.currMax(i) |
| 119 | + } |
| 120 | + if (currMin(i) > other.currMin(i)) { |
| 121 | + currMin(i) = other.currMin(i) |
| 122 | + } |
| 123 | + i += 1 |
| 124 | + } |
| 125 | + nnz += other.nnz |
| 126 | + } else if (totalCnt == 0 && other.totalCnt != 0) { |
| 127 | + this.n = other.n |
| 128 | + this.currMean = other.currMean.copy |
| 129 | + this.currM2n = other.currM2n.copy |
| 130 | + this.totalCnt = other.totalCnt |
| 131 | + this.nnz = other.nnz.copy |
| 132 | + this.currMax = other.currMax.copy |
| 133 | + this.currMin = other.currMin.copy |
| 134 | + } |
| 135 | + this |
| 136 | + } |
| 137 | + |
| 138 | + override def mean: Vector = { |
| 139 | + require(totalCnt > 0, s"Nothing has been added to this summarizer.") |
| 140 | + |
| 141 | + val realMean = BDV.zeros[Double](n) |
| 142 | + var i = 0 |
| 143 | + while (i < n) { |
| 144 | + realMean(i) = currMean(i) * (nnz(i) / totalCnt) |
| 145 | + i += 1 |
| 146 | + } |
| 147 | + Vectors.fromBreeze(realMean) |
| 148 | + } |
| 149 | + |
| 150 | + override def variance: Vector = { |
| 151 | + require(totalCnt > 0, s"Nothing has been added to this summarizer.") |
| 152 | + |
| 153 | + val realVariance = BDV.zeros[Double](n) |
| 154 | + |
| 155 | + val denominator = totalCnt - 1.0 |
| 156 | + |
| 157 | + // Sample variance is computed, if the denominator is less than 0, the variance is just 0. |
| 158 | + if (denominator > 0.0) { |
| 159 | + val deltaMean = currMean |
| 160 | + var i = 0 |
| 161 | + while (i < currM2n.size) { |
| 162 | + realVariance(i) = |
| 163 | + currM2n(i) + deltaMean(i) * deltaMean(i) * nnz(i) * (totalCnt - nnz(i)) / totalCnt |
| 164 | + realVariance(i) /= denominator |
| 165 | + i += 1 |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + Vectors.fromBreeze(realVariance) |
| 170 | + } |
| 171 | + |
| 172 | + override def count: Long = totalCnt |
| 173 | + |
| 174 | + override def numNonzeros: Vector = { |
| 175 | + require(totalCnt > 0, s"Nothing has been added to this summarizer.") |
| 176 | + |
| 177 | + Vectors.fromBreeze(nnz) |
| 178 | + } |
| 179 | + |
| 180 | + override def max: Vector = { |
| 181 | + require(totalCnt > 0, s"Nothing has been added to this summarizer.") |
| 182 | + |
| 183 | + var i = 0 |
| 184 | + while (i < n) { |
| 185 | + if ((nnz(i) < totalCnt) && (currMax(i) < 0.0)) currMax(i) = 0.0 |
| 186 | + i += 1 |
| 187 | + } |
| 188 | + Vectors.fromBreeze(currMax) |
| 189 | + } |
| 190 | + |
| 191 | + override def min: Vector = { |
| 192 | + require(totalCnt > 0, s"Nothing has been added to this summarizer.") |
| 193 | + |
| 194 | + var i = 0 |
| 195 | + while (i < n) { |
| 196 | + if ((nnz(i) < totalCnt) && (currMin(i) > 0.0)) currMin(i) = 0.0 |
| 197 | + i += 1 |
| 198 | + } |
| 199 | + Vectors.fromBreeze(currMin) |
| 200 | + } |
| 201 | +} |
0 commit comments