Skip to content

Conversation

@bhargava-d16
Copy link
Contributor


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

Progresses #9416

Description

What is the purpose of this pull request?

This pull request:

  • This PR adds implementation of entropy calculation for half-normal distribution.

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

I consulted ChatGPT to verify mathematical formulation of half-normal entropy function 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: 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: 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 Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Jan 14, 2026
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Jan 14, 2026

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/halfnormal/entropy $\color{green}164/164$
$\color{green}+0.00%$
$\color{green}8/8$
$\color{green}+0.00%$
$\color{green}2/2$
$\color{green}+0.00%$
$\color{green}164/164$
$\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 this PR! Left initial comment.

// returns ~2.6238370975295946
```

If provided `sigma < 0`, the function returns `NaN`.
Copy link
Member

Choose a reason for hiding this comment

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

The documentation says sigma < 0 but the implementation in lib/main.js checks sigma <= 0.0. This should be updated to sigma <= 0 to match the implementation behavior (sigma=0 also returns NaN).

Note: docs/repl.txt correctly uses σ ≤ 0.


```c
double out = stdlib_base_dists_halfnormal_entropy( 1.0 );
// returns ~0.726
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 output value ~0.726 is incorrect. For sigma=1.0, the entropy should be ~1.0143991850954939 (as shown correctly in the JavaScript examples above).

double out = stdlib_base_dists_halfnormal_entropy( 1.0 );
// returns ~1.014

*
* ## Notes
*
* - If provided `σ < 0`, the function returns `NaN`.
Copy link
Member

Choose a reason for hiding this comment

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

The documentation says σ < 0 but the implementation checks σ <= 0. This should be updated to σ ≤ 0 to match the implementation behavior (sigma=0 also returns NaN).

Note: docs/repl.txt correctly uses σ ≤ 0.

Comment on lines 60 to 64
var y = entropy( 1.0 );
// returns ~1.0143991850954939

y = entropy( 5.0 );
// returns ~2.6238370975295946
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var y = entropy( 1.0 );
// returns ~1.0143991850954939
y = entropy( 5.0 );
// returns ~2.6238370975295946
var y = entropy( 1.0 );
// returns ~1.014
y = entropy( 5.0 );
// returns ~2.624

@Planeshifter Planeshifter changed the title feat: add stats/base/dists/halfnormal/entropy feat: add stats/base/dists/halfnormal/entropy Jan 15, 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: 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: passed
  - task: lint_license_headers
    status: passed
---
@@ -0,0 +1,26 @@
{{alias}}( σ )
Copy link
Member

Choose a reason for hiding this comment

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

The file should start with a leading blank line before {{alias}}.

Suggested change
{{alias}}( σ )
{{alias}}( σ )

Comment on lines 32 to 37
* var v = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.6238370975295946
Copy link
Member

Choose a reason for hiding this comment

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

Same as the repl.txt - the example return values should use short approximations instead of full precision. Reference packages (e.g., rayleigh/entropy) use // returns ~3.139 rather than the full value.

Suggested change
* var v = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.6238370975295946
* var v = entropy( 1.0 );
* // returns ~1.014
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.624

for ( i = 0; i < expected.length; i++ ) {
y = entropy( sigma[ i ] );
if ( y === expected[ i ] ) {
t.strictEqual(y, expected[ i ], 'sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i]);
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 t.strictEqual( - should be t.strictEqual( y, with a space after the opening parenthesis, matching stdlib's spacing convention.

Suggested change
t.strictEqual(y, expected[ i ], 'sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i]);
t.strictEqual( y, expected[ i ], 'sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i] );

} else {
delta = abs( y - expected[ i ] );
tol = 40.0 * EPS * abs( expected[ i ] );
t.ok(delta <= tol, 'within tolerance. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.');
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 t.ok( - should be t.ok( delta with a space after the opening parenthesis.

Suggested change
t.ok(delta <= tol, 'within tolerance. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.');
t.ok( delta <= tol, 'within tolerance. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );

} else {
delta = abs( y - expected[ i ] );
tol = 40.0 * EPS * abs( expected[ i ] );
t.ok(delta <= tol, 'within tolerance. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.');
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 t.ok( - should be t.ok( delta with a space after the opening parenthesis.

Suggested change
t.ok(delta <= tol, 'within tolerance. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.');
t.ok( delta <= tol, 'within tolerance. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );

Comment on lines 20 to 23
> var v = {{alias}}( 1.0 )
~1.0143991850954939
> v = {{alias}}( 5.0 )
~2.6238370975295946
Copy link
Member

Choose a reason for hiding this comment

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

The example output values should use short approximations instead of full precision. Looking at other stdlib repl.txt files (e.g., rayleigh/entropy), they use formats like ~3.34 and ~2.446 rather than ~1.0143991850954939.

Suggested change
> var v = {{alias}}( 1.0 )
~1.0143991850954939
> v = {{alias}}( 5.0 )
~2.6238370975295946
> var v = {{alias}}( 1.0 )
~1.014
> v = {{alias}}( 5.0 )
~2.624

Comment on lines 29 to 33
* var v = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* v = entropy( 5.0 );
* // returns ~2.6238370975295946
Copy link
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 = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* v = entropy( 5.0 );
* // returns ~2.6238370975295946
* var v = entropy( 1.0 );
* // returns ~1.014
*
* v = entropy( 5.0 );
* // returns ~2.624

Comment on lines 38 to 43
* var v = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.6238370975295946
Copy link
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 = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.6238370975295946
* var v = entropy( 1.0 );
* // returns ~1.014
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.624

Comment on lines 36 to 41
* var v = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.6238370975295946
Copy link
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 = entropy( 1.0 );
* // returns ~1.0143991850954939
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.6238370975295946
* var v = entropy( 1.0 );
* // returns ~1.014
*
* @example
* var v = entropy( 5.0 );
* // returns ~2.624

Comment on lines 32 to 33
* double y = stdlib_base_dists_halfnormal_entropy( 1.0 );
* // returns ~1.0143991850954939
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* double y = stdlib_base_dists_halfnormal_entropy( 1.0 );
* // returns ~1.0143991850954939
* double y = stdlib_base_dists_halfnormal_entropy( 1.0 );
* // returns ~1.014

---
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: 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
---
---
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: 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
---
@Neerajpathak07 Neerajpathak07 added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Jan 16, 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: 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: missing_dependencies
  - 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
---
@bhargava-d16
Copy link
Contributor Author

Changes applied as requested @Planeshifter. Thanks for the review !

@bhargava-d16
Copy link
Contributor Author

@Planeshifter I have made the requested changes.Please let me know if it needs further improvements

Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
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.

Let's land this one; thank you!

@Planeshifter Planeshifter added the Ready To Merge A pull request which is ready to be merged. label Jan 22, 2026
@stdlib-bot stdlib-bot removed the Needs Changes Pull request which needs changes before being merged. label Jan 22, 2026
@stdlib-bot
Copy link
Contributor

PR Commit Message

feat: add `stats/base/dists/halfnormal/entropy`

PR-URL: https://github.com/stdlib-js/stdlib/pull/9753
Ref: https://github.com/stdlib-js/stdlib/issues/9416

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>

Please review the above commit message and make any necessary adjustments.

@Planeshifter Planeshifter merged commit 9bf5e56 into stdlib-js:develop Jan 22, 2026
31 checks passed
@stdlib-bot stdlib-bot removed the Ready To Merge A pull request which is ready to be merged. label Jan 22, 2026
ivishal-g pushed a commit to ivishal-g/stdlib-fork that referenced this pull request Jan 22, 2026
PR-URL: stdlib-js#9753
Ref: stdlib-js#9416

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
ivishal-g pushed a commit to ivishal-g/stdlib-fork that referenced this pull request Jan 22, 2026
PR-URL: stdlib-js#9753
Ref: stdlib-js#9416

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
if ( isnan( sigma ) || sigma <= 0.0 ) {
return NaN;
}
return 0.5 + ln( sigma ) + ln( SQRT_HALF_PI ) + ( 0.5 * GAMMA );
Copy link
Member

Choose a reason for hiding this comment

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

@bhargava-d16 Can you explain how you derived this formula? It doesn't match Wikipedia (ref: https://en.wikipedia.org/wiki/Half-normal_distribution), so I am curious as to its derivation.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

And in fact, when tested against SciPy, all tests fail.

Copy link
Contributor Author

@bhargava-d16 bhargava-d16 Jan 24, 2026

Choose a reason for hiding this comment

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

The initial formulation followed the structure used for related scale distributions (e.g. Rayleigh), where the entropy contains an E[ln X] and thus a gamma/2 contribution.

However, explicitly expanding the half-normal log-density shows that the entropy depends only on a second-moment term E[X **2] and no logarithmic expectation remains after simplification.

I will update the implementation to use the simplified closed-form expression 0.5 + ln(sigma) + ln(sqrt(pi/2))

Please let me know if there are any additional changes or checks you’d recommend.

Copy link
Member

Choose a reason for hiding this comment

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

@bhargava-d16 Sorry. I am not sure I follow. Would you mind providing the mathematical derivation of the entropy for a half-normal distribution?

Copy link
Member

Choose a reason for hiding this comment

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

@bhargava-d16 Please do not bother submitting a PR, as I went through and fixed the entire implementation. The implementation now returns the same results as SciPy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Starting from the definition of differential entropy
(Ref: https://en.wikipedia.org/wiki/Differential_entropy),

H(X) = -∫ f(x) ln f(x) dx,

where the integral is over [0, infinity) since the half-normal distribution
is supported on x ≥ 0.

For a half-normal distribution with scale parameter sigma > 0, the
probability density function is
(Ref: https://en.wikipedia.org/wiki/Half-normal_distribution)

f(x) = sqrt(2)/(sigmasqrt(pi)) * exp(-x^2/(2sigma^2)), x ≥ 0.

Taking the logarithm on both sides,

ln f(x) = ln(sqrt(2)/(sigmasqrt(pi))) - x^2/(2sigma^2).

Substituting into the entropy definition and splitting terms gives

H(X) = -ln(sqrt(2)/(sigmasqrt(pi))) + E[X^2]/(2sigma^2).

For a half-normal random variable X = |Z| with
Z ~ N(0, sigma^2), we have X^2 = Z^2, and hence

E[X^2] = Var(Z) + (E[Z])^2 = sigma^2.

Therefore the second term evaluates to 1/2, and combining terms yields

H(X) = 0.5 + ln(sigma) + 0.5*ln(pi/2)
= 0.5 + ln(sigma) + ln(sqrt(pi/2))

Copy link
Contributor Author

@bhargava-d16 bhargava-d16 Jan 24, 2026

Choose a reason for hiding this comment

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

Thank you for the implementation.Out of curiosity, purely for learning purposes, whether the derivation I shared earlier was correct or if there were any differences with the approach you ended with.

@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. review: 1 labels Jan 24, 2026
nakul-krishnakumar pushed a commit to nakul-krishnakumar/stdlib that referenced this pull request Jan 25, 2026
PR-URL: stdlib-js#9753
Ref: stdlib-js#9416

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
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. review: 1 Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants