Message Boards Message Boards

How to implement this Sample Entropy Algorithm?

Hello, I am very new to mathematica and also new to this community. I have a problem given below.I want to implement a Sample Entropy Algorithm click here ,and here ,and here in Mathematica.I wrote the code , but do not know is there correct.

 data = Table[Sin[i], {i, 1, 100, .2}];f[n_] := data[[n ;; n]]; 

 d[i_, j_, m_, n_] := Max[Table[Abs[f[i + k] - f[j + k]], {k, 0, m - 1}]];
 nm[i_, r_, m_, n_] := (1/(n - m - 1))* Sum[If[d[i, j, m, n] < r, 1, 0], {j, 1, n - m}];
 nm1[i_, r_, m_, n_] := (1/(n - m - 1))*Sum[If[d[i, j, m + 1, n] < r, 1, 0], {j, 1, n - m}];
 B[r_, m_, n_] := (1/(n - m))*Sum[nm[i, r, m, n], {i, 1, n - m}];
 A[r_, m_, n_] := (1/(n - m))*Sum[nm1[i, r, m, n], {i, 1, n - m}];
 SampEn[n_, m_, r_] := -Log[A[r, m, n]/B[r, m, n]]; 
 SampEn[Length[data], 2, 0.2 StandardDeviation[data]] // N

with this document click here SampEn= 0.2359326 ,but with my code is SampEn=0.230084.

I do not know at which point I made a mistake.Any help will be appreciated!

POSTED BY: Mariusz Iwaniuk
2 Replies

I found code on this site https://mathematica.stackexchange.com.Works for MMA 11.2.

 data = Table[Sin[i], {i, 1, 100, .2}];

 ClearAll[sampleEnt2]
 sampleEnt2[data_, ws_, t_] :=  Module[{nF1, nF2, diff1, diff2, va1, va2},
  va1 = Partition[data, ws, 1];
  va2 = Partition[data, 1 + ws, 1];
  nF1 = Nearest[va1 -> Automatic, DistanceFunction -> ChessboardDistance];
  nF2 = Nearest[va2 -> Automatic, DistanceFunction -> ChessboardDistance];
  diff1 = Total[(Length /@ nF1[va1, {All, t}])] - Length[va1];
  diff2 = Total[(Length /@ nF2[va2, {All, t}])] - Length[va2];
  -Log[N@(diff2/diff1)]
  ]


   m = 2;
   r = 0.2*StandardDeviation[data];
   sampleEnt2[data, m, r] // AbsoluteTiming
   (* {0.00581366, 0.230651} *)
POSTED BY: Mariusz Iwaniuk

Did you figure out what went wrong in the code? Or was it an error in the document?

POSTED BY: Sam Carrettie
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract