Utility Functions¶
The utility functions help in adjusting the simulated data cube. There are some assumptions about the cube. Namely, assuming that we are running a Monte-Carlo simulation of asset returns, the axis will be 3 dimensional where each axis represents the time, trials and asset class respectively.
Calibrate Data¶
-
muarch.calibrate.
calibrate_data
(data: numpy.ndarray, mean: Optional[Iterable[float]] = None, sd: Optional[Iterable[float]] = None, time_unit: Union[int, str] = 'month', inplace=False, tol=1e-06) → numpy.ndarray[source]¶ Calibrates the data given the target mean and standard deviation.
- Parameters
data (ndarray) – Data tensor to calibrate
mean (iterable float, optional) – The target annual mean vector
sd (iterable float, optional) – The target annual standard deviation (volatility) vector
time_unit (int or str) – Specifies how many units (first axis) is required to represent a year. For example, if each time period represents a month, set this to 12. If quarterly, set to 4. Defaults to 12 which means 1 period represents a month. Alternatively, you could put in a string name of the time_unit. Accepted values are weekly, monthly, quarterly, semi-annually and yearly
inplace (bool) – If True, calibration will modify the original data. Otherwise, a deep copy of the original data will be made before calibration. Deep copy can be time consuming if data is big.
tol (float) – Tolerance used to determine if calibrate should be called. For example, if the cube’s target annualized mean is similar to the actual tolerance, function will skip the mean adjustment.
- Returns
An instance of the adjusted numpy tensor
- Return type
ndarray
Truncate Outliers¶
-
muarch.calibrate.
truncate_outliers
(data: numpy.ndarray, *, bounds: List[Tuple[float, float]] = None, sd=0, replacement='mean', inplace=False)[source]¶ Truncates outliers by replacing it with the mean, median or a specified value.
Outlier is determined by the number of standard deviations past the mean within the asset group.
- Parameters
data (ndarray) – The tensor (data cube) where the axis represents time, trials and number of asset classes respectively
bounds (List of numbers) – A list containing the lower and upper bound for each asset class. If specified, this takes precedence over the
sd
parameter. Ifsd
is set to 0 and bounds are not specified, no changes will be madesd (float) – The number of standard deviations to consider a point an outlier. If
sd
is set to 0 and bounds are not specified, no changes will be madereplacement ({float, 'mean', 'median'}) – The value to replace outliers with. Valid values are ‘mean’, ‘median’ or a number.
inplace (bool) – If True, calibration will modify the original data. Otherwise, a deep copy of the original data will be made before calibration. Deep copy can be time consuming if data is big.
- Returns
A data cube with the outliers replaced
- Return type
ndarray
Basic Statistics¶
-
muarch.funcs.moments.
get_annualized_mean
(data: numpy.ndarray, time_unit: Union[int, str] = 'monthly') → Union[float, numpy.ndarray][source]¶ Gets the annualized mean for each asset class in the data cube
- Parameters
data – Data matrix or tensor. The axis must represent time, trials and assets respectively where the assets axis is valid only if the data is a tensor.
time_unit (int or str) – Specifies how many units (first axis) is required to represent a year. For example, if each time period represents a month, set this to 12. If quarterly, set to 4. Defaults to 12 which means 1 period represents a month. Alternatively, you could put in a string name of the time_unit. Accepted values are weekly, monthly, quarterly, semi-annually and yearly
- Returns
The annualized mean for the asset class or an array containing the annualized mean for each asset class.
- Return type
float or ndarray
-
muarch.funcs.moments.
get_annualized_sd
(data: numpy.ndarray, time_unit: Union[int, str] = 'monthly') → Union[float, numpy.ndarray][source]¶ Gets the annualized standard deviation for each asset class in the data cube
- Parameters
data – Data matrix or tensor. The axis must represent time, trials and assets respectively where the assets axis is valid only if the data is a tensor.
time_unit (int or str) – Specifies how many units (first axis) is required to represent a year. For example, if each time period represents a month, set this to 12. If quarterly, set to 4. Defaults to 12 which means 1 period represents a month. Alternatively, you could put in a string name of the time_unit. Accepted values are weekly, monthly, quarterly, semi-annually and yearly
- Returns
The annualized standard deviation (volatility) for the asset class or an array containing the annualized standard deviation for each asset class.
- Return type
float ndarray
-
muarch.funcs.moments.
get_annualized_skew
(data: numpy.ndarray, time_unit: Union[int, str] = 'monthly') → Union[float, numpy.ndarray][source]¶ Gets the annualized skew for each asset class in the data cube
- Parameters
data – Data matrix or tensor. The axis must represent time, trials and assets respectively where the assets axis is valid only if the data is a tensor.
time_unit (int or str) – Specifies how many units (first axis) is required to represent a year. For example, if each time period represents a month, set this to 12. If quarterly, set to 4. Defaults to 12 which means 1 period represents a month. Alternatively, you could put in a string name of the time_unit. Accepted values are weekly, monthly, quarterly, semi-annually and yearly
- Returns
The annualized skew for the asset class or an array containing the annualized skew for each asset class.
- Return type
float or ndarray
-
muarch.funcs.moments.
get_annualized_kurtosis
(data: numpy.ndarray, time_unit: Union[int, str] = 'monthly') → Union[float, numpy.ndarray][source]¶ Gets the annualized kurtosis for each asset class in the data cube
- Parameters
data – Data matrix or tensor. The axis must represent time, trials and assets respectively where the assets axis is valid only if the data is a tensor.
time_unit (int or str) – Specifies how many units (first axis) is required to represent a year. For example, if each time period represents a month, set this to 12. If quarterly, set to 4. Defaults to 12 which means 1 period represents a month. Alternatively, you could put in a string name of the time_unit. Accepted values are weekly, monthly, quarterly, semi-annually and yearly
- Returns
- The annualized kurtosis for the asset class or an array containing the annualized kurtosis for each
asset class.
- Return type
ndarray