2022-06-14 15:27:50 +00:00
|
|
|
import { sortBy, sum } from 'lodash'
|
2022-06-13 20:51:15 +00:00
|
|
|
|
2022-04-09 23:10:58 +00:00
|
|
|
export const logInterpolation = (min: number, max: number, value: number) => {
|
|
|
|
if (value <= min) return 0
|
|
|
|
if (value >= max) return 1
|
|
|
|
|
|
|
|
return Math.log(value - min + 1) / Math.log(max - min + 1)
|
|
|
|
}
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
export function normpdf(x: number, mean = 0, variance = 1) {
|
|
|
|
if (variance === 0) {
|
|
|
|
return x === mean ? Infinity : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
Math.exp((-0.5 * Math.pow(x - mean, 2)) / variance) /
|
|
|
|
Math.sqrt(TAU * variance)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-13 20:51:15 +00:00
|
|
|
export const TAU = Math.PI * 2
|
|
|
|
|
2022-06-14 15:27:50 +00:00
|
|
|
export function median(xs: number[]) {
|
|
|
|
if (xs.length === 0) return NaN
|
2022-06-13 20:51:15 +00:00
|
|
|
|
2022-06-14 15:27:50 +00:00
|
|
|
const sorted = sortBy(xs, (x) => x)
|
2022-06-13 20:51:15 +00:00
|
|
|
const mid = Math.floor(sorted.length / 2)
|
|
|
|
if (sorted.length % 2 === 0) {
|
|
|
|
return (sorted[mid - 1] + sorted[mid]) / 2
|
|
|
|
}
|
|
|
|
return sorted[mid]
|
|
|
|
}
|
2022-06-14 15:27:50 +00:00
|
|
|
|
|
|
|
export function average(xs: number[]) {
|
|
|
|
return sum(xs) / xs.length
|
|
|
|
}
|
2022-07-10 18:05:44 +00:00
|
|
|
|
|
|
|
const EPSILON = 0.00000001
|
|
|
|
|
|
|
|
export function floatingEqual(a: number, b: number, epsilon = EPSILON) {
|
|
|
|
return Math.abs(a - b) < epsilon
|
|
|
|
}
|
|
|
|
|
|
|
|
export function floatingGreaterEqual(a: number, b: number, epsilon = EPSILON) {
|
|
|
|
return a + epsilon >= b
|
|
|
|
}
|
|
|
|
|
|
|
|
export function floatingLesserEqual(a: number, b: number, epsilon = EPSILON) {
|
|
|
|
return a - epsilon <= b
|
|
|
|
}
|