1 Introduction to Time Series

First we should clear the environment and download financial data from Yahoo Finance

We star clearing our R environment

rm(list=ls())
# To avoid scientific notation for numbers: 
options(scipen=999)

I will load the quantmod package

library(quantmod)
## Warning: package 'quantmod' was built under R version 4.0.3
## Loading required package: xts
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.0.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

2 Downloading time-series financial prices

getSymbols(c("^MXX", "^GSPC"), from="2011-01-01", periodicity = "monthly", src = "yahoo")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "^MXX"  "^GSPC"

2.1 Data Management

I would integrate both datasets into one:

# We merge the datasets into a new R object called prices:
prices = merge(MXX,GSPC)
# We only keep the adjusted price columns:
prices = Ad(prices)
# We rename the columns with simpler names:
names(prices) = c("MXX","GSPC")

2.2 Visualization of time series

Now i will be able to see how the S&P500 is behaving over time

chartSeries(MXX, theme=("white"))

chartSeries(GSPC, theme=("white"))

To check the class of my R objects

class(GSPC)
## [1] "xts" "zoo"

“xts”= extensible time series “zoo”= is time series , to easy manipulate the data.

2.2.1 Question 1

WHAT YOU CAN SAY ABOUT THE TREND OF BOTH MARKET INDEXES? IS IT CONSTANTLY GROWING, OR DECLINING, OR THERE IS NO CLEAR TREND? BRIEFLY EXPLAIN

2.2.1.1 Answer 1

WE CAN SEE THAT IN A GENERAL VIEW, THE S&P500 IS CONSTANTLY GROWING, ALTHOUGH THERE IS A SMALL FALL IN 2020, I AM ALMOST SURE THAT THIS WAS CAUSED BY THE PANDEMIC, ON THE OTHER SIDE, THE MEXICAN MARKET IS REALLY VOLATILE IT PRESENTS UP AND DOWNS BY THE PASS OF THE YEARS, ALSO WE CAN SEE THE FALL IN 2020 CAUSED BY THE PANDEMIC, IN CONCLUSION THE AMERICAN INDEX IS MORE CONSTANT AND I CAN SAY MORE RELIABLE.

lnprices = log(prices)
plot(lnprices$MXX, main = "Log of the Mexican Index over time")

2.2.2 Question 2

DO YOU SEE ANY DIFFERENCE BETWEEN THIS PLOT OF THE LOG OF MXX INDEX COMPARED TO THE PLOT OF THE MXX INDEX

2.2.2.1 Answer 2

NO, THE GRAPH IS BASICALLY THE SAME AS THE MXX INDEX.

2.3 Financial Returns

A financial simple return for a stock (Rt) is calculated as a percentage change of price from the previous period (t-1) to the present period (t):

Rt=((Adjpricet−Adjpricet−1))/Adjpricet−1=(Adjpricet/Adjpricet−1)−1 For example, if the adjusted price of a stock at the end of January 2021 was $100.00, and its previous (December 2020) adjusted price was $80.00, then the monthly simple return of the stock in January 2021 will be:

RJan2021=(AdpriceJan2021/AdpriceDec2020)−1=100/80−1=0.25

We can use returns in decimal or in percentage (multiplying by 100). We will keep using decimals.

In Finance it is very recommended to calculate continuously compounded returns (cc returns) and using cc returns instead of simple returns for data analysis, statistics and econometric models. cc returns are also called log returns.

One way to calculate cc returns is by subtracting the log of the current adjusted price (at t) minus the log of the previous adjusted price (at t-1):

rt=log(Adjpricet)−log(Adjprice(t−1))

This is also called as the difference of the log of the price.

We can also calculate cc returns as the log of the current adjusted price (at t) divided by the previous adjusted price (at t-1):

rt=log(Adjpricet/(Adjpricet−1))

cc returns are usually represented by small r, while simple returns are represented by capital R.

2.4 Calculation of Financial Returns

R = prices / lag(prices,n=1) - 1 
r = diff(log(prices))

Do a graph for the continously compounded returns of the Mexican Index

plot(r$MXX, col = "darkblue",
     main = "cc return for the MXX index")

### Question 3 DOES THIS SERIES HAVE ABOUT THE SAME MEAN FOR ALL TIME PERIODS?

2.4.0.1 Answer 3

YES, IT DOES, ONLY IN JANUARY 2020 THERE WAS A FALL UP TO -0.12 APROXIMATELY AND AN UP IN APRIL UP TO 0.12.

2.4.1 Question 4

DOES IT HAVE THE SAME STANDARD DEVIATION (VOLATILITY) FOR ALL TIME PERIODS?

2.4.1.1 Answer 4

IT CAN VARIATE BUT ITS ALMOST THE SAME.

2.5 Reading

2.5.1 A)

WHAT IS A STATIONARY SERIES

-R= IS A SERIES IN WHICH THE AVERAGE OR THE EXPECTED VALUE OF THE SERIES IS CONSTANT OVER TIME. E(Yt) = E(Yt−h) = µ

2.5.2 B)

WHICH ARE THE CONDITIONS OF A SERIES TO BE CONSIDERED AS A STATIONARY SERIES?

-R= THE MEAN, VARIANCE AND STANDARD DEVIATION HAS TO BE CONSTANT OVER THE TIME; THERE IS NO SEASONALITY; THE COVARIANCE(CORRELATION) BETWEEN ONE VALUE OF THE SERIES AND ONE IN THE PAST/FUTURE IS THE SAME.