4
$\begingroup$

I've noticed this interesting study on metatrader 4; see the picture below:

enter image description here

Where the financial timeseries is "decomposed" (deconvolved?) into a series of periodic sinusoidal functions. In the picture above the red line is the sum of three vectors (the two green and dark green/black) sinusoidal function.

I've looked into FourierDST and spectral analytical functions of Mathematica but wasn't able to approach this problem, could you please provide me me some pointers?

$\endgroup$
  • $\begingroup$ There are several ways to do this. If you have the data displayed, please share it. Otherwise, I will take some similar financial time series from FinancialData. $\endgroup$ – Anton Antonov Apr 14 at 19:03
  • 2
    $\begingroup$ Might this be Empirical Mode Decomposition? See mathematica.stackexchange.com/questions/28724/… $\endgroup$ – mikado Apr 14 at 20:01
  • $\begingroup$ @mikado I tried the Empirical Mode Decomposition (EMD), did not get good results -- see my answer. (I have to say I did not try that hard to get better results with EMD...) $\endgroup$ – Anton Antonov Apr 15 at 13:51
4
$\begingroup$

Applying Empirical Mode Decomposition

As suggested by @mikado in the comments:

Might this be Empirical Mode Decomposition? See mathematica.stackexchange.com/questions/28724/….

Does not work that well over the data obtained in the next section. (A good suggestion nevertheless.)

enter image description here

Extracting Sin/Cos terms with Quantile Regression

Not a full answer, because I am not sure is this what OP wants. If it is then I will elaborate... First, I used data obtained with FinancialData:

ts2 = FinancialData["EUROX", {{2015, 7, 1}, {2017, 1, 1}, "Day"}];
DateListPlot[ts2, PlotTheme -> "Detailed", AspectRatio -> 1/4, ImageSize -> 800]

enter image description here

Next, I followed the procedure described in this answer.

I derived/selected these curves:

enter image description here

Here are the corresponding terms:

enter image description here

The code

Import["https://raw.githubusercontent.com/antononcube/\
MathematicaForPrediction/master/MonadicProgramming/\
MonadicQuantileRegression.m"]

ts2 = FinancialData["EUROX", {{2015, 7, 1}, {2017, 1, 1}, "Day"}];
DateListPlot[ts2, PlotTheme -> "Detailed", AspectRatio -> 1/4, 
 ImageSize -> 800]

ts2 = QRMonUnit[ts2]⟹QRMonTakeData;


bFuncs = Prepend[
   Flatten[Table[{Sin[b + h x], Cos[b + h x]}, {h, 1, 100, 1}, {b, 0, 1, 0.5}]], 1];
Length[bFuncs]

(* 601 *)

AbsoluteTiming[
 qrObj2 =
   QRMonUnit[ts2]⟹
    QRMonRescale⟹
    QRMonQuantileRegressionFit[bFuncs, 0.5]⟹
    QRMonSetRegressionFunctionsPlotOptions[PlotStyle -> Red]⟹
    QRMonPlot[PlotTheme -> "Detailed", AspectRatio -> 1/4, ImageSize -> Large];
 ]

(* {24.2521, Null} *)


qFunc2 = (qrObj2⟹QRMonTakeRegressionFunctions)[0.5][t];

terms = Cases[qFunc2, (f_?NumberQ*c_) :> {f, c}];
TakeLargestBy[terms, Abs@*First, 6]
ListPlot[terms[[All, 1]], PlotRange -> All, Filling -> Axis, PlotTheme -> "Scientific"]

(* {{15.1087, Sin[0. + t]}, {6.00884, Cos[1. + 3 t]}, {1.95579,
   Sin[0. + 5 t]}, {0.875732, Cos[1. + 9 t]}, {0.549675, 
  Sin[0. + 11 t]}, {0.311493, Cos[1. + 21 t]}} *)

Re-do the fit with a more informed basis

largestTerms = TakeLargestBy[terms, First, 7]

(* {{15.1087, Sin[0. + t]}, {6.00884, Cos[1. + 3 t]}, {1.95579, 
  Sin[0. + 5 t]}, {0.875732, Cos[1. + 9 t]}, {0.549675, 
  Sin[0. + 11 t]}, {0.311493, Cos[1. + 21 t]}, {0.218136, Sin[0. + 18 t]}} *)

terms = SortBy[terms, -Abs[#[[1]]] &];

spans = {Span[1, 3], Span[4, 8], Span[9, 50]};
res =
  MapThread[
   Function[{terms, span},
    QRMonUnit[ts2]⟹
     QRMonRescale[Axes -> {True, False}]⟹
     QRMonQuantileRegressionFit[Prepend[terms[[All, 2]], 1], 0.5]⟹
     QRMonSetRegressionFunctionsPlotOptions[PlotStyle -> Red]⟹
     QRMonPlot[PlotTheme -> "Detailed", AspectRatio -> 1/4, ImageSize -> Large, PlotLabel -> Row[{"span: ", span}]]⟹
     QRMonTakeRegressionFunctions
    ],
   {terms[[#]] & /@ spans, spans}
   ];

Block[{data = ts2, rData = qrObj2⟹QRMonTakeData, lines, rFunc},
 rFunc = Rescale[x, {0, 1}, MinMax[data[[All, 1]]]];
 lines = Outer[{rFunc /. x -> #2, #1[#2]} &, res[[All, 1]], rData[[All, 1]]];
 Show[{
   DateListPlot[data, PlotStyle -> {GrayLevel[0.3]}, 
    PlotTheme -> "Detailed"],
   ListLinePlot[lines, PlotRange -> All, 
    PlotStyle -> {Thickness[0.004]}, 
    PlotLegends -> Map[Row[{"terms: ", #}] &, spans]]},
  ImageSize -> 800, AspectRatio -> 1/4
 ]]

GridTableForm[
 Map[{#, Simplify[
     terms[[#]][[All, 2]] /. 
      t -> Rescale[t, MinMax[ts2[[All, 1]]], {0, 1}]]} &, spans], 
 TableHeadings -> {"span", "terms"}]
$\endgroup$
  • $\begingroup$ Spot on, please share the code. $\endgroup$ – Jerome Ibanes Apr 15 at 23:41
  • $\begingroup$ @JeromeIbanes Just posted the code -- tell me if you have any problems running it or you need more explanations. $\endgroup$ – Anton Antonov Apr 16 at 2:17
  • $\begingroup$ I seem to be getting quite different results, please review below: [eskimo.com/~jibanes/tmp/aa.pdf]. $\endgroup$ – Jerome Ibanes Apr 16 at 2:43
  • $\begingroup$ @JeromeIbanes The data is not ingested properly. Try to replace ts2 with a simple array with two numerical columns. Which Mathematica version are you using? $\endgroup$ – Anton Antonov Apr 16 at 7:50
  • $\begingroup$ Thank you Anton! $\endgroup$ – Jerome Ibanes Apr 17 at 0:51

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.