Skip to content

feat: add ml/base/loss/float64/hinge#11953

Open
nakul-krishnakumar wants to merge 4 commits intostdlib-js:developfrom
nakul-krishnakumar:ml-loss-hinge
Open

feat: add ml/base/loss/float64/hinge#11953
nakul-krishnakumar wants to merge 4 commits intostdlib-js:developfrom
nakul-krishnakumar:ml-loss-hinge

Conversation

@nakul-krishnakumar
Copy link
Copy Markdown
Member


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

Resolves None.

Description

What is the purpose of this pull request?

This pull request:

  • Adds ml/base/loss/float64/hinge.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • None.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

None.


@stdlib-js/reviewers

---
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
---
@nakul-krishnakumar nakul-krishnakumar requested a review from a team May 5, 2026 15:51
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label May 5, 2026
@nakul-krishnakumar nakul-krishnakumar added GSoC Google Summer of Code. gsoc: 2026 Google Summer of Code (2026). Needs Review A pull request which needs code review. and removed Needs Review A pull request which needs code review. labels May 5, 2026
@stdlib-bot
Copy link
Copy Markdown
Contributor

stdlib-bot commented May 5, 2026

Coverage Report

No coverage information available.

@nakul-krishnakumar
Copy link
Copy Markdown
Member Author

nakul-krishnakumar commented May 5, 2026

Clarifications required:

  1. hinge( y, p ) loss function expect y to be either +1 or -1. Should I mention this in the README.md and repl.txt?
  2. Please have a look at the fixture generation and lmk if the approach is apt.
  3. Should I keep y as integer or double? y is the target class label, so it would be an integer, but keeping it as double seems more appropriate.

@nakul-krishnakumar
Copy link
Copy Markdown
Member Author

/stdlib update-copyright-years

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label May 5, 2026
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label May 5, 2026
@nakul-krishnakumar nakul-krishnakumar marked this pull request as draft May 5, 2026 17:22
@stdlib-bot stdlib-bot removed the Needs Review A pull request which needs code review. label May 5, 2026
@nakul-krishnakumar nakul-krishnakumar marked this pull request as ready for review May 5, 2026 18:01
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label May 5, 2026
---
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: na
  - 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: na
  - task: lint_license_headers
    status: passed
---
* limitations under the License.
*/

#ifndef STDLIB_ML_BASE_FLOAT64_HINGE_H
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
#ifndef STDLIB_ML_BASE_FLOAT64_HINGE_H
#ifndef STDLIB_ML_BASE_LOSS_FLOAT64_HINGE_H

Guards need to match the package name, NOT the function name. Applies here and below.

// MAIN //

/**
* Computes the hinge loss between two double-precision floating-point number.
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
* Computes the hinge loss between two double-precision floating-point number.
* Computes the hinge loss between two double-precision floating-point numbers.

Comment on lines +62 to +65
if ( isnan( y ) || isnan( p ) ) {
return NaN;
}
if ( y !== -1.0 && y !== 1.0 ) {
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 ( isnan( y ) || isnan( p ) ) {
return NaN;
}
if ( y !== -1.0 && y !== 1.0 ) {
if ( isnan( y ) || isnan( p ) || y !== -1.0 && y !== 1.0 ) {

// MAIN //

/**
* Computes the hinge loss between two double-precision floating-point number.
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
* Computes the hinge loss between two double-precision floating-point number.
* Computes the hinge loss between two double-precision floating-point numbers.

Applies here and throughout this PR.

Comment on lines +36 to +39
if ( stdlib_base_is_nan( y ) || stdlib_base_is_nan( p ) ) {
return STDLIB_CONSTANT_FLOAT64_NAN;
}
if ( y != -1.0 && y != 1.0 ) {
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 ( stdlib_base_is_nan( y ) || stdlib_base_is_nan( p ) ) {
return STDLIB_CONSTANT_FLOAT64_NAN;
}
if ( y != -1.0 && y != 1.0 ) {
if ( stdlib_base_is_nan( y ) || stdlib_base_is_nan( p ) || y != -1.0 && y != 1.0 ) {

if ( y != -1.0 && y != 1.0 ) {
return STDLIB_CONSTANT_FLOAT64_NAN;
}
return stdlib_base_max( 0, 1 - ( y*p ) );
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
return stdlib_base_max( 0, 1 - ( y*p ) );
return stdlib_base_max( 0.0, 1.0 - ( y*p ) );

Floating-point values should have decimals; otherwise, you force implicit casts.

if ( y !== -1.0 && y !== 1.0 ) {
return NaN;
}
return max( 0, 1 - ( y*p ) );
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
return max( 0, 1 - ( y*p ) );
return max( 0.0, 1.0 - ( y*p ) );

Computes the hinge loss between two double-precision floating-point number.

If either argument is `NaN`, the function returns `NaN`.
If `y` is not +1 or -1, the function returns `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
If `y` is not +1 or -1, the function returns `NaN`.
If `y` is not +1 or -1, the function returns `NaN`.

hinge( 1.0, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
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
// The compiler throws an error if the function is provided insufficient arguments...
// The compiler throws an error if the function is provided an unsupported number of arguments...

Comment on lines +97 to +99
return 1;
}
return -1;
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
return 1;
}
return -1;
return 1.0;
}
return -1.0;

Avoid implicit casts.

Copy link
Copy Markdown
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

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

Left initial comments.

@kgryte kgryte removed the Needs Review A pull request which needs code review. label May 5, 2026
@kgryte kgryte added Feature Issue or pull request for adding a new feature. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Needs Changes Pull request which needs changes before being merged. review: 3 labels May 5, 2026
---
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: 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: na
  - task: lint_javascript_tests
    status: na
  - 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: na
  - 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
---
@nakul-krishnakumar nakul-krishnakumar added Needs Review A pull request which needs code review. and removed Needs Changes Pull request which needs changes before being merged. labels May 6, 2026
@nakul-krishnakumar nakul-krishnakumar requested a review from kgryte May 6, 2026 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Feature Issue or pull request for adding a new feature. GSoC Google Summer of Code. gsoc: 2026 Google Summer of Code (2026). Needs Review A pull request which needs code review. review: 3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants