phenance.com
Stock Data Pages | TCA League Tables | About

Analyzing Bitcoin in R - Part 7 - Calculating and plotting statistics

Pub: 04.12.17

| By Anonymous

In Finance.

Tags: coding r bitcoin analysis .

Let's take a look at the data with a simple plot. We put two plots on one and manipulate the margins to fit better.

png('btc_plot_daily_cl-vol.png')
par(mfrow = c(2, 1))
par(mar = c(3, 2, 3, 2))
plot(rawdailyohlc$Close, main = "Bitcoin USD Close")
plot(rawdailyohlc$Volume, main = "Bitcoin Volume")
dev.off()

Daily Bitcoin USD and Volume Chart

Using the PerformanceAnalytics package we can get a quick summary of our time series variable.

library(PerformanceAnalytics)
table.Stats(rawdailyohlc)

The output looks like the following. Volume is in scientific notation.

                        Open         High          Low        Close       Volume
Observations       2253.0000    2253.0000    2253.0000    2253.0000 2.253000e+03
NAs                   0.0000       0.0000       0.0000       0.0000 0.000000e+00
Minimum               2.2200       2.2700       1.5000       2.2400 2.500000e-01
Quartile 1           88.0400      90.6600      81.5000      88.1600 3.012206e+03
Median              349.9900     359.0000     340.0100     350.0000 6.745395e+03
Arithmetic Mean     699.7319     724.3399     676.8089     704.7470 9.908925e+03
Geometric Mean      192.3329     197.9920     186.0529     193.1077 4.705933e+03
Quartile 3          635.1600     652.7900     618.6700     635.0000 1.303740e+04
Maximum           10875.6800   11800.0100   10637.6900   11180.7800 1.370702e+05
SE Mean              27.3959      28.6678      26.4880      27.7876 2.392046e+02
LCL Mean (0.95)     646.0081     668.1219     624.8654     650.2551 9.439841e+03
UCL Mean (0.95)     753.4558     780.5578     728.7525     759.2389 1.037801e+04
Variance        1690954.3066 1851606.2422 1580741.6520 1739652.5330 1.289141e+08
Stdev              1300.3670    1360.7374    1257.2755    1318.9589 1.135403e+04
Skewness              3.8896       3.9671       3.9006       3.9386 3.533900e+00
Kurtosis             17.5976      18.5721      17.6967      18.1663 2.235400e+01

We can also calculate daily returns from the closing values of the Bitcoin USD value:

dailyret <- dailyReturn(Cl(rawdailyohlc))
table.Stats(dailyret)

The result looks like this:

                daily.returns
Observations        2253.0000
NAs                    0.0000
Minimum               -0.4852
Quartile 1            -0.0109
Median                 0.0024
Arithmetic Mean        0.0047
Geometric Mean         0.0034
Quartile 3             0.0201
Maximum                0.5613
SE Mean                0.0011
LCL Mean (0.95)        0.0026
UCL Mean (0.95)        0.0068
Variance               0.0026
Stdev                  0.0514
Skewness               0.3289
Kurtosis              19.2614

Monthly returns can also be calculated from the daily raw data in xts format.

monthret <- monthlyReturn(Cl(rawdailyohlc))
table.Stats(monthret)

They look like this:

                monthly.returns
Observations            76.0000
NAs                      0.0000
Minimum                 -0.3461
Quartile 1              -0.0708
Median                   0.0771
Arithmetic Mean          0.1743
Geometric Mean           0.1042
Quartile 3               0.2408
Maximum                  4.5002
SE Mean                  0.0681
LCL Mean (0.95)          0.0386
UCL Mean (0.95)          0.3100
Variance                 0.3527
Stdev                    0.5939
Skewness                 5.4655
Kurtosis                36.0110

Let's quickly plot the daily returns to see what they look like:

png('btc_plot_daily_retspanel.png')
par(mfrow = c(2,2))
par(mar = c(3, 2, 3, 2))
plot(Cl(rawdailyohlc))
plot(dailyret)
hist(dailyret, breaks = 100)
qqnorm(as.vector(dailyret))
qqline(as.vector(dailyret))
dev.off()

Daily Bitcoin Returns Chart

We might want to compare the distribution of daily returns to a normal distribution to see how different they are:

png('btc_plot_daily_hist.png')
f.den <- function(t) dnorm(t,mean(as.vector(dailyret), na.rm = TRUE),sqrt(var(as.vector(dailyret), na.rm = TRUE)))
curve(f.den, xlim=c(-0.2,0.2))
hist(as.vector(dailyret), prob = T, add = T, breaks = 100)
dev.off()

Daily Bitcoin Returns Histogram

What are the extreme daily returns that you might see at a 2.5% probability

quantile(dailyret, probs = c(.025), na.rm = TRUE)
quantile(dailyret, probs = c(.975), na.rm = TRUE)
     2.5% 
-0.09403481
    97.5% 
0.1092761 

Moving on to calculating some summary statistics in rolling windows: Preliminary return analysis with rolling windows

You can also jump to each section directly from here: