Peter Johnson

Results 186 comments of Peter Johnson

Weighted arithmetic mean ![Screenshot_20230719-210238_Chrome](https://github.com/delphidabbler/code-snippets/assets/5164283/6b3804bb-4cd0-4f08-b4af-766155aeda47) Source: [Wikipedia](https://en.m.wikipedia.org/wiki/Weighted_arithmetic_mean)

Useful helper functions for checking arrays for zero & negative entries: ```pascal // Check if all elements of a non-empty array are zero function ArrayIsZero(const A: array of Extended): Boolean;...

A related (helper) function is LogSumExp or LSE for a sequence x1,...,xn is is defined as the [logarithm](https://en.m.wikipedia.org/wiki/Logarithm) of the sum of the [exponentials](https://en.m.wikipedia.org/wiki/Exponential_function) of the arguments: LSE(x1,...,xn) = log(exp(x1)...

Strictly convex LogSumExp ![Screenshot_20230720-040354_Chrome](https://github.com/delphidabbler/code-snippets/assets/5164283/abdb8e20-7c37-4aca-aaed-85513a37676b) Source: [Wikipedia](https://en.m.wikipedia.org/wiki/LogSumExp) Since exp(0) = e^0 = 1, we have LSE(0, x1,...,xn) = log(exp(0) + exp(x1) + ... + exp(xn)) = log(1 + exp(x1) + ......

Quasi-arithmetic mean ![Screenshot_20230720-042100_Chrome](https://github.com/delphidabbler/code-snippets/assets/5164283/217776e5-c782-4d7d-a29f-1c6d140863df) Source: [Wikipedia](https://en.m.wikipedia.org/wiki/Quasi-arithmetic_mean) Note that certain functions f may be restricted to certain intervals of the real numbers, so if would be wise to pass a closure to...

Harmonic mean ![Screenshot_20230720-042913_Chrome](https://github.com/delphidabbler/code-snippets/assets/5164283/7d5612d9-e607-4c07-8ae8-6cc4d9d1f1bc) Source: [Wikipedia](https://en.m.wikipedia.org/wiki/Harmonic_mean)

Median Two overloads suggested, one real & one integer: ```pascal function Median(const A: array of Extended): Extended; overload; begin Assert(Length(A) > 0); // optimisations for array lengths 1 & 2...

Mode for countable sets: `ModeN` Obeys [rule](https://www.investopedia.com/terms/m/mode.asp) that if all items in a sequence of values are unique then there is no mode. The following function returns an empty array...

Mode for real numbers. Numbers are considered equal if they are within `Epsilon` of each other. If `Epsilon` is zero then Delphi chooses a suitable default. ```pascal function ModeR(const A:...

Mode with [probability function p(x)](https://www.radfordmathematics.com/probabilities-and-statistics/discrete-probability-distributions-discrete-random-variables/parameters-discrete-random-variables.html#:~:text=the%20Poisson%20Distribution.-,Mode,x%3F%2C%20reaches%20a%20maximum.) that gives weighted values for, say, integer x. We define probability function as a closure: ```pascal P: TFunc; ```