---
title: "Introduction to patterncausality"
author: "Stavros Stavroglou, Athanasios Pantelous, Hui Wang"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to patterncausality}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  warning = FALSE,
  collapse = TRUE,
  comment = "#>"
)
```

Pattern Causality gives us a method to measure the causality in the complex system, it has its own ability to catch the hidden pattern in many kinds of series, so that it is statistically sufficient.


## PC in financial market

First of all, we can import the data of AAPL and MSFT, also we can import data from the **yahooo** api.

```{r message = FALSE}
library(patterncausality)
data(stock)
head(stock)
```

We can visualize this stock price.

```{r echo=FALSE}
library(ggplot2)
df <- data.frame(
  Date = as.Date(rownames(stock)),
  Value = c(
    stock$AAPL.Close,
    stock$MSFT.Close
  ),
  Type = c(
    rep("AAPL", dim(stock)[1]),
    rep("MSFT", dim(stock)[1])
  )
)
ggplot(df) +
  geom_line(aes(Date, Value, group = Type, colour = Type), linewidth = 0.4) +
  theme_bw(base_size = 12, base_family = "Times New Roman") +
  xlab("Time") +
  ylab("Stock Price") +
  theme(
    legend.position = c(0.1, 0.85), legend.box.background = element_rect(fill = NA, color = "black", linetype = 1), legend.key = element_blank(),
    legend.title = element_blank(), legend.background = element_blank(), axis.text = element_text(size = rel(0.8)),
    strip.text = element_text(size = rel(0.8))
  ) +
  scale_color_manual(values = c("#DC143C", "#191970"))
```

### Search the parameters

Then search the best parameters for the PC.

```{r eval=FALSE}
dataset <- stock
parameter <- optimalParametersSearch(Emax = 5, tauMax = 5, metric = "euclidean", dataset = dataset)
```

### Calculate the causality

After that, calculate the causality of each status.

```{r}
X <- stock$AAPL.Close
Y <- stock$MSFT.Close
pc <- pcLightweight(X, Y, E = 3, tau = 2, metric = "euclidean", h = 1, weighted = TRUE, tpb=FALSE)
print(pc)
```

Lastly we can also visualize this result.

```{r}
library(ggplot2)
df <- data.frame(
  name = stringr::str_to_title(c(colnames(pc))),
  val = as.vector(unlist(pc))
)

ggplot(df, aes(x = name, y = val, fill = name)) +
  geom_bar(stat = "identity", alpha = .6, width = .4) +
  scale_fill_grey(start = 0, end = 0.8) + # start and end define the range of grays
  labs(x = "Status", y = "Strength") +
  theme_bw(base_size = 12, base_family = "Times New Roman") +
  theme(
    legend.position = "none", axis.text = element_text(size = rel(0.8)),
    strip.text = element_text(size = rel(0.8))
  )
```

### Full details

The details could be found with following code.

```{r eval=FALSE}
X <- stock$AAPL.Close
Y <- stock$MSFT.Close
detail <- pcFullDetails(X, Y, E = 3, tau = 2, metric = "euclidean", h = 1, weighted = TRUE)
predict_status <- detail$spectrumOfCausalityPredicted
real_status <- detail$spectrumOfCausalityReal
names(detail)
```

So far, the whole process of this algorithm has finished.

