Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mean() to stats module in @observerly/fits #48

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/stats/__tests__/mean.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*****************************************************************************************************************/

// @author Michael Roberts <[email protected]>
// @package @observerly/fits
// @license Copyright © 2021-2025 observerly

/*****************************************************************************************************************/

import { describe, expect, it } from 'vitest'

import { mean } from '../mean'

/*****************************************************************************************************************/

describe('mean', () => {
it('should calculate the mean of an array with positive numbers', () => {
const input = [1, 2, 3, 4, 5]
const result = mean(input)
expect(result).toBe(3) // (1 + 2 + 3 + 4 + 5) / 5 = 3
})

it('should calculate the mean of an array with negative numbers', () => {
const input = [-1, -2, -3, -4, -5]
const result = mean(input)
expect(result).toBe(-3) // (-1 + -2 + -3 + -4 + -5) / 5 = -3
})

it('should calculate the mean of an array with mixed positive and negative numbers', () => {
const input = [-3, -2, -1, 1, 2, 3]
const result = mean(input)
expect(result).toBe(0) // (-3 + -2 + -1 + 1 + 2 + 3) / 6 = 0
})

it('should calculate the mean of an array with a single element', () => {
const input = [42]
const result = mean(input)
expect(result).toBe(42) // Mean of a single element is the element itself
})

it('should handle an empty array gracefully', () => {
const input: number[] = []
expect(mean(input)).toBeNaN()
})

it('should calculate the mean of an array with floating-point numbers', () => {
const input = [1.5, 2.5, 3.5, 4.5]
const result = mean(input)
expect(result).toBe(3) // (1.5 + 2.5 + 3.5 + 4.5) / 4 = 3
})

it('should handle arrays with repeated values', () => {
const input = [2, 2, 2, 2]
const result = mean(input)
expect(result).toBe(2) // Mean of identical values is the value itself
})

it('should not mutate the original array', () => {
const input = [1, 2, 3, 4]
const original = [...input]
mean(input)
expect(input).toEqual(original)
})
})

/*****************************************************************************************************************/
1 change: 1 addition & 0 deletions src/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/*****************************************************************************************************************/

export { mean } from './mean'
export { median } from './median'

/*****************************************************************************************************************/
13 changes: 13 additions & 0 deletions src/stats/mean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*****************************************************************************************************************/

// @author Michael Roberts <[email protected]>
// @package @observerly/fits
// @license Copyright © 2021-2025 observerly

/*****************************************************************************************************************/

export const mean = (arr: number[]): number => {
return arr.reduce((a, b) => a + b, 0) / arr.length
}

/*****************************************************************************************************************/
Loading