1
$\begingroup$

Can anyone show me how to select the states that have equal times-dates. For example, i have two time series. First that have 10 observations, say from 2014/10/1 to 2014/10/10, and second that have 7 observations say from 2014/10/2 to 2014/10/8.Now,I want to select the states from first and second series on equal times-dates. To be more concrete I will write down a exact problem: Two financial series of stock prices (Period 2007/1/1/ to 2014/10/20):

f1 = FinancialData["PA:ACA", "Return", {{2007, 1, 1}, {2014, 10, 20}}];
f2 = FinancialData["PA:AIR", "Return", {{2007, 1, 1}, {2014, 10, 20}}];

These series have 2031 and 2019 observations, respectively. I need to select the "states"-returns on the same "Times"-dates in order to estimate the covariance between stock returns.

$\endgroup$

2 Answers 2

1
$\begingroup$
f1 = FinancialData["PA:ACA", "Return", {{2007, 1, 1}, {2014, 10, 20}}];
f2 = FinancialData["PA:AIR", "Return", {{2007, 1, 1}, {2014, 10, 20}}];

f3 = Select[GatherBy[Join[f1, f2], First], Length@# == 2 &];

Or

f3b = Pick[#, Length@# == 2 & /@ #] &@GatherBy[Join[f1, f2], First];

f3 == f3b
(* True *)

The first 5 rows of f1, f2 and f3:

Row[{Labeled[Column@f1[[;; 5]], "f1", Top],
  Labeled[Column@f2[[;; 5]], "f2", Top],
  Labeled[Column@f3[[;; 5]], "f3", Top]}, Spacer[5]]

enter image description here

Or, to join the states for each date, you can use

    f12 = {#[[1, 1]], #[[1, 2]], #[[2, 2]]}&/@ Select[GatherBy[Join[f1, f2], First], Length@# == 2 &];
Labeled[Column@f12[[;; 5]], "f12", Top]

enter image description here

$\endgroup$
2
0
$\begingroup$

In MMA10 this can be done with TimeSeriesWindow

EDIT

Here base version of TimeSeriesWindow for MMA9

SelectBetweenDates[financialData_,{startDate_,endDate_}]:=Select[financialData,AbsoluteTime[startDate]<=AbsoluteTime[#[[1]]]<=AbsoluteTime[endDate]&];
SelectBetweenDates[f1,{{2014,10,2},{2014,10,8}}]
(*{{{2014,10,2},-0.0330789},{{2014,10,3},0.0263158},{{2014,10,6},-0.00940171},{{2014,10,7},-0.0163934},{{2014,10,8},-0.0131579}}*)

EDIT2 Select data between some dates

t3 = SelectBetweenDates[f1, {{2014, 10, 2}, {2014, 10, 8}}]~Join~SelectBetweenDates[f2, (*{{2014, 10, 2}, {2014, 10, 8}}];
{{{2014, 10, 2}, -0.0330789}, {{2014, 10, 3}, 
  0.0263158}, {{2014, 10, 6}, -0.00940171}, {{2014, 10, 
   7}, -0.0163934}, {{2014, 10, 8}, -0.0131579}, {{2014, 10, 
   2}, -0.0248523}, {{2014, 10, 3}, 0.0357217}, {{2014, 10, 6}, 
  0.0068576}, {{2014, 10, 7}, -0.026242}, {{2014, 10, 8}, -0.0211891}}*)

and then group this data by dates

GatherBy[t3, First]
(*{{{{2014,10,2},-0.0330789},{{2014,10,2},-0.0248523}},{{{2014,10,3},0.0263158},{{2014,10,3},0.0357217}},{{{2014,10,6},-0.00940171},{{2014,10,6},0.0068576}},{{{2014,10,7},-0.0163934},{{2014,10,7},-0.026242}},{{{2014,10,8},-0.0131579},{{2014,10,8},-0.0211891}}}*)

It's more general approach than proposed by kguler

$\endgroup$
4
  • $\begingroup$ Please, add any relevant code and an explanation. As it is currently written it is not a self-contained answer. $\endgroup$
    – Sektor
    Oct 22, 2014 at 12:02
  • $\begingroup$ @Branimir try this one $\endgroup$ Oct 22, 2014 at 12:30
  • $\begingroup$ Please don't start the symbol names with capitals in your answers. It's generally a bad practice as they may collide with future Mma System names.Thanks! $\endgroup$ Oct 22, 2014 at 14:34
  • $\begingroup$ Maybe I wasn't clear. I need one new variable say f3, that will have states form f1 and f2 on the same date. For example, if f1 have 10 observations form 2014/10/1 to 2014/10/10 and f2 have 8 observations in the same period (2014/10/1-2014/10/10), the new f3 variable should select the states from variables f1 and f2 (8 of them) that have same states-dates. This means that if say f2 doesn't have obs. on 6th and 8th October, f3 should have only returns on other dates (1,2,3,4,5,7,9,10) from f1 and all obs. from f2. Form of f3 {{{date}return f1},{{date} return f2}}. $\endgroup$
    – Branimir
    Oct 22, 2014 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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