1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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.) $\endgroup$ – Henrik Schumacher Mar 19 at 10:39
  • 1
    $\begingroup$ 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} $\endgroup$ – Curtis Mar 19 at 10:46
2
$\begingroup$

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

delta1[data_] := With[{a = Developer`ToPackedArray[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]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.