Skip to content

Commit 52d65af

Browse files
feat: add ml/base/loss/float64/huber-gradient
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 79dfbc4 commit 52d65af

30 files changed

Lines changed: 2226 additions & 0 deletions

File tree

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# huberGradient
22+
23+
> Compute the [huber loss gradient][huber-loss-gradient] with respect to a model parameter.
24+
25+
<section class="intro">
26+
27+
The [huber loss gradient][huber-loss-gradient] is defined as
28+
29+
<!-- <equation class="equation" label="eq:huber_loss_gradient" align="center" raw="\frac{\partial L}{\partial w}=
30+
\begin{cases}-(y-p)x, & \left|y-p\right|\le\delta,\\[6pt]-\delta\,\operatorname{sign}(y-p)\,x, & \left|y-p\right|>\delta.\end{cases}" alt="Equation for the huber loss gradient."> -->
31+
32+
```math
33+
\frac{\partial L}{\partial w}=
34+
\begin{cases}
35+
-(y-p)x, & \left|y-p\right|\le\delta,\\[6pt]
36+
-\delta\,\operatorname{sign}(y-p)\,x, & \left|y-p\right|>\delta.
37+
\end{cases}
38+
```
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
<!-- eslint-disable id-length -->
51+
52+
```javascript
53+
var huberGradient = require( '@stdlib/ml/base/loss/float64/huber-gradient' );
54+
```
55+
56+
#### huberGradient( x, d, y, p )
57+
58+
Computes the [huber loss gradient][huber-loss-gradient] with respect to a model parameter.
59+
60+
<!-- eslint-disable id-length -->
61+
62+
```javascript
63+
var v = huberGradient( 3.0, 5.0, 10.2, 0.782 );
64+
// returns -15.0
65+
66+
v = huberGradient( -1.3, 1.0, 23.2, -0.999 );
67+
// returns 1.3
68+
```
69+
70+
The function accepts the following arguments:
71+
72+
- **x**: input value.
73+
- **d**: threshold.
74+
- **y**: true target value.
75+
- **p**: predicted value.
76+
77+
If any argument is `NaN`, the function returns `NaN`.
78+
79+
<!-- eslint-disable id-length -->
80+
81+
```javascript
82+
var v = huberGradient( NaN, 1.0, 1.0, 0.782 );
83+
// returns NaN
84+
85+
v = huberGradient( 1.0, 1.0, NaN, 0.782 );
86+
// returns NaN
87+
88+
v = huberGradient( NaN, NaN, 1.0, 0.782 );
89+
// returns NaN
90+
91+
v = huberGradient( NaN, NaN, NaN, NaN );
92+
// returns NaN
93+
```
94+
95+
</section>
96+
97+
<!-- /.usage -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint-disable id-length -->
104+
105+
<!-- eslint no-undef: "error" -->
106+
107+
```javascript
108+
var uniform = require( '@stdlib/random/array/uniform' );
109+
var logEachMap = require( '@stdlib/console/log-each-map' );
110+
var huberGradient = require( '@stdlib/ml/base/loss/float64/huber-gradient' );
111+
112+
var x = uniform( 100, -100.0, 100.0, {
113+
'dtype': 'float64'
114+
});
115+
var d = uniform( 100, 0.0, 5.0, {
116+
'dtype': 'float64'
117+
});
118+
var y = uniform( 100, -100.0, 100.0, {
119+
'dtype': 'float64'
120+
});
121+
var p = uniform( 100, -5.0, 5.0, {
122+
'dtype': 'float64'
123+
});
124+
125+
logEachMap( 'huberGradient(%0.4f, %0.4f, %0.4f, %0.4f) = %0.4f', x, d, y, p, huberGradient );
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- C interface documentation. -->
133+
134+
* * *
135+
136+
<section class="c">
137+
138+
## C APIs
139+
140+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
141+
142+
<section class="intro">
143+
144+
</section>
145+
146+
<!-- /.intro -->
147+
148+
<!-- C usage documentation. -->
149+
150+
<section class="usage">
151+
152+
### Usage
153+
154+
```c
155+
#include "stdlib/ml/base/loss/float64/huber_gradient.h"
156+
```
157+
158+
#### stdlib_base_float64_huber_gradient( x, d, y, p )
159+
160+
Computes the [huber loss gradient][huber-loss-gradient] with respect to a model parameter.
161+
162+
```c
163+
double out = stdlib_base_float64_huber_gradient( 3.0, 5.0, 10.2, 0.782 );
164+
// returns -15.0
165+
```
166+
167+
The function accepts the following arguments:
168+
169+
- **x**: `[in] double` input value.
170+
- **d**: `[in] double` threshold.
171+
- **y**: `[in] double` true target value.
172+
- **p**: `[in] double` predicted value.
173+
174+
```c
175+
double stdlib_base_float64_huber_gradient( const double x, const double d, const double y, const double p );
176+
```
177+
178+
</section>
179+
180+
<!-- /.usage -->
181+
182+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
183+
184+
<section class="notes">
185+
186+
</section>
187+
188+
<!-- /.notes -->
189+
190+
<!-- C API usage examples. -->
191+
192+
<section class="examples">
193+
194+
### Examples
195+
196+
```c
197+
#include "stdlib/ml/base/loss/float64/huber_gradient.h"
198+
#include <stdio.h>
199+
200+
int main( void ) {
201+
const double x[] = { -10.0, -9.56, -8.67, -7.78, -6.89, 6.89, 7.78, 8.67, 9.56, 10.0 };
202+
const double d[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
203+
const double y[] = { -9.9, -7.7, -5.5, -3.3, -1.1, 1.1, 3.3, 5.5, 7.7, 9.9 };
204+
const double p[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
205+
206+
double v;
207+
int i;
208+
for ( i = 0; i < 10; i++ ) {
209+
v = stdlib_base_float64_huber_gradient( x[ i ], d[ i ], y[ i ], p[ i ] );
210+
printf( "huberGradient(%lf, %lf, %lf, %lf) = %lf\n", x[ i ], d[ i ], y[ i ], p[ i ], v );
211+
}
212+
}
213+
```
214+
215+
</section>
216+
217+
<!-- /.examples -->
218+
219+
</section>
220+
221+
<!-- /.c -->
222+
223+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
224+
225+
<section class="related">
226+
227+
</section>
228+
229+
<!-- /.related -->
230+
231+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
232+
233+
<section class="links">
234+
235+
[huber-loss-gradient]: https://en.wikipedia.org/wiki/Huber_loss
236+
237+
</section>
238+
239+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var huberGradient = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var len;
34+
var x;
35+
var y;
36+
var p;
37+
var v;
38+
var i;
39+
40+
len = 100;
41+
x = uniform( len, -100.0, 100.0 );
42+
y = uniform( len, -100.0, 100.0 );
43+
p = uniform( len, -100.0, 100.0 );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
v = huberGradient( x[ i%x.length ], 1.0, y[ i%y.length ], p[ i%p.length ] );
48+
if ( isnan( v ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnan( v ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var huberGradient = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( huberGradient instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
43+
var len;
44+
var x;
45+
var y;
46+
var p;
47+
var v;
48+
var i;
49+
50+
len = 100;
51+
x = uniform( len, -100.0, 100.0 );
52+
y = uniform( len, -100.0, 100.0 );
53+
p = uniform( len, -100.0, 100.0 );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = huberGradient( x[ i%x.length ], 1.0, y[ i%y.length ], p[ i%p.length ] );
58+
if ( isnan( v ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( v ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});

0 commit comments

Comments
 (0)