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
230 changes: 230 additions & 0 deletions lib/node_modules/@stdlib/ml/base/loss/float64/hinge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<!--

@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.

-->

# hinge

> Compute the [hinge loss][hinge-loss] between two double-precision floating-point numbers.

<section class="intro">

Comment thread
kgryte marked this conversation as resolved.
The [hinge loss][hinge-loss] is defined as

<!-- <equation class="equation" label="eq:hinge_loss" align="center" raw="{L(y,p)=\max(0,t-y\cdot p)}" alt="Equation for the hinge loss."> -->

Comment thread
kgryte marked this conversation as resolved.
```math
{L(y,p)=\max(0,t-y\cdot p)}
```

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var hinge = require( '@stdlib/ml/base/loss/float64/hinge' );
```

#### hinge( t, y, p )

Computes the [hinge loss][hinge-loss] between two double-precision floating-point numbers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "between two double-precision floating-point numbers"? Why not "with respect to a model parameter" as you did for hinge-gradient? Question applies throughout this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, “with respect to a model parameter” would not be applicable, since we are not differentiating the loss function with respect to a model parameter, as we would when computing the loss gradient. However, I’m open to changing the description to something more appropriate if you have a better suggestion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make things more clear:

  1. Hinge Loss function
$$L = \max(0, 1-yf(x))$$
  1. Hinge loss function gradient
$$\frac{\partial L}{\partial w} = \begin{cases} -yx, & y(w^Tx+b)<1\\[4pt] 0, & y(w^Tx+b)>1 \end{cases}$$

Here it is being differentiated wrt to model parameter.


```javascript
var v = hinge( 1.0, 1.0, 0.782 );
// returns ~0.218

v = hinge( 2.0, 1.0, -0.9 );
// returns 2.9
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter list.

The function accepts the following arguments:

- **t**: margin threshold.
- **y**: true target value.
- **p**: predicted value.


Comment on lines +66 to +67

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

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

```javascript
var v = hinge( 1.0, NaN, 12.0 );
// returns NaN

v = hinge( 2.0, 5.0, NaN );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
v = hinge( 2.0, 5.0, NaN );
v = hinge( 2.0, 1.0, NaN );

y = 5.0 already returns NaN, so this example doesn't show the NaN case for p.

// returns NaN
```

If `y` is not +1 or -1, the function returns `NaN`.

```javascript
var v = hinge( 1.0, 2.3, 1.0 );
// returns NaN

v = hinge( 1.0, -1.3, 0.987 );
// returns NaN
```

</section>

<!-- /.usage -->

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!-- /.usage -->
<!-- /.usage -->
<section class="notes">
## Notes
- When `t = 1.0`, we get the loss used by SVM; whereas, when `t = 0.0`, we get the loss used by the Perceptron.
</section>
<!-- /.notes -->

Same note as hinge-gradient. t was added so this package could also serve as the Perceptron loss.


<section class="examples">

## Examples

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

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

var t = uniform( 100, 0.0, 10.0, {
'dtype': 'float64'
});
var y = sample( [ -1.0, 1.0 ], {
'size': 100
});
var p = uniform( 100, -5.0, 5.0, {
'dtype': 'float64'
});

logEachMap( 'hinge(%0.4f, %0.4f, %0.4f) = %0.4f', t, y, p, hinge );
```

</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/hinge.h"
```

#### stdlib_base_float64_hinge( t, y, p )

Computes the [hinge loss][hinge-loss] between two double-precision floating-point numbers.

```c
double out = stdlib_base_float64_hinge( 1.0, 1.0, 0.782 );
// returns ~0.218
```

The function accepts the following arguments:

- **t**: `[in] double` threshold value.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **t**: `[in] double` threshold value.
- **t**: `[in] double` margin threshold.

Matches line 63 and hinge-gradient. Same in lib/main.js:32, lib/native.js:32, docs/types/index.d.ts:28, src/main.c:27 and docs/repl.txt:12 (Margin threshold.).

- **y**: `[in] double` true target value.
- **p**: `[in] double` predicted value.

```c
double stdlib_base_float64_hinge( const double t, 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/hinge.h"
#include <stdio.h>

int main( void ) {
const double t[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
const double y[] = { -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
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_hinge( t[ i ], y[ i ], p[ i ] );
printf( "hinge(%lf, %lf, %lf) = %lf\n", t[ 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">

[hinge-loss]: https://en.wikipedia.org/wiki/Hinge_loss

<!-- <related-links> -->

Comment thread
kgryte marked this conversation as resolved.
<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @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 sample = require( '@stdlib/random/sample' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var hinge = require( './../lib' );


// MAIN //

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

len = 100;
y = sample( [ -1.0, 1.0 ], {
'size': len
});
p = uniform( len, -2.0, 2.0 );
t = uniform( len, 0.0, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = hinge( t[ i % t.length ], y[ i % y.length ], p[ i % p.length ] );
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,71 @@
/**
* @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 sample = require( '@stdlib/random/sample' );
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 hinge = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( hinge instanceof Error )
};


// MAIN //

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

len = 100;
y = sample( [ -1.0, 1.0 ], {
'size': len
});
p = uniform( len, -2.0, 2.0 );
t = uniform( len, 0.0, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = hinge( t[ i % t.length ], y[ i % y.length ], p[ i % p.length ] );
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
Loading