Skip to content

Commit 5278c8e

Browse files
committed
Add pkg to incrementally compute a sum of squared absolute values
1 parent 0846bbb commit 5278c8e

File tree

9 files changed

+580
-0
lines changed

9 files changed

+580
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# incrsumabs2
22+
23+
> Compute a sum of squared absolute values incrementally.
24+
25+
<section class="intro">
26+
27+
The sum of squared absolute values is defined as
28+
29+
<!-- <equation class="equation" label="eq:sum_squared_absolute_values" align="center" raw="s = \sum_{i=0}^{n-1} x_i^2" alt="Equation for the sum of squared absolute values."> -->
30+
31+
<div class="equation" align="center" data-raw-text="s = \sum_{i=0}^{n-1} x_i^2" data-equation="eq:sum_squared_absolute_values">
32+
<img src="" alt="Equation for the sum of squared absolute values.">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var incrsumabs2 = require( '@stdlib/stats/incr/sumabs2' );
48+
```
49+
50+
#### incrsumabs2()
51+
52+
Returns an accumulator `function` which incrementally computes a sum of squared absolute values.
53+
54+
```javascript
55+
var accumulator = incrsumabs2();
56+
```
57+
58+
#### accumulator( \[x] )
59+
60+
If provided an input value `x`, the accumulator function returns an updated sum. If not provided an input value `x`, the accumulator function returns the current sum.
61+
62+
```javascript
63+
var accumulator = incrsumabs2();
64+
65+
var sum = accumulator( 2.0 );
66+
// returns 4.0
67+
68+
sum = accumulator( -1.0 );
69+
// returns 5.0
70+
71+
sum = accumulator( -3.0 );
72+
// returns 14.0
73+
74+
sum = accumulator();
75+
// returns 14.0
76+
```
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<section class="notes">
83+
84+
## Notes
85+
86+
- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
87+
- For long running accumulations or accumulations of large numbers, care should be taken to prevent overflow.
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var randu = require( '@stdlib/random/base/randu' );
101+
var incrsumabs2 = require( '@stdlib/stats/incr/sumabs2' );
102+
103+
var accumulator;
104+
var v;
105+
var i;
106+
107+
// Initialize an accumulator:
108+
accumulator = incrsumabs2();
109+
110+
// For each simulated datum, update the sum...
111+
for ( i = 0; i < 100; i++ ) {
112+
v = ( randu()*100.0 ) - 50.0;
113+
accumulator( v );
114+
}
115+
console.log( accumulator() );
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<section class="links">
123+
124+
</section>
125+
126+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrsumabs2 = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrsumabs2();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrsumabs2();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()-0.5 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a sum of
4+
squared absolute values.
5+
6+
If provided a value, the accumulator function returns an updated sum. If not
7+
provided a value, the accumulator function returns the current sum.
8+
9+
If provided `NaN` or a value which, when used in computations, results in
10+
`NaN`, the accumulated value is `NaN` for all future invocations.
11+
12+
For long running accumulations or accumulations of large numbers, care
13+
should be taken to prevent overflow.
14+
15+
Returns
16+
-------
17+
acc: Function
18+
Accumulator function.
19+
20+
Examples
21+
--------
22+
> var accumulator = {{alias}}();
23+
> var s = accumulator()
24+
null
25+
> s = accumulator( 2.0 )
26+
4.0
27+
> s = accumulator( -5.0 )
28+
29.0
29+
> s = accumulator()
30+
29.0
31+
32+
See Also
33+
--------
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
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+
'use strict';
20+
21+
var randu = require( '@stdlib/random/base/randu' );
22+
var incrsumabs2 = require( './../lib' );
23+
24+
var accumulator;
25+
var sum;
26+
var v;
27+
var i;
28+
29+
// Initialize an accumulator:
30+
accumulator = incrsumabs2();
31+
32+
// For each simulated datum, update the sum...
33+
console.log( '\nValue\tSum\n' );
34+
for ( i = 0; i < 100; i++ ) {
35+
v = ( randu()*100.0 ) - 50.0;
36+
sum = accumulator( v );
37+
console.log( '%d\t%d', v.toFixed( 3 ), sum.toFixed( 3 ) );
38+
}
39+
console.log( '\nFinal sum: %d\n', accumulator() );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
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+
'use strict';
20+
21+
/**
22+
* Compute a sum of squared absolute values incrementally.
23+
*
24+
* @module @stdlib/stats/incr/sumabs2
25+
*
26+
* @example
27+
* var incrsumabs2 = require( '@stdlib/stats/incr/sumabs2' );
28+
*
29+
* var accumulator = incrsumabs2();
30+
*
31+
* var sum = accumulator();
32+
* // returns null
33+
*
34+
* sum = accumulator( 2.0 );
35+
* // returns 4.0
36+
*
37+
* sum = accumulator( -5.0 );
38+
* // returns 29.0
39+
*
40+
* sum = accumulator();
41+
* // returns 29.0
42+
*/
43+
44+
// MODULES //
45+
46+
var incrsumabs2 = require( './main.js' );
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = incrsumabs2;

0 commit comments

Comments
 (0)