I need a way to define local minima and maxima in time series data using Mathematica. It seems like it should be easy to do, but it is difficult. I posted this on MathForum, but thought that I could get extra eyes on it here.
You can find an article discussing the issue: http://www.cs.cmu.edu/~eugene/research/full/compress-series.pdf
I have tried this so far ...
Get and format some data:
data = FinancialData["SPY", {"May 1, 2006", "Jan. 21, 2011"}][[All, 2]];
data = data/First@data;
data = Transpose[{Range[Length@data], data}];
Define 2 functions:
First method:
findMinimaMaxima[data_, window_] := With[{k = window},
data[[k + Flatten@Position[Partition[data[[All, 2]], 2 k + 1, 1], x_List /; x[[k + 1]] < Min[Delete[x, k + 1]] || x[[k + 1]] > Max[Delete[x, k + 1]]]]]]
Now a different approach, although not as flexible:
findMinimaMaxima2[data_] := data[[Accumulate@(Length[
See what each function does. The first findMinimaMaxima2 []:
minmax = findMinimaMaxima2[data];
{Length@data, Length@minmax}
ListLinePlot@minmax
( ) 49% , .
. 2, , , :
minmax2 = findMinimaMaxima[data, 2];
{Length@data, Length@minmax2}
ListLinePlot@minmax2
, , 60:
minmax2 = findMinimaMaxima[data, 60];
ListLinePlot[{data, minmax2}]
.
findMinimaMaxima2 [] findMinimaMaxima [] ...
minmax3 = findMinimaMaxima2[minmax2];
ListLinePlot[{data, minmax2, minmax3}]
.
, , . , R (, ), , . :
findMinimaMaxima3[data_, R_] := Module[{d, n, positions},
d = data[[All, 2]];
n = Transpose[{data[[All, 1]], Rest@FoldList[If[(
n = Sign[Rest@n[[All, 2]] - Most@n[[All, 2]]];
positions = Flatten@Rest[Most[Position[n, Except[0]]]];
data[[positions]]
]
minmax4 = findMinimaMaxima3[data, 0.1];
ListLinePlot[{data, minmax4}]
- findMinimaMaxima2 []
ListLinePlot[{data, findMinimaMaxima2[minmax4]}]
, , , R - , . R , :
minmax4 = findMinimaMaxima3[data, 0.15];
ListLinePlot[{data, minmax4}]
, . . . / R , ( , ).
- - ?
. , - .
,
Jagra