Description
asrol calculates descriptive statistics in a user’s defined rolling-window or over a grouping variable. asrol can efficiently handle all types of data structures such as data declared as time series or panel data, undeclared data, or data with duplicate values, missing values or data having time series gaps. asrol can be used for the calculation of a variety of statistics [see table of contents].
Installation
ssc install asrol
After installation, you can read the help file by typing:
help asrol
Options and defaults
Version 4.5.1 of asrol significantly improves the calculation of the product and the geometric mean. Since both the statistics involve the multiplication of values in a given window, the presence of missing values and zeros present a challenge to getting the desired results. Following are the defaults in asrol to deal with missing values and zeros.
a. Missing values are ignored when calculating the product or the geometric mean of values.
b. Handling zeros in geometric mean: To be consistent with Stata’s default for geometric mean calculations, (see ameans), the default in asrol is to ignore zeros and negative numbers. So the geometric mean of 0,2,4,6 is 3.6342412, that is [2 \cdot 4 \cdot 6]^{\frac{1}{3}} . And the geometric mean of (0,-2,4,6) is 4.8989795, that is [4\cdot 6]^{\frac{1}{2}}
c. Handling zeros in products: Zeros are considered when calculating the product of values. So the product of 0,2,4,6 is 0
d. Option ignorezero: This option can be used to ignore zeros when calculating the product of values. Therefore, when the zero is ignored, the product of 0,2,4,6 is 48
e. Option add(#) : The add
option involves adding a constant value to each element within a given range before computing either the product or the geometric mean. Once the desired statistical measure is calculated, the same constant is subtracted from the result. For instance, when using the add(1)
option, consider the values 0, 0.2, 0.4, 0.6 . The product of these values, after adding 1 to each, is 1.6880001 (i.e., [(1 + 0) * (1 + 0.2) * (1 + 0.4) * (1 + 0.6)] - 1) . Similarly, the geometric mean, after applying the same adjustment, is approximately 0.280434 = [ (1 + 0) \cdot (1 + 0.2) \cdot (1 + 0.4) \cdot (1 + 0.6)^{\frac{1}{4}} - 1 ].
Examples
Let us start with simple examples of calculating the geometric mean and products. Our example data has stock prices, company identifiers (symbols) and time identifier (date)
use http://fintechprofessor.com/stocks.dta, clear
⠀
* Generae numeric identifier for each firm
encode symbol, gen(id)
⠀
* Declear the data as panel data
tsset id date* Create stock returns
gen returns = d.close/l.close
⠀
* Note the above formula for stock returns is analogous to
gen returns2 = (close - L.close) / L.close
Geometric mean
Now find geometric mean for stock returns, adding 1 before calculation and subtracting the same after calculation. The calculations are made for each firm in a rolling window of 20 observations
bys id: asrol returns, stat(gmean) window(date 20) add(1)
Products – the case of cumulative returns
Since we find products of (1+returns) for finding cumulative returns over n-periods, we can use the product function of asrol [read this blog entry for more more details on simple and log returns.
R_c = \prod_{i=1}^{n} (1+ r_i) - 1 \quad --- (Eq. 1)
In this equation:
- (R_c) represents the cumulative return over n periods.
- (r_i) represents the simple return for the i-th period.
- The product symbol (\prod) is used to indicate that we multiply together the terms (1 + (r_i)) for each period from i = 1\; to\; n.
The asrol command for the 20-periods rolling window cumulative returns would be:
bys id: asrol returns, stat(product) window(date 20) add(1)
Option to ignore zeros
The ignorezero
or ig
option proves useful when we need to exclude zeros from product calculations. Consider a scenario where we have a variable x
with values 1, 2, 3, 0, and 5. Calculating the product of this variable would yield zeros due to the presence of the zero value. However, in situations where we specifically want to find the product of only non-zero values, we can use the ig
option of asrol
:
asrol x, stat(product) ig
list
x | x\_product |
---|---|
1 | 30 |
2 | 30 |
3 | 30 |
0 | 30 |
5 | 30 |
\text{\footnotesize{Notes:}} |
Without using the option ig
, the product would be zero.
asrol x, stat(product) gen(prod_withoutig)
list
x | x\_product | prod\_withoutig |
---|---|---|
1 | 30 | 0 |
2 | 30 | 0 |
3 | 30 | 0 |
0 | 30 | 0 |
5 | 30 | 0 |
\text{\footnotesize{Notes:}} |
A note on the methods used
In previous versions of asrol, the log transformation method was employed to find products and geometric means. However, starting from version 4.5.1, this log transformation method has been discontinued. Instead, the calculations now directly consider the actual multiplications of the values within a given range.
Specifically, when calculating the geometric mean, we follow this approach:
Given a set of numbers (x_1, x_2, \ldots, x_n), the geometric mean is defined as:
\text{Geometric Mean} = \sqrt[n]{x_1 \cdot x_2 \cdot \ldots \cdot x_n}Where:
- (n) represents the total number of values in the set.
- The product of all the values is taken.
- The nth root of this product yields the geometric mean.
[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
Leave A Comment