Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' );
var isVectorLike = require( '@stdlib/assert/is-vector-like' );
var Float64Array = require( '@stdlib/array/float64' );
var ctor = require( '@stdlib/ndarray/ctor' );
var bctor = require( '@stdlib/ndarray/base/ctor' );
var numel = require( '@stdlib/ndarray/base/numel' );
var format = require( '@stdlib/string/format' );
// FUNCTIONS //
/**
* Returns a matrix.
*
* @private
* @param {PositiveInteger} n - matrix order
* @param {boolean} bool - boolean indicating whether to create a low-level ndarray
* @returns {ndarray} matrix
*/
function createMatrix( n, bool ) {
var strides;
var buffer;
var shape;
var f;
if ( bool ) {
f = bctor;
} else {
f = ctor;
}
buffer = new Float64Array( n*n );
shape = [ n, n ];
strides = [ n, 1 ];
return f( 'float64', buffer, shape, strides, 0, 'row-major' );
}
/**
* Returns a vector.
*
* @private
* @param {PositiveInteger} N - number of elements
* @returns {ndarray} vector
*/
function createVector( N ) {
var strides;
var buffer;
var shape;
buffer = new Float64Array( N );
shape = [ N ];
strides = [ 1 ];
return bctor( 'float64', buffer, shape, strides, 0, 'row-major' );
}
// MAIN //
/**
* Returns an accumulator function which incrementally computes an unbiased sample covariance matrix.
*
* ## Method
*
* - For each unbiased sample covariance, we begin by defining the co-moment \\(C_{jn}\\)
*
* ```tex
* C_n = \sum_{i=1}^{n} ( x_i - \bar{x}_n ) ( y_i - \bar{y}_n )
* ```
*
* where \\(\bar{x}_n\\) and \\(\bar{y}_n\\) are the sample means for \\(x\\) and \\(y\\), respectively.
*
* - Based on Welford's method, we know the update formulas for the sample means are given by
*
* ```tex
* \bar{x}_n = \bar{x}_{n-1} + \frac{x_n - \bar{x}_{n-1}}{n}
* ```
*
* and
*
* ```tex
* \bar{y}_n = \bar{y}_{n-1} + \frac{y_n - \bar{y}_{n-1}}{n}
* ```
*
* - Substituting into the equation for \\(C_n\\) and rearranging terms
*
* ```tex
* C_n = C_{n-1} + (x_n - \bar{x}_n) (y_n - \bar{y}_{n-1})
* ```
*
* where the apparent asymmetry arises from
*
* ```tex
* x_n - \bar{x}_n = \frac{n-1}{n} (x_n - \bar{x}_{n-1})
* ```
*
* and, hence, the update term can be equivalently expressed
*
* ```tex
* \frac{n-1}{n} (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})
* ```
*
* - The covariance can be defined
*
* ```tex
* \begin{align*}
* \operatorname{cov}_n(x,y) &= \frac{C_n}{n} \\
* &= \frac{C_{n-1} + \frac{n-1}{n} (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})}{n} \\
* &= \frac{(n-1)\operatorname{cov}_{n-1}(x,y) + \frac{n-1}{n} (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})}{n}
* \end{align*}
* ```
*
* - Applying Bessel's correction, we arrive at an update formula for calculating an unbiased sample covariance
*
* ```tex
* \begin{align*}
* \operatorname{cov}_n(x,y) &= \frac{n}{n-1}\cdot\frac{(n-1)\operatorname{cov}_{n-1}(x,y) + \frac{n-1}{n} (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})}{n} \\
* &= \operatorname{cov}_{n-1}(x,y) + \frac{(x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})}{n} \\
* &= \frac{C_{n-1}}{n-1} + \frac{(x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})}{n}
* &= \frac{C_{n-1} + \frac{n-1}{n} (x_n - \bar{x}_{n-1}) (y_n - \bar{y}_{n-1})}{n-1}
* \end{align*}
* ```
*
* @param {(PositiveInteger|ndarray)} out - order of the covariance matrix or a square 2-dimensional output ndarray for storing the covariance matrix
* @param {ndarray} [means] - mean values
* @throws {TypeError} first argument must be either a positive integer or a 2-dimensional ndarray having equal dimensions
* @throws {TypeError} second argument must be a 1-dimensional ndarray
* @throws {Error} number of means must match covariance matrix dimensions
* @returns {Function} accumulator function
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
*
* // Create an output covariance matrix:
* var buffer = new Float64Array( 4 );
* var shape = [ 2, 2 ];
* var strides = [ 2, 1 ];
* var offset = 0;
* var order = 'row-major';
*
* var cov = ndarray( 'float64', buffer, shape, strides, offset, order );
*
* // Create a covariance matrix accumulator:
* var accumulator = incrcovmat( cov );
*
* var out = accumulator();
* // returns null
*
* // Create a data vector:
* buffer = new Float64Array( 2 );
* shape = [ 2 ];
* strides = [ 1 ];
*
* var vec = ndarray( 'float64', buffer, shape, strides, offset, order );
*
* // Provide data to the accumulator:
* vec.set( 0, 2.0 );
* vec.set( 1, 1.0 );
*
* out = accumulator( vec );
* // returns <ndarray>
*
* var bool = ( out === cov );
* // returns true
*
* vec.set( 0, -5.0 );
* vec.set( 1, 3.14 );
*
* out = accumulator( vec );
* // returns <ndarray>
*
* // Retrieve the covariance matrix:
* out = accumulator();
* // returns <ndarray>
*/
function incrcovmat( out, means ) {
var order;
var cov;
var mu;
var C;
var d;
var N;
N = 0;
if ( isPositiveInteger( out ) ) {
order = out;
cov = createMatrix( order, false );
} else if ( isSquareMatrix( out ) ) {
order = out.shape[ 0 ];
cov = out;
} else {
throw new TypeError( format( 'invalid argument. First argument must either specify the order of the covariance matrix or be a square two-dimensional ndarray for storing the covariance matrix. Value: `%s`.', out ) );
}
// Create a scratch array for storing residuals (i.e., `x_i - xbar_{i-1}`):
d = new Float64Array( order );
// Create a low-level scratch matrix for storing co-moments:
C = createMatrix( order, true );
if ( arguments.length > 1 ) {
if ( !isVectorLike( means ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a one-dimensional ndarray. Value: `%s`.', means ) );
}
if ( numel( means.shape ) !== order ) {
throw new Error( format( 'invalid argument. The number of elements (means) in the second argument must match covariance matrix dimensions. Expected: `%u`. Actual: `%u`.', order, numel( means.shape ) ) );
}
mu = means; // TODO: should we copy this? Otherwise, internal state could be "corrupted" due to mutation outside the accumulator
return accumulator2;
}
// Create an ndarray vector for storing sample means (note: an ndarray interface is not necessary, but it reduces implementation complexity by ensuring a consistent abstraction for accessing and updating sample means):
mu = createVector( order );
return accumulator1;
/**
* If provided a data vector, the accumulator function returns an updated unbiased sample covariance matrix. If not provided a data vector, the accumulator function returns the current unbiased sample covariance matrix.
*
* @private
* @param {ndarray} [v] - data vector
* @throws {TypeError} must provide a 1-dimensional ndarray
* @throws {Error} vector length must match covariance matrix dimensions
* @returns {(ndarray|null)} unbiased sample covariance matrix or null
*/
function accumulator1( v ) {
var covij;
var denom;
var rdx;
var cij;
var m;
var n;
var r;
var i;
var j;
if ( arguments.length === 0 ) {
if ( N === 0 ) {
return null;
}
return cov;
}
if ( !isVectorLike( v ) ) {
throw new TypeError( format( 'invalid argument. Must provide a one-dimensional ndarray. Value: `%s`.', v ) );
}
if ( v.shape[ 0 ] !== order ) {
throw new Error( format( 'invalid argument. Vector length must match covariance matrix dimensions. Expected: `%u`. Actual: `%u`.', order, v.shape[ 0 ] ) );
}
n = N;
N += 1;
r = n / N;
denom = n || 1; // Bessel's correction (avoiding divide-by-zero below)
for ( i = 0; i < order; i++ ) {
m = mu.get( i );
// Compute the residual:
d[ i ] = v.get( i ) - m;
// Update the sample mean:
m += d[ i ] / N;
mu.set( i, m );
// Update the co-moments and covariance matrix, recognizing that the covariance matrix is symmetric...
rdx = r * d[ i ]; // if `n=0`, `r=0.0`
for ( j = 0; j <= i; j++ ) {
cij = C.get( i, j ) + ( rdx*d[j] );
C.set( i, j, cij );
C.set( j, i, cij ); // via symmetry
covij = cij / denom;
cov.set( i, j, covij );
cov.set( j, i, covij ); // via symmetry
}
}
return cov;
}
/**
* If provided a data vector, the accumulator function returns an updated unbiased sample covariance matrix. If not provided a data vector, the accumulator function returns the current unbiased sample covariance matrix.
*
* @private
* @param {ndarray} [v] - data vector
* @throws {TypeError} must provide a 1-dimensional ndarray
* @throws {Error} vector length must match covariance matrix dimensions
* @returns {(ndarray|null)} unbiased sample covariance matrix or null
*/
function accumulator2( v ) {
var covij;
var cij;
var di;
var i;
var j;
if ( arguments.length === 0 ) {
if ( N === 0 ) {
return null;
}
return cov;
}
if ( !isVectorLike( v ) ) {
throw new TypeError( format( 'invalid argument. Must provide a one-dimensional ndarray. Value: `%s`.', v ) );
}
if ( v.shape[ 0 ] !== order ) {
throw new Error( format( 'invalid argument. Vector length must match covariance matrix dimensions. Expected: `%u`. Actual: `%u`.', order, v.shape[ 0 ] ) );
}
N += 1;
for ( i = 0; i < order; i++ ) {
// Compute the residual:
d[ i ] = v.get( i ) - mu.get( i );
// Update the co-moments and covariance matrix, recognizing that the covariance matrix is symmetric...
di = d[ i ];
for ( j = 0; j <= i; j++ ) {
cij = C.get( i, j ) + ( di*d[j] );
C.set( i, j, cij );
C.set( j, i, cij ); // via symmetry
covij = cij / N;
cov.set( i, j, covij );
cov.set( j, i, covij ); // via symmetry
}
}
return cov;
}
}
// EXPORTS //
module.exports = incrcovmat;
|