Skip to content

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

Open
nakul-krishnakumar wants to merge 19 commits into
stdlib-js:developfrom
nakul-krishnakumar:ml-loss-hinge
Open

nakul-krishnakumar wants to merge 19 commits into
stdlib-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

stdlib-bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
ml/base/loss/float64/hinge $\\color{green}194/194$
$\\color{green}+0.00\\%$
$\\color{green}11/11$
$\\color{green}+0.00\\%$
$\\color{green}2/2$
$\\color{green}+0.00\\%$
$\\color{green}194/194$
$\\color{green}+0.00\\%$

The above coverage report was generated for the changes in this PR.

@nakul-krishnakumar

nakul-krishnakumar commented May 5, 2026 •

Copy link
Copy Markdown
Member Author

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
---
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/lib/main.js Outdated
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/lib/main.js Outdated
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/lib/native.js Outdated
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/src/main.c Outdated
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/src/main.c Outdated
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/lib/main.js Outdated
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/docs/repl.txt
Comment thread lib/node_modules/@stdlib/ml/base/loss/float64/hinge/docs/types/test.ts Outdated

@kgryte kgryte left a comment

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.

Left initial comments.

@kgryte kgryte removed the Needs Review A pull request which needs code review. label May 5, 2026
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
@kgryte
kgryte self-requested a review May 14, 2026 09:06
@kgryte kgryte removed the Needs Changes Pull request which needs changes before being merged. label May 14, 2026
Signed-off-by: Athan <kgryte@gmail.com>
@nakul-krishnakumar

Copy link
Copy Markdown
Member Author

Before this gets merged, I wanted to discuss a possibility. If we were supposed to add a threshold arg which would be either 1.0 or 0.0, then we could reuse this implementation for perceptron-loss. Reference: sklearn

@kgryte

kgryte commented May 28, 2026

Copy link
Copy Markdown
Member

@nakul-krishnakumar Yes, fine with adding a threshold argument. Although, it need not be restricted to 0 and 1. Can be any value greater than or equal to 0.

@nakul-krishnakumar nakul-krishnakumar added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Jun 2, 2026
@nakul-krishnakumar

Copy link
Copy Markdown
Member Author

/stdlib merge

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Jul 24, 2026
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Jul 24, 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: skipped
  - task: lint_markdown_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - 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 added Needs Review A pull request which needs code review. and removed Needs Changes Pull request which needs changes before being merged. labels Jul 24, 2026
@kgryte kgryte removed the Needs Review A pull request which needs code review. label Aug 9, 2026
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.


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

@kgryte kgryte added the Needs Changes Pull request which needs changes before being merged. label Aug 9, 2026
@kgryte

kgryte commented Sep 28, 2026

Copy link
Copy Markdown
Member

@nakul-krishnakumar Would you be up for moving this PR along? Cheers!

@kgryte kgryte added Needs Review A pull request which needs code review. and removed Needs Changes Pull request which needs changes before being merged. labels Oct 6, 2026
@kgryte
kgryte self-requested a review October 6, 2026 16:44
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

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.


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


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


/**
* Computes the 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.

Suggested change
*
*
* ## Notes
*
* - If `y` is not +1 or -1, the function returns `NaN`.
* - When `t = 1.0`, we get the loss used by SVM, whereas when `t = 0.0`, we get the loss used by the Perceptron.
*

Matches hinge-gradient/lib/main.js. Add the SVM/Perceptron note in docs/types/index.d.ts:26 and after docs/repl.txt:7 as well.

* // returns 2.099
*
* @example
* var v = hinge( 5.0, -1.0, 0.234 );

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
* var v = hinge( 5.0, -1.0, 0.234 );
* var v = hinge( 5.0, -1.0, 0.234 );

Same in lib/native.js:50, lib/index.js:38 and docs/types/index.d.ts:46.

This branch has not been deployed

No deployments
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.

4 participants