⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Conversation

@Om-A-osc
Copy link

Progresses #9416.

Description

This pull request:

  • Adds the probability density function (PDF) implementation for the half-normal distribution.
  • Includes JavaScript and native implementations, along with tests, benchmarks, examples, and Julia fixtures for validation.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

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.

I used AI assistance to verify the mathematical formula for the half-normal PDF and to confirm expected numerical values.


@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: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - 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: missing_dependencies
  - task: lint_c_examples
    status: missing_dependencies
  - task: lint_c_benchmarks
    status: missing_dependencies
  - 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
---
@stdlib-bot stdlib-bot added the Statistics Issue or pull request related to statistical functionality. label Jan 11, 2026
@stdlib-bot
Copy link
Contributor

👋 Hi there! 👋

And thank you for opening your first pull request! We will review it shortly. 🏃 💨

Getting Started

Next Steps

  1. A project maintainer will approve GitHub Actions workflows for your PR.
  2. All CI checks must pass before your submission can be fully reviewed.
  3. You'll need to address any failures in linting or unit tests.

Running Tests Locally

You can use make to run any of the CI commands locally from the root directory of the stdlib repository:

# Run tests for all packages in the math namespace:
make test TESTS_FILTER=".*/@stdlib/math/.*"

# Run benchmarks for a specific package:
make benchmark BENCHMARKS_FILTER=".*/@stdlib/math/base/special/sin/.*"

If you haven't heard back from us within two weeks, please ping us by tagging the "reviewers" team in a comment on this PR.

If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members.

We appreciate your contribution!

Documentation Links

@stdlib-bot stdlib-bot added First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Review A pull request which needs code review. labels Jan 11, 2026
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Jan 11, 2026

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/halfnormal/pdf $\color{green}269/269$
$\color{green}+0.00%$
$\color{green}22/22$
$\color{green}+0.00%$
$\color{green}4/4$
$\color{green}+0.00%$
$\color{green}269/269$
$\color{green}+0.00%$

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

Copy link
Member

@Planeshifter Planeshifter left a comment

Choose a reason for hiding this comment

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

Thanks for opening your first PR!

Please address the review comments and workflow failures.

> y = {{alias}}( 2.0, 1.0 )
~0.108
> y = {{alias}}( 0.5, 1.0 )
~0.352
Copy link
Member

Choose a reason for hiding this comment

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

The expected value here is incorrect. pdf(0.5, 1.0) returns approximately 0.704, not 0.352.

Suggested change
~0.352
~0.704

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in latest commit.

> var y = myPDF( 0.0 )
~0.399
> y = myPDF( 2.0 )
~0.192
Copy link
Member

Choose a reason for hiding this comment

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

The expected value here is incorrect. myPDF(2.0) with sigma=2.0 returns approximately 0.242, not 0.192.

Suggested change
~0.192
~0.242

*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law of or agreed to in writing, software
Copy link
Member

Choose a reason for hiding this comment

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

There's a typo in the license header - "law of or agreed" should be "law or agreed". Curious how that happened.

Suggested change
* Unless required by applicable law of or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software

Copy link
Author

Choose a reason for hiding this comment

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

I’ve pushed two new commits addressing the review feedback. The first commit fixes the incorrect numeric values in the half-normal PDF documentation examples, and the second commit addresses the remaining style issues including the license header typo, indentation, and formatting as requested.

Kindly review when you have time, and please let me know if anything else needs adjustment. I’m happy to iterate further.

if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( sigma ) || sigma <= 0.0 ) {
return 0.0/0.0; // NaN
}
if( x < 0.0 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Missing space after if - should be if ( not if(

Suggested change
if( x < 0.0 ) {
if ( x < 0.0 ) {

if( x < 0.0 ) {
return 0.0;
}
const double C = x / sigma;
Copy link
Member

Choose a reason for hiding this comment

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

In C89/C90, all variable declarations must appear at the beginning of a block, before any executable statements. This declaration should be moved to the top of the function body (right after the opening brace on line 36), like:

double stdlib_base_dists_halfnormal_pdf( const double x, const double sigma ) {
	double C;
	if ( stdlib_base_is_nan( x ) || ...

Comment on lines 124 to 125
y = pdf( -1.0 );
t.strictEqual( isnan(y), true, 'returns expected value' );
Copy link
Member

Choose a reason for hiding this comment

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

These lines use spaces for indentation instead of tabs. Per the stdlib style guide, JavaScript files should use TAB indentation. Also, isnan(y) should have spaces inside the parentheses: isnan( y ).

Suggested change
y = pdf( -1.0 );
t.strictEqual( isnan(y), true, 'returns expected value' );
y = pdf( -1.0 );
t.strictEqual( isnan( y ), true, 'returns expected value' );

t.strictEqual( isnan(y), true, 'returns expected value' );

y = pdf( PINF );
t.strictEqual( isnan(y), true, 'returns expected value' );
Copy link
Member

Choose a reason for hiding this comment

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

Missing spaces inside parentheses - should be isnan( y ) for consistency with the rest of the file.

Suggested change
t.strictEqual( isnan(y), true, 'returns expected value' );
t.strictEqual( isnan( y ), true, 'returns expected value' );

t.strictEqual( isnan(y), true, 'returns expected value' );

y = pdf( NINF );
t.strictEqual( isnan(y), true, 'returns expected value' );
Copy link
Member

Choose a reason for hiding this comment

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

Missing spaces inside parentheses - should be isnan( y ) for consistency.

Suggested change
t.strictEqual( isnan(y), true, 'returns expected value' );
t.strictEqual( isnan( y ), true, 'returns expected value' );

Comment on lines 97 to 98
y = pdf( -1.0, 0.0 );
t.strictEqual( isnan( y ), true, 'returns expected value' );
Copy link
Member

Choose a reason for hiding this comment

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

These lines use spaces for indentation instead of tabs. Per the stdlib style guide, JavaScript files should use TAB indentation.

Suggested change
y = pdf( -1.0, 0.0 );
t.strictEqual( isnan( y ), true, 'returns expected value' );
y = pdf( -1.0, 0.0 );
t.strictEqual( isnan( y ), true, 'returns expected value' );

---
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: na
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - 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
---
---
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: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - 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
---
@Planeshifter Planeshifter changed the title feat: add implementation of halfnormal/pdf feat: add stats/base/dists/halfnormal/pdf Jan 11, 2026
Copy link
Member

@Planeshifter Planeshifter left a comment

Choose a reason for hiding this comment

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

This PR should be getting close; left one more round of comments.


@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.
Copy link
Member

Choose a reason for hiding this comment

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

This is a new file, so the copyright year should be 2026 rather than 2018.

Suggested change
Copyright (c) 2018 The Stdlib Authors.
Copyright (c) 2026 The Stdlib Authors.


[halfnormal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution

[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution
Copy link
Member

Choose a reason for hiding this comment

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

This link reference isn't used anywhere in the document - looks like copy-paste residue that can be removed.

Suggested change
[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution

var y = pdf( -1.0, 1.0 );
// returns 0.0
```

Copy link
Member

Choose a reason for hiding this comment

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

The README is missing the #### pdf.factory( sigma ) documentation section that should appear here to document the factory method. See other similar distribution packages like levy/pdf/README.md for the pattern - it should include a brief description and example usage of the factory function.

/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
Copy link
Member

Choose a reason for hiding this comment

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

New file - copyright year should be 2026.

Suggested change
* Copyright (c) 2018 The Stdlib Authors.
* Copyright (c) 2026 The Stdlib Authors.

/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
Copy link
Member

Choose a reason for hiding this comment

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

New file - copyright year should be 2026.

Suggested change
* Copyright (c) 2018 The Stdlib Authors.
* Copyright (c) 2026 The Stdlib Authors.

* var y = myPDF( 0.5 );
* // returns ~0.387
*/
declare const pdf: PDF;
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with other stdlib PDF packages, this should use declare var instead of declare const.

Suggested change
declare const pdf: PDF;
declare var pdf: PDF;

* @return evaluated PDF
*
* @example
* double y = stdlib_stats_base_dists_halfnormal_pdf( 2.0, 1.0 );
Copy link
Member

Choose a reason for hiding this comment

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

The function name in the example doesn't match the actual function name. It says stdlib_stats_base_dists_halfnormal_pdf but should be stdlib_base_dists_halfnormal_pdf.

Suggested change
* double y = stdlib_stats_base_dists_halfnormal_pdf( 2.0, 1.0 );
* double y = stdlib_base_dists_halfnormal_pdf( 2.0, 1.0 );

/**
* Returns a function for evaluating the probability density function (PDF) for a half-normal distribution.
*
* @param {NonNegativeNumber} sigma - scale parameter
Copy link
Member

Choose a reason for hiding this comment

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

Since sigma <= 0.0 returns NaN (not just sigma < 0), this should be {PositiveNumber} rather than {NonNegativeNumber}.

Suggested change
* @param {NonNegativeNumber} sigma - scale parameter
* @param {PositiveNumber} sigma - scale parameter

Copy link
Author

Choose a reason for hiding this comment

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

I’ve pushed two new commits to address the latest review feedback.
The first commit updates the documentation in README.md (including the copyright year and factory documentation), and the second commit fixes the remaining issues across the source and type files.

Kindly take another look when you have time, and please let me know if anything else needs to be adjusted.

---
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: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - 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
---
---
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: na
  - task: lint_repl_help
    status: na
  - 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: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - 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
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Review A pull request which needs code review. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants