### Introduction

In this chapter we are going to introduce some basic concepts in quantitative finance. We start with rate of return, mean and variance. You may think it's simple to calculate these values, however, there are number of different methods to calculate them. It's important to choose the appropriate calculation methods case by case.

### Rate of Return

#### Single-period Return

The single-period rate of return can be calculated as following:

Where is the rate of return, is the asset price at time , and is the asset price at time 0.

```
import numpy as np
rate_return = 102.0/100 - 1
print(rate_return)
[out]: 0.02
```

Let's say we bought a stock at $100, and half a year later it will grow to $102. A year later the price will come to $104. How to calculate our total return? Well, we can either deem it as a single-period:

or as a two-stage period:

Here we make calculations twice a year. It's called semi-annual compounding. How about quarterly compounding? Let's assume the stock prices at the end of each quarter are respectively.

The rate of return we calculate here is called **cumulative return** or **overall return**. It measures the total return of this asset over a period of time.

Now consider the following situation: we have two strategies: strategy A and strategy B. We backtested strategy A for 1 years and the cumulative return is 20%, while we backtested strategy B for 3 months(one quarter) and the cumulative return is 6%. Which strategy has a high rate of return? Our commonly used method is to convert all the returns into **compounding annual return**, regardless of the investing horizon of each strategy. We can compare the returns of strategies with different time horizon now. Since there are four quarters in a year,the annual return of strategy B is

Strategy B has an higher compounding annual return when we compare 26% with 20%.

#### Logarithm Return

In the above example, strategy A has 6% return over three months. Nominally, the annual return would be 4*6% = 24%. This nominal annual interest rate is called the stated annual interest rate. It is calculated as the periodic interest rate times the number of periods per year. It works according to the simple interest and does not take into account the compounding periods, while the effective annual interest rate is 26% as we calculated above and it does account for intra-year compounding.

The effective annual interest rate is an essential tool that allows the evaluation of the real return on investment. If we assume the number of compounding periods in one year is n, the formula to convert the stated annual interest rate to the effective annual interest rate is

Now imagine the price of asset is changing every second or even every millisecond, the period of compounding n approaches infinite. This is called **continuous compounding**. The calculation formula is given below:

From the above limitation equation, we know that if we assume continuous compounding:

Then we take on both side of the equation:

Here we got the **logarithmic return,** or **continuously compounded return**. This return is the nominal return with the interest compounding every millisecond. To see how it is close to effective interest rate, recall the equation above:

then we have

where the second equality holds due to Taylor Expansion and the interest rate being small. This is frequently used when calculating returns, because once we take the logarithm of asset prices, we can calculate the logarithm return by simply doing a subtraction. Here we use Apple stock prices as an example:

```
from datetime import datetime
qb = QuantBook()
aapl = qb.AddEquity("AAPL").Symbol
aapl_table = qb.History(aapl, datetime(1998,1,1), qb.Time, Resolution.Daily).loc[aapl]
aapl = aapl_table.loc['2017-3',['open','close']]
#take log return
aapl['log_price'] = np.log(aapl.close)
aapl['log_return'] = aapl['log_price'].diff()
print(aapl)
```

The output is:

```
open close log_price log_return
time
2017-03-01 32.210640 32.189492 3.471640 NaN
2017-03-02 32.403321 32.847428 3.491873 0.020233
2017-03-03 32.896773 32.652397 3.485918 -0.005955
2017-03-04 32.612451 32.845078 3.491802 0.005884
2017-03-07 32.739338 32.741688 3.488649 -0.003153
2017-03-08 32.675895 32.783984 3.489940 0.001291
2017-03-09 32.642998 32.661796 3.486206 -0.003734
2017-03-10 32.600702 32.586603 3.483901 -0.002305
2017-03-11 32.725240 32.694693 3.487213 0.003311
2017-03-14 32.619500 32.708791 3.487644 0.000431
2017-03-15 32.732289 32.659446 3.486134 -0.001510
2017-03-16 32.760486 33.004862 3.496655 0.010521
2017-03-17 33.070656 33.058907 3.498291 0.001636
2017-03-18 33.131750 32.894423 3.493303 -0.004988
2017-03-21 33.004862 33.239839 3.503749 0.010446
2017-03-22 33.401973 32.859177 3.492231 -0.011518
2017-03-23 32.856827 33.230440 3.503466 0.011235
2017-03-24 33.192844 33.112952 3.499924 -0.003542
2017-03-25 33.249238 33.047158 3.497936 -0.001989
2017-03-28 32.753437 33.103553 3.499641 0.001705
2017-03-29 33.105902 33.789685 3.520156 0.020515
2017-03-30 33.759138 33.864878 3.522378 0.002223
2017-03-31 33.869578 33.820232 3.521059 -0.001319
```

Here we calculated the daily logarithmic return of Apple stock. Given that we know the daily logarithm return of in this month, we can calculate the monthly return by simply sum all the daily returns up.

```
month_return = aapl.log_return.sum()
print(month_return)
[out]: 0.0494191398112811
```

It may sounds incorrect to sum up the daily returns, but we can prove that it's mathematically correct. Let's assume the stock prices in a period of time are represented by . Then the cumulative rate of return is given by:

According to the equation above, we can simple sum up each logarithmic return in a period to get the cumulative return. The convenience of this method is also one of the reasons why we use logarithmic return in quantitative finance.

### Mean

#### Arithmetic Mean

Mean is a measure of the central tendency of a data series. It capture the key character of the distribution of the data series. When we talk about mean, by default it refers to **arithmetic mean**. It's defined as the sum of the values divided by the number of observations:

Where is our data series.

In python we can use NumPy.mean() to do the calculation:

```
print(np.mean(aapl.log_price))
[out]: 3.4956395904827184
```

#### Geometric Mean

The geometric mean is an average that is useful for data series of positive numbers that are better interpreted according to their product, such as growth rate. It's calculated by:

Let's calculate the geometric mean of a series of single-period return:

Now the equation becomes the form which we are familiar with:

This is why we said it make sense when applied to growth rates.

### Variance and Standard Deviation

#### Variance

**Variance** is a measure of dispersion. In finance, most of the time variance is a synonym for risk. The higher the variance of an asset price is, the higher risk the asset bears. Variance is usually represented by , and it's calculated by

In python we can use NumPy.var to calculate it:

```
print(np.var(aapl.log_price))
[out]: 0.00014725117002413818
```

#### Standard Deviation

The most commonly used measure of dispersion in finance is **standard deviation**. It's usually represented by . It's obvious to see the relation between standard deviation and variance:

NumPy also provides us a method to calculate standard deviation.

```
print(np.std(aapl.log_price))
[out]: 0.012134709309420568
```

### Summary

We introduced different types of rate of return in this chapter, which could be a little bit tricky when we calculate them. Mean and standard deviation are also very important concepts when we conduct hypothesis test or measure the risk associated with a asset. We will use those concepts intensively in our later chapter.