Skip to content
Vladislav Pyatnitskiy edited this page Oct 3, 2023 · 33 revisions

Welcome to the stockanalytics wiki!

1. Descriptive Statistics

1.1 Mean

  • Default R script: mean(x)
  • Advanced R script: apply(x,2, function(col) mean(col))

1.2 Median

  • Default R script: median(x) or quantile(x, 0.5)
  • Advanced R script: apply(x,2, function(col) mean(col)) or apply(x, 2, function(col) quantile(col, 0.5))

1.3 Mode

Rarely used in Finance, but can be referred as the 'height' of distribution, representing the most frequent observation value.

  • Default R script: mode(x)

1.4 Standard Deviation

  • Default R script: sd()
  • Advanced R script: apply(x,2, function(col) sd(col))

1.5 Skewness

Shows how distribution is skewed. It depends on the value that further located from the mode: further minimum value states that distribution is negatively skewed whereas further maximum values demonstrates positively skewed distribution.

  • Default R script: skewness()
  • fBasics library: colSkewness()
  • Advanced R script: apply(x,2, function(col) skewness(col))

1.6 Kurtosis

Measures peakedness of the distribution.

  • Default R script: kurtosis()
  • fBasics library: colKurtosis()
  • Advanced R script: apply(x,2, function(col) kurtosis(col))

1.7 Other methods

Default R script enables to calculate statistical measures only for one array or matrix column. To overcome limitation is possible thanks to two options: either downloading package or using apply()

2. Correlation

R code: cor()

3. Regression

Regression is a great tool to find which factors indeed have an impact on dependent variable and forecast possible change. For example, how will change Oil Company price if WTI or Brent goes up. Default script enables to change variables manually when experienced econometrician would prefer packages like MuMIn, which facilitates selection of valid variables.

• Default R script: summary(lm(formula = y ~ x)

• R script for MuMIn package: full.model <- lm(Portfolio ~ GC.F + BZ.F + HG.F + SI.F, reg_df) dredge(full.model)

3.1 Beta

4. Normality test

4.1 Jacque-Bera test

Mostly used by economists.

  • R script: jarqueberaTest() or jbTest()

4.2 Shapiro-Wilk test

4.3 Kolmogorov-Smirnov test

Often used in Life Sciences.

  • R script: ksnormTest()

5. Securities & Portfolio Performance Assessment

Here are represented scripts to calculate ratios that enable to assess performance of the financial instruments.

5.1 Sharpe ratio

The most popular ratio to assess asset and portfolio performance. It is a ratio of market premium (Rp - Rf, difference between asset/portfolio return and risk free rate) and standard deviation of the asset/portfolio. The higher Sharpe, the better the performance has been for the selected period.

5.2 Treynor ratio

Alternative to Sharpe ratio, where denominator is beta of the asset/portfolio.

The drawback of the ratio is the unsuitability for assets with negative beta coefficient to market as it makes their values ambiguous to interpret. Meanwhile, values of the Sharpe's standard deviation can never be negative.

5.3 Sortinto ratio

The least popular ratio due to its complexity to calculate.

References

Würtz, D. et al. (2014) Basic R for Finance. publication. Zurich: Finance Online GmbH, pp. 186.

https://cran.r-project.org/web/packages/MuMIn/MuMIn.pdf