|
| 1 | +/*****************************************************************************************************************/ |
| 2 | + |
| 3 | +// @author Michael Roberts <michael@observerly.com> |
| 4 | +// @package @observerly/fits |
| 5 | +// @license Copyright © 2021-2025 observerly |
| 6 | + |
| 7 | +/*****************************************************************************************************************/ |
| 8 | + |
| 9 | +import { describe, expect, it } from 'vitest' |
| 10 | + |
| 11 | +import { type Point, performLinearRegression } from '../regression' |
| 12 | + |
| 13 | +/*****************************************************************************************************************/ |
| 14 | + |
| 15 | +describe('performLinearRegression', () => { |
| 16 | + it('should correctly compute slope and intercept for a simple linear relationship', () => { |
| 17 | + const points: Point[] = [ |
| 18 | + { x: 0, y: 1 }, |
| 19 | + { x: 1, y: 3 }, |
| 20 | + { x: 2, y: 5 }, |
| 21 | + { x: 3, y: 7 } |
| 22 | + ] |
| 23 | + |
| 24 | + const { m, c } = performLinearRegression(points) |
| 25 | + |
| 26 | + expect(m).toBeCloseTo(2) |
| 27 | + expect(c).toBeCloseTo(1) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should handle floating point values accurately', () => { |
| 31 | + const points: Point[] = [ |
| 32 | + { x: 0.5, y: 2.1 }, |
| 33 | + { x: 1.5, y: 3.9 }, |
| 34 | + { x: 2.5, y: 5.8 }, |
| 35 | + { x: 3.5, y: 7.7 } |
| 36 | + ] |
| 37 | + |
| 38 | + const { m, c } = performLinearRegression(points) |
| 39 | + |
| 40 | + expect(m).toBeCloseTo(1.867, 2) |
| 41 | + expect(c).toBeCloseTo(1.135, 2) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should correctly compute slope and intercept for a vertical line-like data', () => { |
| 45 | + const points: Point[] = [ |
| 46 | + { x: 1, y: 2 }, |
| 47 | + { x: 2, y: 4 }, |
| 48 | + { x: 3, y: 6 }, |
| 49 | + { x: 4, y: 8 } |
| 50 | + ] |
| 51 | + |
| 52 | + const { m, c } = performLinearRegression(points) |
| 53 | + expect(m).toBeCloseTo(2) |
| 54 | + expect(c).toBeCloseTo(0) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should handle negative slopes correctly', () => { |
| 58 | + const points: Point[] = [ |
| 59 | + { x: 0, y: 10 }, |
| 60 | + { x: 1, y: 8 }, |
| 61 | + { x: 2, y: 6 }, |
| 62 | + { x: 3, y: 4 } |
| 63 | + ] |
| 64 | + |
| 65 | + const { m, c } = performLinearRegression(points) |
| 66 | + expect(m).toBeCloseTo(-2) |
| 67 | + expect(c).toBeCloseTo(10) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should handle points with zero variance in y', () => { |
| 71 | + const points: Point[] = [ |
| 72 | + { x: 0, y: 5 }, |
| 73 | + { x: 1, y: 5 }, |
| 74 | + { x: 2, y: 5 }, |
| 75 | + { x: 3, y: 5 } |
| 76 | + ] |
| 77 | + |
| 78 | + const { m, c } = performLinearRegression(points) |
| 79 | + expect(m).toBeCloseTo(0) |
| 80 | + expect(c).toBeCloseTo(5) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should compute correct values for a random set of points', () => { |
| 84 | + const points: Point[] = [ |
| 85 | + { x: 1, y: 2 }, |
| 86 | + { x: 2, y: 3 }, |
| 87 | + { x: 3, y: 5 }, |
| 88 | + { x: 4, y: 4 }, |
| 89 | + { x: 5, y: 6 } |
| 90 | + ] |
| 91 | + |
| 92 | + const { m, c } = performLinearRegression(points) |
| 93 | + expect(m).toBeCloseTo(0.9) |
| 94 | + expect(c).toBeCloseTo(1.3) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should throw an error when no points are provided', () => { |
| 98 | + const points: Point[] = [] |
| 99 | + |
| 100 | + expect(() => performLinearRegression(points)).toThrow( |
| 101 | + 'No valid points provided for linear regression.' |
| 102 | + ) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should throw an error when all x values are the same', () => { |
| 106 | + const points: Point[] = [ |
| 107 | + { x: 2, y: 3 }, |
| 108 | + { x: 2, y: 4 }, |
| 109 | + { x: 2, y: 5 } |
| 110 | + ] |
| 111 | + |
| 112 | + expect(() => performLinearRegression(points)).toThrow( |
| 113 | + 'Denominator is zero. Cannot compute linear regression.' |
| 114 | + ) |
| 115 | + }) |
| 116 | +}) |
| 117 | + |
| 118 | +/*****************************************************************************************************************/ |
0 commit comments