S-PLUS : Copyright (c) 1988, 2007 Insightful Corp. S : Copyright Insightful Corp. Enterprise Developer Version 8.0.4 for Microsoft Windows : 200 7 Working data will be in C:\DDRIVE~1\MYDOCU~1\S-PLUS~1\Project1 This program will expire in 363 day(s). > #Illustration of S-Plus functions (Used on Day 5) > #getReturns > #autoCorTest > #Reminder of the "@" extractor and the "$" extractor > #for slotnames and names (for two flavors of S objects) > > module(finmetrics) S+FinMetrics Version 2.0.4 for Microsoft Windows : 2006 Warning messages: This library will expire in 363 day(s). in: attach(name, pos, section, purpose) > > #We load the finmetrics module which has a data set in it called singleIndex.dat > #We looked at it before, but lets look again. > > class(singleIndex.dat) [1] "timeSeries" > > slotNames(singleIndex.dat) [1] "data" "positions" "start.position" "end.position" "future.positions" "units" [7] "title" "documentation" "attributes" "fiscal.year.start" "type" > > #Lets put the MSFT prices into a vector. > > > msft.p=singleIndex.dat[, "MSFT"] > > msft.p Positions MSFT Jan 1990 1.2847 Feb 1990 1.3715 Mar 1990 1.5382 Apr 1990 1.6111 May 1990 2.0278 Jun 1990 2.1111 Jul 1990 1.8472 Aug 1990 1.7083 Sep 1990 1.7500 Oct 1990 1.7708 Nov 1990 2.0069 Dec 1990 2.0903 Jan 1991 2.7257 Feb 1991 2.8819 Mar 1991 2.9479 Apr 1991 2.7500 May 1991 3.0486 Jun 1991 2.8385 Jul 1991 3.0625 Aug 1991 3.5521 Sep 1991 3.7083 Oct 1991 3.9115 Nov 1991 4.0521 Dec 1991 4.6354 Jan 1992 5.0104 Feb 1992 5.1458 Mar 1992 4.9375 Apr 1992 4.5938 May 1992 5.0417 Jun 1992 4.3750 Jul 1992 4.5469 Aug 1992 4.6562 Sep 1992 5.0312 Oct 1992 5.5469 Nov 1992 5.8203 Positions MSFT Dec 1992 5.3359 Jan 1993 5.4062 Feb 1993 5.2109 Mar 1993 5.7812 Apr 1993 5.3438 May 1993 5.7891 Jun 1993 5.5000 Jul 1993 4.6250 Aug 1993 4.6953 Sep 1993 5.1562 Oct 1993 5.0078 Nov 1993 5.0000 Dec 1993 5.0391 Jan 1994 5.3203 Feb 1994 5.1562 Mar 1994 5.2969 Apr 1994 5.7812 May 1994 6.7188 Jun 1994 6.4531 Jul 1994 6.4375 Aug 1994 7.2656 Sep 1994 7.0156 Oct 1994 7.8750 Nov 1994 7.8594 Dec 1994 7.6406 Jan 1995 7.4219 Feb 1995 7.8750 Mar 1995 8.8906 Apr 1995 10.2188 May 1995 10.5859 Jun 1995 11.2969 Jul 1995 11.3125 Aug 1995 11.5625 Sep 1995 11.3125 Oct 1995 12.5000 Positions MSFT Nov 1995 10.8906 Dec 1995 10.9688 Jan 1996 11.5625 Feb 1996 12.3359 Mar 1996 12.8906 Apr 1996 14.1562 May 1996 14.8438 Jun 1996 15.0156 Jul 1996 14.7344 Aug 1996 15.3125 Sep 1996 16.4844 Oct 1996 17.1562 Nov 1996 19.6094 Dec 1996 20.6562 Jan 1997 25.5000 Feb 1997 24.3750 Mar 1997 22.9219 Apr 1997 30.3750 May 1997 31.0000 Jun 1997 31.5938 Jul 1997 35.3438 Aug 1997 33.0469 Sep 1997 33.0781 Oct 1997 32.5000 Nov 1997 35.3750 Dec 1997 32.3125 Jan 1998 37.2969 Feb 1998 42.3750 Mar 1998 44.7500 Apr 1998 45.0625 May 1998 42.4062 Jun 1998 54.1875 Jul 1998 54.9688 Aug 1998 47.9688 Sep 1998 55.0312 Positions MSFT Oct 1998 52.9375 Nov 1998 61.0000 Dec 1998 69.3438 Jan 1999 87.5000 Feb 1999 75.0625 Mar 1999 89.6250 Apr 1999 81.3125 May 1999 80.6875 Jun 1999 90.1875 Jul 1999 85.8125 Aug 1999 92.5625 Sep 1999 90.5625 Oct 1999 92.5625 Nov 1999 91.0469 Dec 1999 116.7500 Jan 2000 97.8750 Feb 2000 89.3750 Mar 2000 106.2500 Apr 2000 69.7500 May 2000 62.5625 Jun 2000 80.0000 Jul 2000 69.8125 Aug 2000 69.8125 Sep 2000 60.3125 Oct 2000 68.8750 Nov 2000 57.3750 Dec 2000 43.3750 Jan 2001 61.0625 > > #Let's look at these prices > > plot(msft.p) > > #Tools for getReturns() > #See discussion in ZW > > args(getReturns) function(x, type = "continuous", percentage = F, trim = T) NULL function(x, type = "continuous", percentage = F, trim = T) NULL > > msft.ret=getReturns(msft.p,percentage=T) > > #we looked at the plot before; let's look again. > > plot(msft.ret) > > #Now, when we look at this can we tell if this is just noise. > > help(autocorTest) > > #Now, lets give it a look. > > autocorTest(msft.ret) Test for Autocorrelation: Ljung-Box Null Hypothesis: no autocorrelation Test Statistics: MSFT Test Stat 27.9856 p.value 0.1406 Dist. under Null: chi-square with 21 degrees of freedom Total Observ.: 132 Test for Autocorrelation: Ljung-Box Null Hypothesis: no autocorrelation Test Statistics: MSFT Test Stat 27.9856 p.value 0.1406 Dist. under Null: chi-square with 21 degrees of freedom Total Observ.: 132 > > #This is non-significant. Irritating --- disappointing... maybe wrong? > > #Let's explore. First, let's look at price --- just for fun > > autocorTest(msft.p) Test for Autocorrelation: Ljung-Box Null Hypothesis: no autocorrelation Test Statistics: MSFT Test Stat 1664.404 p.value 0.000 Dist. under Null: chi-square with 21 degrees of freedom Total Observ.: 133 > > #Wow, insanely significant ... is this COOL .. or is it BOGUS? > > #What are the assumtions of the autocorTest? At a minimum, correlations have to > #make sense. Do correlations in prices make sense? NO! > #Why? Well, there is no chance they are stationary! > > #We have to look at returns ... they have a chance of being stationary. > > #So, OK, Let's look directly at the autocorrelations.. > > acf(msft.ret) Call: acf(x = msft.ret) Autocorrelation matrix: lag msft.ret 1 0 1.0000 2 1 -0.1975 3 2 -0.0623 4 3 0.1694 5 4 -0.0784 6 5 -0.0566 7 6 -0.0327 8 7 0.1813 9 8 0.0282 10 9 -0.0454 11 10 0.0640 12 11 0.0332 13 12 0.0646 14 13 -0.1233 15 14 0.1194 16 15 -0.0529 17 16 -0.1648 18 17 0.0304 19 18 0.0164 20 19 -0.0006 21 20 0.0053 22 21 0.0879 > > #There looks like there is something there > #Maybe if we don't that the defacult number of lags in our Ljung-Box test. > > autocorTest(msft.ret, lag.n=2) Test for Autocorrelation: Ljung-Box Null Hypothesis: no autocorrelation Test Statistics: MSFT Test Stat 5.7932 p.value 0.0552 Dist. under Null: chi-square with 2 degrees of freedom Total Observ.: 132 > > #Pretty close to significant. I'm not going to disregard the possibility that MSFT > #had an eploitable level of month-to-month correlation during this study period. > #There are lots of things to worry about, but we have a little nibble --- > #an indication of "signal." > > #Supponse we just look exactly at one month. > > autocorTest(msft.ret, lag.n=1) Test for Autocorrelation: Ljung-Box Null Hypothesis: no autocorrelation Test Statistics: MSFT Test Stat 5.2650 p.value 0.0218 Dist. under Null: chi-square with 1 degrees of freedom Total Observ.: 132 > > #Now, just to cover some ground for HW 2, lets use the $ extractor to understand the #information provided by acf() > > v=acf(msft.ret, plot=F) > > v Call: acf(x = msft.ret, plot = F) Autocorrelation matrix: lag msft.ret 1 0 1.0000 2 1 -0.1975 3 2 -0.0623 4 3 0.1694 5 4 -0.0784 6 5 -0.0566 7 6 -0.0327 8 7 0.1813 9 8 0.0282 10 9 -0.0454 11 10 0.0640 12 11 0.0332 13 12 0.0646 14 13 -0.1233 15 14 0.1194 16 15 -0.0529 17 16 -0.1648 18 17 0.0304 19 18 0.0164 20 19 -0.0006 21 20 0.0053 22 21 0.0879 > > names(v) [1] "acf" "lag" "n.used" "type" "series" "units" "call" "snames" > > w=v$acf > > w[1:5] [1] 1.00000000 -0.19746659 -0.06231104 0.16935329 -0.07843009 > > #You can use acf() and this way of extracting information from the value > #the acf returns in order to build the function for LB that you need for HW2. > > #Note that the first value of w is the lag 0 corrleation, which is 1. > #Note also from the definition of the LB statistic that it just contains > #terms with lag 1,2,...,k. > #Correct code for HW2 will need to take this into consideration. > > > >