4
$\begingroup$

I've the following time series in smoothedData:

tmp = WolframAlpha[
   "EUR vs USD in 2020", {{"Result", 1}, "ComputableData"}];
data = TimeSeries@tmp[[1]];
smoothedData = MovingAverage[data, #] & /@ {10, 50};
DateListPlot[smoothedData, PlotTheme -> "Marketing"]

enter image description here

How can I find the intersection points between those two time series?

I don't know if it's better to represent them as interpolating functions or as time series and in this case the points should be the samples that are next to the intersection (i.e. where the difference of time series change sign between a timestamp and the next one).

What's the best solution?

$\endgroup$
1
  • $\begingroup$ Just look for points where they are equal or when the difference changes sign. $\endgroup$
    – A.G.
    Apr 24 '21 at 20:04
6
$\begingroup$

There's a long winded way with TimeSeriesThread or interpolation and subtracting the curves to find zero crossings, however there's also a shortcut where you can extract the lines from the plot and immediately get the intersections:

tmp = WolframAlpha["EUR vs USD in 2020", {{"Result", 1}, "ComputableData"}];
data = TimeSeries@tmp[[1]];
smoothedData = MovingAverage[data, #] & /@ {10, 50};
plot = DateListPlot[smoothedData];
points = RegionIntersection @@ Cases[plot, Line[_], Infinity];

plot = DateListPlot[smoothedData, PlotTheme -> "Marketing", 
  Epilog -> {White, PointSize[Large], points}]

plot with crossings

... and if you're okay with using undocumented functions you could alternatively write:

points = Point[Graphics`Mesh`FindIntersections[plot]]
$\endgroup$
1
  • $\begingroup$ Thanks a lot this is what I needed! $\endgroup$
    – Jepessen
    Apr 25 '21 at 8:09
5
$\begingroup$

Find where the differences between two time series change sign by subtracting the second series from the first. The transitions show the dates where the two series intersect.

transitions = Sign[smoothedData[[1]] - smoothedData[[2]]];
DateListPlot[transitions]

time series differences

None of the intersecting values are equal (none of the differences are zero).

Position[transitions["Values"], 0]
(* {} *)

Find the dates before and after each transition value. Get the values for each date from the smoothed values. Plot the dates with the smoothed data plot.

pos = Sort@Flatten[{#, # + 1}] &@
   Flatten[Position[Abs[Differences[transitions]["Values"]], 2]];
dates = transitions["Dates"][[pos]];
dateValues = Transpose[{dates, smoothedData[[1]][dates]}];

Show[DateListPlot[smoothedData, PlotTheme -> "Marketing"],
 DateListPlot[Style[dateValues, Yellow], Joined -> False]]

time series and intersections

Here are the dates before and after each intersection between the time series.

TableForm[Partition[{DateString[#1, "Date"], #2} & @@@ dateValues, 2]]

dates and values

$\endgroup$
1
  • $\begingroup$ Thanks, this is a really nice solution. $\endgroup$
    – Jepessen
    Apr 25 '21 at 17:09

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.