Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions lib/node_modules/@stdlib/ml/base/loss/float64/huber-gradient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# huberGradient

> Compute the [huber loss gradient][huber-loss-gradient] with respect to a model parameter.

<section class="intro">

The [huber loss gradient][huber-loss-gradient] is defined as

<!-- <equation class="equation" label="eq:huber_loss_gradient" align="center" raw="\frac{\partial L}{\partial w}=
\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."> -->

```math
\frac{\partial L}{\partial w}=
\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}
```

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

<!-- eslint-disable id-length -->

```javascript
var huberGradient = require( '@stdlib/ml/base/loss/float64/huber-gradient' );
```

#### huberGradient( x, d, y, p )

Computes the [huber loss gradient][huber-loss-gradient] with respect to a model parameter.

<!-- eslint-disable id-length -->

```javascript
var v = huberGradient( 3.0, 5.0, 10.2, 0.782 );
// returns -15.0

v = huberGradient( -1.3, 1.0, 23.2, -0.999 );
// returns 1.3
```

The function accepts the following arguments:

- **x**: input value.
- **d**: threshold.
- **y**: true target value.
- **p**: predicted value.

If any argument is `NaN`, the function returns `NaN`.

<!-- eslint-disable id-length -->

```javascript
var v = huberGradient( NaN, 1.0, 1.0, 0.782 );
// returns NaN

v = huberGradient( 1.0, 1.0, NaN, 0.782 );
// returns NaN

v = huberGradient( NaN, NaN, 1.0, 0.782 );
// returns NaN

v = huberGradient( NaN, NaN, NaN, NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint-disable id-length -->

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var huberGradient = require( '@stdlib/ml/base/loss/float64/huber-gradient' );

var x = uniform( 100, -100.0, 100.0, {
'dtype': 'float64'
});
var d = uniform( 100, 0.0, 5.0, {
'dtype': 'float64'
});
var y = uniform( 100, -100.0, 100.0, {
'dtype': 'float64'
});
var p = uniform( 100, -5.0, 5.0, {
'dtype': 'float64'
});

logEachMap( 'huberGradient(%0.4f, %0.4f, %0.4f, %0.4f) = %0.4f', x, d, y, p, huberGradient );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/ml/base/loss/float64/huber_gradient.h"
```

#### stdlib_base_float64_huber_gradient( x, d, y, p )

Computes the [huber loss gradient][huber-loss-gradient] with respect to a model parameter.

```c
double out = stdlib_base_float64_huber_gradient( 3.0, 5.0, 10.2, 0.782 );
// returns -15.0
```

The function accepts the following arguments:

- **x**: `[in] double` input value.
- **d**: `[in] double` threshold.
- **y**: `[in] double` true target value.
- **p**: `[in] double` predicted value.

```c
double stdlib_base_float64_huber_gradient( const double x, const double d, const double y, const double p );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/ml/base/loss/float64/huber_gradient.h"
#include <stdio.h>

int main( void ) {
const double x[] = { -10.0, -9.56, -8.67, -7.78, -6.89, 6.89, 7.78, 8.67, 9.56, 10.0 };
const double d[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
const double y[] = { -9.9, -7.7, -5.5, -3.3, -1.1, 1.1, 3.3, 5.5, 7.7, 9.9 };
const double p[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };

double v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_float64_huber_gradient( x[ i ], d[ i ], y[ i ], p[ i ] );
printf( "huberGradient(%lf, %lf, %lf, %lf) = %lf\n", x[ i ], d[ i ], y[ i ], p[ i ], v );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[huber-loss-gradient]: https://en.wikipedia.org/wiki/Huber_loss

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var huberGradient = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var len;
var x;
var y;
var p;
var v;
var i;

len = 100;
x = uniform( len, -100.0, 100.0 );
y = uniform( len, -100.0, 100.0 );
p = uniform( len, -100.0, 100.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = huberGradient( x[ i%x.length ], 1.0, y[ i%y.length ], p[ i%p.length ] );

Check warning on line 47 in lib/node_modules/@stdlib/ml/base/loss/float64/huber-gradient/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 84. Maximum allowed is 80
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var huberGradient = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( huberGradient instanceof Error )
};


// MAIN //

bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
var len;
var x;
var y;
var p;
var v;
var i;

len = 100;
x = uniform( len, -100.0, 100.0 );
y = uniform( len, -100.0, 100.0 );
p = uniform( len, -100.0, 100.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = huberGradient( x[ i%x.length ], 1.0, y[ i%y.length ], p[ i%p.length ] );

Check warning on line 57 in lib/node_modules/@stdlib/ml/base/loss/float64/huber-gradient/benchmark/benchmark.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 84. Maximum allowed is 80
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading