Skip to content

Implement Skewness Calculation with Edge Case Handling#91

Merged
montanaflynn merged 4 commits intomontanaflynn:masterfrom
ANANDHA007:skewness
Mar 24, 2026
Merged

Implement Skewness Calculation with Edge Case Handling#91
montanaflynn merged 4 commits intomontanaflynn:masterfrom
ANANDHA007:skewness

Conversation

@ANANDHA007
Copy link
Copy Markdown
Contributor

This PR adds a Skewness function for float64 datasets, calculating the third central moment normalized by standard deviation cubed. It handles zero-variance datasets and validates minimum input length. Unit tests cover symmetric, right-skewed, and left-skewed distributions, complementing the library’s existing descriptive statistics.

@ANANDHA007
Copy link
Copy Markdown
Contributor Author

Hi @montanaflynn added the functional logic to calculate the Skewness

ANANDHA007 and others added 3 commits January 7, 2026 10:28
- Fix skewness formula (was off by factor of n)
- Add PopulationSkewness and SampleSkewness to match library pattern
- Use package error variables (ErrEmptyInput) instead of errors.New
- Return math.NaN() on error to match other functions
- Unexport helper functions (Differences, PowerofN) and inline logic
- Use package stats_test and table-driven tests with scipy reference values
- Add doc comments on exported functions
@montanaflynn
Copy link
Copy Markdown
Owner

Thanks for adding skewness — great addition to the library! I pushed a commit with some adjustments to align with the existing codebase conventions:

Formula fix:

  • The original formula computed sumOfCubes / stdDevCubed, but sumOfCubes is the raw sum Σ(xi - μ)³ while stdDevCubed was derived from variance = Σ(xi - μ)² / n (already divided by n). This made the result off by a factor of n. Fixed to (sumOfCubes / n) / stdDevCubed.

Added population/sample variants:

  • Skewness → alias for PopulationSkewness (matches scipy.stats.skew(bias=True))
  • PopulationSkewness → third central moment / σ³
  • SampleSkewness → adjusted Fisher-Pearson coefficient with bias correction: g1 * sqrt(n*(n-1)) / (n-2) (matches scipy.stats.skew(bias=False))

This follows the same pattern as Variance/PopulationVariance/SampleVariance and StandardDeviation/StandardDeviationPopulation/StandardDeviationSample.

Convention fixes:

  • Use ErrEmptyInput instead of errors.New(...) — matches the package's error pattern
  • Return math.NaN() on error instead of 0.0 — matches every other function in the library
  • Unexported Differences() and PowerofN() helpers and inlined the logic — keeps the public API clean
  • Switched tests to package stats_test and table-driven format with exact reference values verified against scipy
  • Added doc comments on exported functions

All tests pass against scipy reference values for both population and sample skewness across 5 datasets.

@montanaflynn montanaflynn merged commit 01caa59 into montanaflynn:master Mar 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants