I want to program the chi-square between two distribution Xu and Vu with length N. This function is defined as below: Xu and Vu are real and positive.
chiDistance[U_, V_] := Module[{vecs, vselect}, (
(*delete the {0,0} occurrence to avoid the division by zero*)
vecs = Join[Transpose[{U}], Transpose[{V}], 2];
vselect = Select[vecs, # != {0., 0.} &];
(1/2) Total[(vselect[[All, 1]] -
vselect[[All, 2]])^2/(vselect[[All, 1]] +
vselect[[All, 2]])]
)]
This function take
0.047625 when N=40000
When I replaced Select[]
by DeleteCases[]
, I got the same complexity.