MCPcopy
hub / github.com/ranaroussi/quantstats / distribution

Function distribution

quantstats/stats.py:49–85  ·  view source on GitHub ↗
(returns, compounded=True, prepare_returns=True)

Source from the content-addressed store, hash-verified

47
48
49def distribution(returns, compounded=True, prepare_returns=True):
50 def get_outliers(data):
51 # https://datascience.stackexchange.com/a/57199
52 Q1 = data.quantile(0.25)
53 Q3 = data.quantile(0.75)
54 IQR = Q3 - Q1 # IQR is interquartile range.
55 filtered = (data >= Q1 - 1.5 * IQR) & (data <= Q3 + 1.5 * IQR)
56 return {
57 "values": data.loc[filtered].tolist(),
58 "outliers": data.loc[~filtered].tolist(),
59 }
60
61 if isinstance(returns, _pd.DataFrame):
62 warn(
63 "Pandas DataFrame was passed (Series expected). "
64 "Only first column will be used."
65 )
66 returns = returns.copy()
67 returns.columns = map(str.lower, returns.columns)
68 if len(returns.columns) > 1 and "close" in returns.columns:
69 returns = returns["close"]
70 else:
71 returns = returns[returns.columns[0]]
72
73 apply_fnc = comp if compounded else _np.sum
74 daily = returns.dropna()
75
76 if prepare_returns:
77 daily = _utils._prepare_returns(daily)
78
79 return {
80 "Daily": get_outliers(daily),
81 "Weekly": get_outliers(daily.resample("W-MON").apply(apply_fnc)),
82 "Monthly": get_outliers(daily.resample("ME").apply(apply_fnc)),
83 "Quarterly": get_outliers(daily.resample("QE").apply(apply_fnc)),
84 "Yearly": get_outliers(daily.resample("YE").apply(apply_fnc)),
85 }
86
87
88def expected_return(returns, aggregate=None, compounded=True, prepare_returns=True):

Callers

nothing calls this directly

Calls 1

get_outliersFunction · 0.85

Tested by

no test coverage detected