bayesline.api.equity.PortfolioHierarchySettings#

pydantic model bayesline.api.equity.PortfolioHierarchySettings#

Specifies portfolio hierarchies with arbitrary groupings (e.g. manager, etc.).

Show JSON schema
{
   "title": "PortfolioHierarchySettings",
   "description": "Specifies portfolio hierarchies with arbitrary groupings (e.g. manager, etc.).",
   "type": "object",
   "properties": {
      "portfolio_schema": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "integer"
            },
            {
               "$ref": "#/$defs/PortfolioOrganizerSettings"
            }
         ],
         "description": "The portfolio organizer settings to use as an underlying schema of portfolios. The 'Default' schema is used by default.",
         "title": "Portfolio Schema"
      },
      "groupings": {
         "additionalProperties": {
            "items": {
               "type": "string"
            },
            "type": "array"
         },
         "title": "Groupings",
         "type": "object"
      },
      "portfolio_ids": {
         "items": {
            "type": "string"
         },
         "title": "Portfolio Ids",
         "type": "array"
      },
      "benchmark_ids": {
         "items": {
            "anyOf": [
               {
                  "type": "string"
               },
               {
                  "type": "null"
               }
            ]
         },
         "title": "Benchmark Ids",
         "type": "array"
      }
   },
   "$defs": {
      "PortfolioOrganizerSettings": {
         "additionalProperties": false,
         "description": "Specifies which portfolios to enable (from different sources).\n\nDifferent sources (e.g. uploaded portfolios) can provide the same portfolio\nidentifiers. These settings allow to specify which portfolios to enable from\nwhich sources.",
         "properties": {
            "dataset": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "description": "The name of the underlying dataset to use for price data needed to forward fill portfolios, obtain corporate actions, etc.If none is given then the configured default dataset is used.",
               "examples": [
                  "Bayesline-US"
               ],
               "title": "Dataset"
            },
            "enabled_portfolios": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "additionalProperties": {
                        "type": "string"
                     },
                     "type": "object"
                  }
               ],
               "description": "The enabled portfolios from different sources. The key is the portfolio ID, and the value is the source (name of the underlying portfolio service). Pass a str to reference an entire portfolio source (e.g. all portfolios from an upload).",
               "title": "Enabled Portfolios"
            }
         },
         "required": [
            "enabled_portfolios"
         ],
         "title": "PortfolioOrganizerSettings",
         "type": "object"
      }
   },
   "additionalProperties": false,
   "required": [
      "portfolio_schema",
      "portfolio_ids",
      "benchmark_ids"
   ]
}

Config:
  • frozen: bool = True

  • extra: str = forbid

Fields:
  • benchmark_ids (list[str | None])

  • groupings (dict[str, list[str]])

  • portfolio_ids (list[str])

  • portfolio_schema (str | int | bayesline.api._src.equity.portfolio_settings.PortfolioOrganizerSettings)

Validators:
  • _fill_benchmark_ids » all fields

  • _validate_benchmark_ids » portfolio_ids

  • _validate_dimensions » all fields

  • _validate_groupings » groupings

  • _validate_portfolio_ids » portfolio_ids

field portfolio_schema: Annotated[str | int | PortfolioOrganizerSettings] [Required]#

The portfolio organizer settings to use as an underlying schema of portfolios. The ‘Default’ schema is used by default.

Constraints:
  • references = <class ‘bayesline.api._src.equity.portfolio_settings.PortfolioOrganizerSettings’>

Validated by:
  • _fill_benchmark_ids

  • _validate_dimensions

field groupings: dict[str, list[str]] [Optional]#
Validated by:
  • _fill_benchmark_ids

  • _validate_dimensions

  • _validate_groupings

field portfolio_ids: list[str] [Required]#
Validated by:
  • _fill_benchmark_ids

  • _validate_benchmark_ids

  • _validate_dimensions

  • _validate_portfolio_ids

field benchmark_ids: list[str | None] [Required]#
Validated by:
  • _fill_benchmark_ids

  • _validate_dimensions

classmethod from_source(source: str, portfolio_ids: list[str], benchmark_ids: list[str | None] | None = None, groupings: dict[str, list[str]] | None = None, dataset: str | None = None) PortfolioHierarchySettings#
classmethod from_polars(df: DataFrame, portfolio_schema: str | int | PortfolioOrganizerSettings = 'Default', portfolio_source: str | None = None, dataset: str | None = None) PortfolioHierarchySettings#

Creates a portfolio hierarchy from a dataframe.

Must contain a column portfolio_id and optionally benchmark_id. Every other column is interpreted as a grouping. 0 groupings are allowed. Index is ignored.

Parameters#

df: pl.DataFrame:

The dataframe to create the hierarchy from.

portfolio_schema: str:

The underlying portfolio schema to use.

portfolio_source: str | None = None:

The source to use for the portfolio schema. If not provided then the portfolio schema is used. If provided then it will override the portfolio schema.

dataset: str | None = None:

The dataset to use for the portfolio schema. If not provided then the default dataset is used. Will be ignored if source is not provided.

Returns#

Pydantic object representing the hierarchy.

to_polars() DataFrame#

Converts the hierarchy to a polars dataframe.

The last two columns are the portfolio and benchmark IDs. Every column before that is a grouping. 0 groupings are possible.

Returns#

pl.DataFrame:

The dataframe representation of the hierarchy, sorted by portfolio ID.