Skip to content

Commit 16d1653

Browse files
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 ---
1 parent c5fad82 commit 16d1653

30 files changed

Lines changed: 2075 additions & 0 deletions

File tree

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# hinge
22+
23+
> Compute the [hinge loss][hinge-loss] between two double-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [hinge loss][hinge-loss] between `y` and `p` is defined as
28+
29+
<!-- <equation class="equation" label="eq:hinge_loss" align="center" raw="g = max(0, 1 - y*p)" alt="Equation for the sample correlation distance."> -->
30+
31+
```math
32+
g = max(0, 1 - y*p)
33+
```
34+
35+
<!-- </equation> -->
36+
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var hinge = require( '@stdlib/ml/base/loss/float64/hinge' );
48+
```
49+
50+
#### hinge( y, p )
51+
52+
Computes the [hinge loss][hinge-loss] between two double-precision floating-point number.
53+
54+
```javascript
55+
var v = hinge( 1.0, 0.782 );
56+
// returns ~0.218
57+
58+
v = hinge( 1.0, -0.9 );
59+
// returns 1.9
60+
```
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
<!-- eslint no-undef: "error" -->
71+
72+
```javascript
73+
var uniform = require( '@stdlib/random/array/uniform' );
74+
var sample = require( '@stdlib/random/sample' );
75+
var logEachMap = require( '@stdlib/console/log-each-map' );
76+
var hinge = require( '@stdlib/ml/base/loss/float64/hinge' );
77+
78+
var y = sample( [ -1.0, 1.0 ], {
79+
'size': 100
80+
});
81+
var p = uniform( 100, -5.0, 5.0, {
82+
'dtype': 'float64'
83+
});
84+
85+
logEachMap( 'hinge(%0.4f, %0.4f) = %0.4f', y, p, hinge );
86+
87+
```
88+
89+
</section>
90+
91+
<!-- /.examples -->
92+
93+
<!-- C interface documentation. -->
94+
95+
* * *
96+
97+
<section class="c">
98+
99+
## C APIs
100+
101+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
102+
103+
<section class="intro">
104+
105+
</section>
106+
107+
<!-- /.intro -->
108+
109+
<!-- C usage documentation. -->
110+
111+
<section class="usage">
112+
113+
### Usage
114+
115+
```c
116+
#include "stdlib/ml/base/loss/float64/hinge.h"
117+
```
118+
119+
#### stdlib_base_float64_hinge( y, p )
120+
121+
Computes the [hinge loss][hinge-loss] between two double-precision floating-point number.
122+
123+
```c
124+
double out = stdlib_base_float64_hinge( 1.0, 0.782 );
125+
// returns ~0.218
126+
```
127+
128+
The function accepts the following arguments:
129+
130+
- **y**: `[in] double` true target value.
131+
- **p**: `[in] double` predicted value.
132+
133+
```c
134+
double stdlib_base_float64_hinge( const double y, const double p );
135+
```
136+
137+
</section>
138+
139+
<!-- /.usage -->
140+
141+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="notes">
144+
145+
</section>
146+
147+
<!-- /.notes -->
148+
149+
<!-- C API usage examples. -->
150+
151+
<section class="examples">
152+
153+
### Examples
154+
155+
```c
156+
#include "stdlib/ml/base/loss/float64/hinge.h"
157+
#include <stdio.h>
158+
159+
int main( void ) {
160+
const double y[] = { -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
161+
const double p[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
162+
163+
double v;
164+
int i;
165+
for ( i = 0; i < 10; i++ ) {
166+
v = stdlib_base_float64_hinge( y[ i ], p[ i ] );
167+
printf( "hinge(%lf, %lf) = %lf\n", y[ i ], p[ i ], v );
168+
}
169+
}
170+
```
171+
172+
</section>
173+
174+
<!-- /.examples -->
175+
176+
</section>
177+
178+
<!-- /.c -->
179+
180+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
181+
182+
<section class="related">
183+
184+
</section>
185+
186+
<!-- /.related -->
187+
188+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
189+
190+
<section class="links">
191+
192+
[hinge-loss]: https://en.wikipedia.org/wiki/Hinge_loss
193+
194+
<!-- <related-links> -->
195+
196+
197+
<!-- </related-links> -->
198+
199+
</section>
200+
201+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 sample = require( '@stdlib/random/sample' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var hinge = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var y;
35+
var p;
36+
var g;
37+
var i;
38+
39+
y = sample( [ -1, 1 ], {
40+
'size': 100
41+
});
42+
p = uniform( 100, -2.0, 2.0 );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
g = hinge( y[ i % y.length ], p[ i % p.length ] );
47+
if ( isnan( g ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnan( g ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 sample = require( '@stdlib/random/sample' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var hinge = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( hinge instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
44+
var y;
45+
var p;
46+
var g;
47+
var i;
48+
49+
y = sample( [ -1, 1 ], {
50+
'size': 100
51+
});
52+
p = uniform( 100, -2.0, 2.0 );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
g = hinge( y[ i % y.length ], p[ i % p.length ] );
57+
if ( isnan( g ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( g ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)