(values: Array<number | null>)
| 174 | * @returns LineChartAnalysis object with statistics and qualitative interpretation |
| 175 | */ |
| 176 | export function computeLineChartAnalysis(values: Array<number | null>): LineChartAnalysis { |
| 177 | const indexedValues: Array<{ value: number; index: number }> = [] |
| 178 | |
| 179 | for (let i = 0; i < values.length; i += 1) { |
| 180 | const v = values[i] |
| 181 | if (v === null || v === undefined) continue |
| 182 | indexedValues.push({ value: v, index: i }) |
| 183 | } |
| 184 | |
| 185 | const n = indexedValues.length |
| 186 | |
| 187 | if (n === 0) { |
| 188 | return { |
| 189 | mean: 0, |
| 190 | standardDeviation: 0, |
| 191 | coefficientOfVariation: null, |
| 192 | slope: 0, |
| 193 | rSquared: null, |
| 194 | interpretation: { |
| 195 | volatility: 'undefined', |
| 196 | trend: 'undefined', |
| 197 | }, |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (n === 1) { |
| 202 | const onlyValue = indexedValues[0]?.value ?? 0 |
| 203 | return { |
| 204 | mean: onlyValue, |
| 205 | standardDeviation: 0, |
| 206 | coefficientOfVariation: null, |
| 207 | slope: 0, |
| 208 | rSquared: null, |
| 209 | interpretation: { |
| 210 | volatility: 'very_stable', |
| 211 | trend: 'none', |
| 212 | }, |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | let _sum = 0 |
| 217 | for (const entry of indexedValues) { |
| 218 | _sum += entry.value |
| 219 | } |
| 220 | const mean = _sum / n |
| 221 | |
| 222 | let varianceSum = 0 |
| 223 | for (const entry of indexedValues) { |
| 224 | const diff = entry.value - mean |
| 225 | varianceSum += diff * diff |
| 226 | } |
| 227 | const standardDeviation = Math.sqrt(varianceSum / n) |
| 228 | |
| 229 | const coefficientOfVariation = mean === 0 ? null : standardDeviation / mean |
| 230 | |
| 231 | const originalYValues: number[] = [] |
| 232 | for (const entry of indexedValues) { |
| 233 | originalYValues.push(entry.value) |
no test coverage detected