Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: add ml/base/loss/float64/hinge
---
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
    status: passed
  - 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
---
  • Loading branch information
nakul-krishnakumar committed May 5, 2026
commit 16d16535c53af8415d4f12bc6453de24430e4375
201 changes: 201 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,201 @@
<!--

@license Apache-2.0

Copyright (c) 2022 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 number.
Comment thread
kgryte marked this conversation as resolved.
Outdated

<section class="intro">

Comment thread
kgryte marked this conversation as resolved.
The [hinge loss][hinge-loss] between `y` and `p` is defined as

<!-- <equation class="equation" label="eq:hinge_loss" align="center" raw="g = max(0, 1 - y*p)" alt="Equation for the sample correlation distance."> -->

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.

Note to self: the GitHub Web UI messes up editing code blocks. Need to add manually.

```math
g = max(0, 1 - y*p)
```
Comment thread
kgryte marked this conversation as resolved.
Outdated

<!-- </equation> -->


</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### hinge( y, p )

Computes the [hinge loss][hinge-loss] between two double-precision floating-point number.
Comment thread
kgryte marked this conversation as resolved.
Outdated

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

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

</section>

<!-- /.usage -->

<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 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', 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( y, p )

Computes the [hinge loss][hinge-loss] between two double-precision floating-point number.
Comment thread
kgryte marked this conversation as resolved.
Outdated

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

The function accepts the following arguments:

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

```c
double stdlib_base_float64_hinge( 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 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( y[ i ], p[ i ] );
printf( "hinge(%lf, %lf) = %lf\n", 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,57 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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 y;
var p;
var g;
var i;

y = sample( [ -1, 1 ], {
'size': 100
});
p = uniform( 100, -2.0, 2.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
g = hinge( y[ i % y.length ], p[ i % p.length ] );
if ( isnan( g ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( g ) ) {
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,67 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 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 y;
var p;
var g;
var i;

y = sample( [ -1, 1 ], {
'size': 100
});
p = uniform( 100, -2.0, 2.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
g = hinge( y[ i % y.length ], p[ i % p.length ] );
if ( isnan( g ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( g ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading