Compute MAR#
The compute_mar function is designed to help financial analysts and investors measure the risk-adjusted performance of various financial instruments such as stocks, bonds, and other securities. It calculates the MAR (Return Over Maximum Drawdown) for the given price data.
To use the compute_mar function, you need to provide a pandas DataFrame containing price data with a datetime index. The function works with time series data.
The function takes the input DataFrame and computes the CAGR (Compound Annual Growth Rate) and the Maximum Drawdown for the given data. Then it divides the CAGR by the absolute value of the Maximum Drawdown to calculate the MAR.
After passing the DataFrame to the compute_mar function, it will return the MAR for the given data.
compute_mar() function#
Function#
def compute_mar(dataframe):
"""
Function to calculate MAR (Return Over Maximum Drawdown) given a dataframe of prices.
Parameters
----------
dataframe : pandas.DataFrame
DataFrame containing price data. Index should be of datetime type.
Returns
-------
float
The MAR of the input data.
"""
# Calculate the CAGR for the given data
cagr = compute_cagr(dataframe)
# Calculate the Maximum Drawdown for the given data
max_drawdown = compute_drawdowns(dataframe).min().abs()
# Calculate the MAR by dividing the CAGR by the absolute value of the Maximum Drawdown
mar = cagr.div(max_drawdown)
return mar
Description#
The compute_mar function computes the MAR (Return Over Maximum Drawdown) for a given pandas DataFrame of prices.
Parameters#
dataframe (pandas.DataFrame): DataFrame containing price data. The index should be of datetime type.
Returns#
float: The MAR of the input data.
Example#
import ds4finance as dsf
import yfinance as yf
import pandas as pd
def download_data(ticker, start, end):
data = yf.download(ticker, start=start, end=end, progress=False)
adj_close = data['Adj Close']
return adj_close
ticker = 'SPY'
start_date = '2015-12-31'
end_date = '2022-12-31'
# Load your price data into a pandas DataFrame
spy = pd.DataFrame(download_data(ticker, start_date, end_date))
spy.columns = ['spy']
# Calculate the MAR for the given data
mar = dsf.compute_mar(spy)
mar
spy 0.33773
dtype: float64