explain¶
See Explainability for how the pieces below fit together, and where the explanation text itself actually lives (hint: not in this module).
Note
from portpy import Portfolio (or any import of the portpy package) binds the package attribute portpy.explain to the function below, not this module. Import the other names (available, get, register, Explanation, MetricResult) directly from portpy.explain as shown in each signature.
Explanation
dataclass
¶
Explanation(
name: str,
category: str,
summary: str,
how_to_read: str,
good_vs_bad: str,
caveats: str = "",
formula: str = "",
interpret: Callable[[Any], str] | None = None,
)
A structured, human-readable knowledge card attached to a PortPy object.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Registry key, matching the function/chart/model/strategy name (e.g. "sharpe_ratio", "drawdown_chart", "capm"). |
category |
str
|
One of "metric", "chart", "model", "strategy". |
summary |
str
|
One or two sentences on what this is. |
how_to_read |
str
|
How to read the number/axis/output in practice. |
good_vs_bad |
str
|
Rules of thumb for judging whether a value is good or bad. |
caveats |
str
|
Known limitations, edge cases, or ways this can mislead. |
formula |
str
|
Optional plain-text formula for reference. |
interpret |
Callable[[Any], str] | None
|
Optional function mapping a concrete value to a one-line verdict, used to render a "this result" line when a value is available. |
render ¶
Render this card as plain text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Optional concrete value to include a one-line, value-specific
verdict for, via |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered explanation text. |
Source code in src/portpy/explain.py
MetricResult ¶
Bases: float
A float that also knows what it means.
Arithmetic, comparisons, round(), string formatting, and numpy/pandas interop all work exactly as they would on a plain float (this is a float subclass). On top of that, it carries the metric's registered name and can render a full explanation on demand.
Note
str(result) prints the plain number (so print(sharpe) stays clean); repr(result) - what you see when a bare expression is evaluated in a REPL/notebook - includes the name and a one-line interpretation.
__new__ ¶
__new__(
value: float,
name: str,
unit: str | None = None,
meta: dict | None = None,
) -> MetricResult
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The numeric result. |
required |
name
|
str
|
The registry key this result explains itself with (e.g. "sharpe_ratio"). |
required |
unit
|
str | None
|
Optional display unit (e.g. "%/yr"). |
None
|
meta
|
dict | None
|
Optional extra metadata carried alongside the value. |
None
|
Returns:
| Type | Description |
|---|---|
MetricResult
|
A new MetricResult. |
Source code in src/portpy/explain.py
interpretation
property
¶
A one-line, value-specific verdict (empty string if none is registered).
explain ¶
Print (by default) and return the full explanation for this result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
print_it
|
bool
|
If True (default), also print() the rendered text. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered explanation text. |
Source code in src/portpy/explain.py
explain ¶
Explain a metric name, :class:MetricResult, chart, model, or strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
A registered name (str), a :class: |
required |
value
|
Any
|
Optional concrete value to generate a one-line verdict for. Inferred
automatically for :class: |
None
|
print_it
|
bool
|
If True (default), also print() the rendered text. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered explanation text. |
Source code in src/portpy/explain.py
register ¶
Register (or overwrite) an Explanation in the global registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
explanation
|
Explanation
|
The Explanation to register, keyed by its |
required |
Returns:
| Type | Description |
|---|---|
Explanation
|
The same |
Source code in src/portpy/explain.py
get ¶
Look up a registered Explanation by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The registry key to look up (e.g. "sharpe_ratio"). |
required |
Returns:
| Type | Description |
|---|---|
Explanation
|
The registered Explanation. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no Explanation is registered under |
Source code in src/portpy/explain.py
available ¶
List registered explanation names, optionally filtered by category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
str | None
|
If given, restrict to this category (e.g. "metric"). |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A sorted list of registered names. |