# How to compute delta coefficients in mathematica

I want to get delta coefficients calculated. The definition can be found here. However, I don't know what to do with the minus indices. I have tried computing it in a slightly different way, namely:

delta[data_] :=
Block[
{
nData = Most[Prepend[data, 0]],
l = Length[data],
sum
},

N[Divide[
Plus @@ Times[data - nData, Range[l]],
Plus @@ (data ^ 2)
]]
]


My question is, whether there's other, easier way, of computing the delta coefficients?

• Would you please also provide an example dataset data. I presume that data is a vector of numbers but who knows? (Right, it is only you.) – Henrik Schumacher Mar 19 '19 at 10:39
• Hi, data is a vector of numbers. E.g. data ={0.0995563,-0.002073,-0.0541243,0.0662532,0.153794,0.0654639,0.158421,0.0362688,0.113608,0.156917,0.114574,0.0606766,0.0106301} – Curtis Mar 19 '19 at 10:46

You may use the following; it is concise, accurate, and fast.

delta1[data_] := With[{a = DeveloperToPackedArray[N[data]]},
ListConvolve[{1., -1.}, a, 1, 0.].Range[N[Length[a]]]/Total[a^2]
]

n = 10^5;
data = RandomReal[{-10, 10}, n];
r1 = delta[data]; // RepeatedTiming // First
r2 = delta1[data]; // RepeatedTiming // First
Max[Abs[r1 - r2]]


0.0887

0.0016

2.77556*10^-15

A general suggestion for performance: Better apply N before any actual computations are performed. (This rule holds only as long as the computations are free of any issues about severe precision loss.)

Should you decide to use cyclic differences, then replace

ListConvolve[{1., -1.}, a, 1, 0.]


by

ListConvolve[{1., -1.}, a, 1]
`