Created
May 23, 2023 17:23
-
-
Save dawu76/a74a31a72089aa23026286ead7f38086 to your computer and use it in GitHub Desktop.
ChatGPT-inspired classes representing business metric formulas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Hacky metrics classes based on ChatGPT output\n", | |
"\n" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Sampling of prompts given to ChatGPT:\n", | |
"\n", | |
"```\n", | |
" given the following formulas for common financial metrics:\n", | |
"\n", | |
" operating profit = revenue - operating expenses\n", | |
" operating margin = operating profit / revenue\n", | |
" net profit = operating profit - interest - taxes\n", | |
" free cash flow = net profit + non-cash expenses - increase in working capital - capital expenditures\n", | |
"\n", | |
" Write python classes to represent each metric as a \"node\" in a DAG.\n", | |
" \n", | |
" for the metrics that are calculated from other metrics, write classes to represent those mathematical operations via an abstract syntax tree (AST).\n", | |
"\n", | |
" Ensure the code can handle metrics calculated from more than two parent metrics. also be sure to convert the names of metrics to lower-case when storing them as instance variables.\n", | |
"\n", | |
" Also satisfy the following requirements:\n", | |
" (a) given a set of available metrics, such as {\"operating expenses\", \"revenue\"} or {\"operating expenses\", \"revenue\", \"interest\", \"taxes\"} which downstream metrics can be calculated directly from those available metrics?\n", | |
" (b) given a set of desired metrics, such as {\"operating margin\", \"free cash flow\"}, which upstream metrics are required to calculate the requested metrics?\n", | |
"\n", | |
" the code should have the following behavior.\n", | |
"\n", | |
" if i run this code block:\n", | |
"\n", | |
" ```python\n", | |
" requested_metrics = {\"operating margin\", \"free cash flow\"}\n", | |
" upstream_metrics = graph.calculate_upstream_metrics(requested_metrics)\n", | |
" print(upstream_metrics)\n", | |
" ```\n", | |
"\n", | |
" I should get this output:\n", | |
"\n", | |
" ```\n", | |
" ['revenue', 'operating expenses', 'interest', 'taxes', 'non cash expenses', 'increase in working_capital', 'capital expenditures']\n", | |
" ```\n", | |
"\n", | |
" if i run this code block: \n", | |
"\n", | |
" ```python\n", | |
" available_metrics = {\"operating expenses\", \"revenue\", \"interest\", \"taxes\"}\n", | |
" downstream_metrics = graph.calculate_downstream_metrics(available_metrics)\n", | |
" print(downstream_metrics)\n", | |
" ```\n", | |
"\n", | |
" i should get this output:\n", | |
"\n", | |
" ```\n", | |
" ['operating profit', 'operating margin', 'net profit']\n", | |
"```" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"ChatGPT output did not produce the desired output, but after a bunch of hacky changes, the following code appears to produce the expected output.\n", | |
"\n", | |
"However it will likely fail any battery of comprehensive unit tests." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import inspect\n", | |
"\n", | |
"class BaseMetric:\n", | |
" \"\"\"\n", | |
" Metrics not derived from upstream metrics. This is sort of arbitrary as any metric\n", | |
" can be further broken down into smaller conponents.\n", | |
" \"\"\"\n", | |
"\n", | |
" def __init__(self, name):\n", | |
" self.name = name.lower()\n", | |
" self.root_parents = [] # upstream root metrics used to calculate this metric\n", | |
" self.parents = [] # upstream derived metrics used to calculate this metric\n", | |
" self.children = [] # downstream metrics calculated from this metric\n", | |
" self.value = None # a concrete value for this metric\n", | |
"\n", | |
" def add_parent(self, parent):\n", | |
" self.parents.append(parent)\n", | |
" parent.children.append(self)\n", | |
"\n", | |
" def remove_parent(self, parent):\n", | |
" self.parents.remove(parent)\n", | |
" parent.children.remove(self)\n", | |
"\n", | |
" def add_child(self, child):\n", | |
" self.children.append(child)\n", | |
" child.parent.append(self)\n", | |
" \n", | |
" def remove_child(self, child):\n", | |
" self.children.remove(child)\n", | |
" child.parent.remove(self)\n", | |
"\n", | |
" def calculate(self):\n", | |
" return self.value\n", | |
"\n", | |
" def get_downstream_metrics(self, downstream_metrics):\n", | |
" downstream_metrics.add(self)\n", | |
" for child in self.children:\n", | |
" child.get_downstream_metrics(downstream_metrics)\n", | |
"\n", | |
" def get_upstream_metrics(self, upstream_metrics):\n", | |
" if not self.parents:\n", | |
" upstream_metrics.add(self)\n", | |
" for parent in self.parents:\n", | |
" parent.get_upstream_metrics(upstream_metrics)\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return self.name\n", | |
"\n", | |
" def get_formula(self):\n", | |
" return self.name\n", | |
"\n", | |
"class DerivedMetric(BaseMetric):\n", | |
" \"\"\"\n", | |
" Metrics calculated from upstream 'parent' metrics. The upstream metrics can be\n", | |
" derived metrics themselves or root/base metrics.\n", | |
" \"\"\"\n", | |
"\n", | |
" def __init__(self, name, operation, *parents):\n", | |
" super().__init__(name)\n", | |
" self.operation = operation\n", | |
"\n", | |
" # add operands to list of current metric's parents\n", | |
" self.parents = list(parents)\n", | |
"\n", | |
" # add current metric as child of each operand\n", | |
" for parent in self.parents:\n", | |
" parent.children.append(self)\n", | |
" \n", | |
" # add base or root metrics to list of current metric's root metrics\n", | |
" root_parent_set = set()\n", | |
" for parent in self.parents:\n", | |
" parent.get_upstream_metrics(root_parent_set)\n", | |
" \n", | |
" self.root_parents = list(root_parent_set)\n", | |
"\n", | |
" def calculate(self):\n", | |
" # if a derived metric's value is already available, just return it\n", | |
" if self.value:\n", | |
" return self.value\n", | |
" # otherwise calculate it from its upstream parent metrics\n", | |
" values = [parent.calculate() for parent in self.parents]\n", | |
" return self.operation(*values)\n", | |
"\n", | |
" def get_formula(self):\n", | |
" return inspect.getsource(self.operation).strip().rstrip(',')\n", | |
"\n", | |
"class MetricGraph:\n", | |
" \"\"\"\n", | |
" Represents the known universe of available metrics.\n", | |
" \"\"\"\n", | |
"\n", | |
" def __init__(self):\n", | |
" self.nodes = {}\n", | |
"\n", | |
" def add_metric(self, metric):\n", | |
" self.nodes[metric.name] = metric\n", | |
"\n", | |
" def calculate_metric(self, metric_name):\n", | |
" metric = self.nodes.get(metric_name.lower())\n", | |
" if metric:\n", | |
" return metric.calculate()\n", | |
" return None\n", | |
"\n", | |
" def list_extended_downstream_metrics(self, available_metrics, debug=False):\n", | |
"\n", | |
" available_metrics = set(metric.lower() for metric in set(available_metrics))\n", | |
" downstream_metrics = set()\n", | |
"\n", | |
" for metric_name in available_metrics:\n", | |
" metric = self.nodes.get(metric_name.lower())\n", | |
"\n", | |
" if metric:\n", | |
" metric.get_downstream_metrics(downstream_metrics)\n", | |
" \n", | |
" if debug:\n", | |
" print(\n", | |
" f'current metric: {metric.name}',\n", | |
" f'downstream metrics so far: {downstream_metrics}',\n", | |
" sep='\\n'\n", | |
" )\n", | |
"\n", | |
" # lists all downstream metrics calculated from the available metrics,\n", | |
" # possibly together with other metrics not listed as available \n", | |
" return [metric.name for metric in downstream_metrics\n", | |
" if metric.name not in available_metrics]\n", | |
"\n", | |
" def list_direct_downstream_metrics(self, available_metrics, debug=False):\n", | |
"\n", | |
" available_metrics = set(metric.lower() for metric in set(available_metrics))\n", | |
" downstream_metrics = self.list_extended_downstream_metrics(available_metrics)\n", | |
" direct_downstream_metrics = set()\n", | |
"\n", | |
" for metric_name in downstream_metrics:\n", | |
" metric = self.nodes.get(metric_name.lower())\n", | |
"\n", | |
" # directly downstream if it can be calculated from just the available metrics\n", | |
" if metric:\n", | |
" # check if its derived parents are subset of available metrics\n", | |
" derived_parent_names = set([parent_metric.name for parent_metric in metric.parents])\n", | |
" # check if its root parents are subset of available metrics\n", | |
" root_parent_names = set([parent_metric.name for parent_metric in metric.root_parents])\n", | |
"\n", | |
" if debug:\n", | |
" print(\n", | |
" f'current metric: {metric.name}',\n", | |
" f'derived parents: {derived_parent_names}',\n", | |
" f'root parents: {root_parent_names}',\n", | |
" f'available metrics: {available_metrics}',\n", | |
" sep='\\n'\n", | |
" )\n", | |
"\n", | |
" if derived_parent_names.issubset(available_metrics) or \\\n", | |
" root_parent_names.issubset(available_metrics):\n", | |
" direct_downstream_metrics.add(metric)\n", | |
"\n", | |
" return [metric.name for metric in direct_downstream_metrics]\n", | |
"\n", | |
" def list_upstream_metrics(self, requested_metrics):\n", | |
" \n", | |
" requested_metrics = set(metric.lower() for metric in set(requested_metrics))\n", | |
" upstream_metrics = set()\n", | |
"\n", | |
" for metric_name in requested_metrics:\n", | |
" metric = self.nodes.get(metric_name.lower())\n", | |
" if metric:\n", | |
" metric.get_upstream_metrics(upstream_metrics)\n", | |
"\n", | |
" return [metric.name for metric in upstream_metrics]\n" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Define metrics and known universe of available metrics" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# define base or root metrics (arbitrary since these can be further decomposed)\n", | |
"revenue = BaseMetric(\"revenue\")\n", | |
"operating_expenses = BaseMetric(\"operating expenses\")\n", | |
"interest = BaseMetric(\"interest\")\n", | |
"taxes = BaseMetric(\"taxes\")\n", | |
"non_cash_expenses = BaseMetric(\"non cash expenses\")\n", | |
"increase_in_working_capital = BaseMetric(\"increase in working capital\")\n", | |
"capital_expenditures = BaseMetric(\"capital expenditures\")\n", | |
"\n", | |
"# define derived or calculated metrics: calculated from root metrics or upstream derived ones\n", | |
"operating_profit = DerivedMetric(\"operating profit\", \n", | |
" lambda rev, opex: rev - opex, \n", | |
" revenue, operating_expenses)\n", | |
"\n", | |
"operating_margin = DerivedMetric(\"operating margin\", \n", | |
" lambda op_profit, rev: op_profit / rev, \n", | |
" operating_profit, revenue)\n", | |
"net_profit = DerivedMetric(\"net profit\", \n", | |
" lambda op_profit, interest, taxes: op_profit - interest - taxes, \n", | |
" operating_profit, interest, taxes)\n", | |
"\n", | |
"operating_cash_flow = DerivedMetric(\"operating cash flow\", \n", | |
" lambda net_profit, noncash_exp, change_wc: net_profit + noncash_exp - change_wc,\n", | |
" net_profit, non_cash_expenses, increase_in_working_capital)\n", | |
"\n", | |
"free_cash_flow = DerivedMetric(\"free cash flow\", \n", | |
" lambda op_cash, capex: op_cash - capex, \n", | |
" operating_cash_flow, capital_expenditures)\n", | |
"\n", | |
"# metric graph represents universe of known metrics\n", | |
"graph = MetricGraph()\n", | |
"graph.add_metric(revenue)\n", | |
"graph.add_metric(operating_expenses)\n", | |
"graph.add_metric(interest)\n", | |
"graph.add_metric(taxes)\n", | |
"graph.add_metric(non_cash_expenses)\n", | |
"graph.add_metric(increase_in_working_capital)\n", | |
"graph.add_metric(capital_expenditures)\n", | |
"graph.add_metric(operating_profit)\n", | |
"graph.add_metric(operating_margin)\n", | |
"graph.add_metric(net_profit)\n", | |
"graph.add_metric(operating_cash_flow)\n", | |
"graph.add_metric(free_cash_flow)\n" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### List upstream metrics required to calculate given set of requested metrics" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"To calculate:\n", | |
"\t{'operating profit'}\n", | |
"... the following metrics are required:\n", | |
"\t['revenue', 'operating expenses']\n", | |
"\n", | |
"To calculate:\n", | |
"\t{'operating margin'}\n", | |
"... the following metrics are required:\n", | |
"\t['revenue', 'operating expenses']\n", | |
"\n", | |
"To calculate:\n", | |
"\t{'operating margin', 'net profit'}\n", | |
"... the following metrics are required:\n", | |
"\t['revenue', 'taxes', 'operating expenses', 'interest']\n", | |
"\n", | |
"To calculate:\n", | |
"\t{'operating cash flow'}\n", | |
"... the following metrics are required:\n", | |
"\t['non cash expenses', 'taxes', 'increase in working capital', 'revenue', 'operating expenses', 'interest']\n", | |
"\n", | |
"To calculate:\n", | |
"\t{'free cash flow', 'operating margin', 'net profit'}\n", | |
"... the following metrics are required:\n", | |
"\t['non cash expenses', 'taxes', 'increase in working capital', 'capital expenditures', 'revenue', 'operating expenses', 'interest']\n" | |
] | |
} | |
], | |
"source": [ | |
"# Calculate upstream metrics required to calculate a set of metrics\n", | |
"print(\n", | |
" \"\\nTo calculate:\",\n", | |
" \"\\t\" + str(requested_metrics := {\n", | |
" \"operating profit\"\n", | |
" }),\n", | |
" \"... the following metrics are required:\",\n", | |
" f\"\\t{graph.list_upstream_metrics(requested_metrics)}\",\n", | |
"\n", | |
" \"\\nTo calculate:\",\n", | |
" \"\\t\" + str(requested_metrics := {\n", | |
" \"operating margin\"\n", | |
" }),\n", | |
" \"... the following metrics are required:\",\n", | |
" f\"\\t{graph.list_upstream_metrics(requested_metrics)}\",\n", | |
"\n", | |
" \"\\nTo calculate:\",\n", | |
" \"\\t\" + str(requested_metrics := {\n", | |
" \"operating margin\", \"net profit\"\n", | |
" }),\n", | |
" \"... the following metrics are required:\",\n", | |
" f\"\\t{graph.list_upstream_metrics(requested_metrics)}\",\n", | |
"\n", | |
" \"\\nTo calculate:\",\n", | |
" \"\\t\" + str(requested_metrics := {\n", | |
" \"operating cash flow\"\n", | |
" }),\n", | |
" \"... the following metrics are required:\",\n", | |
" f\"\\t{graph.list_upstream_metrics(requested_metrics)}\",\n", | |
"\n", | |
" \"\\nTo calculate:\",\n", | |
" \"\\t\" + str(requested_metrics := {\n", | |
" \"net profit\", \"operating margin\", \"free cash flow\"\n", | |
" }),\n", | |
" \"... the following metrics are required:\",\n", | |
" f\"\\t{graph.list_upstream_metrics(requested_metrics)}\",\n", | |
" \n", | |
" sep='\\n'\n", | |
")" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### List metrics with formulas involving terms in a given set of metrics" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"The following metrics:\n", | |
"\t{'operating expenses'}\n", | |
"... are referenced in the formulas for these metrics:\n", | |
"\t['net profit', 'operating profit', 'operating cash flow', 'free cash flow', 'operating margin']\n", | |
"\n", | |
"The following metrics:\n", | |
"\t{'revenue', 'operating expenses'}\n", | |
"... are referenced in the formulas for these metrics:\n", | |
"\t['net profit', 'operating profit', 'operating cash flow', 'free cash flow', 'operating margin']\n", | |
"\n", | |
"The following metrics:\n", | |
"\t{'taxes'}\n", | |
"... are referenced in the formulas for these metrics:\n", | |
"\t['free cash flow', 'operating cash flow', 'net profit']\n", | |
"\n", | |
"The following metrics:\n", | |
"\t{'capital expenditures'}\n", | |
"... are referenced in the formulas for these metrics:\n", | |
"\t['free cash flow']\n", | |
"\n", | |
"The following metrics:\n", | |
"\t{'revenue', 'taxes', 'operating expenses', 'interest'}\n", | |
"... are referenced in the formulas for these metrics:\n", | |
"\t['net profit', 'operating profit', 'operating cash flow', 'free cash flow', 'operating margin']\n" | |
] | |
} | |
], | |
"source": [ | |
"# get all downstream metrics that can be derived from a list of available metrics,\n", | |
"# including those requiring supplementary metrics not listed as available\n", | |
"\n", | |
"print(\n", | |
" \"\\nThe following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\"\n", | |
" }),\n", | |
" \"... are referenced in the formulas for these metrics:\",\n", | |
" f\"\\t{graph.list_extended_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nThe following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\", \"revenue\"\n", | |
" }),\n", | |
" \"... are referenced in the formulas for these metrics:\",\n", | |
" f\"\\t{graph.list_extended_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nThe following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"taxes\"\n", | |
" }),\n", | |
" \"... are referenced in the formulas for these metrics:\",\n", | |
" f\"\\t{graph.list_extended_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nThe following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"capital expenditures\"\n", | |
" }),\n", | |
" \"... are referenced in the formulas for these metrics:\",\n", | |
" f\"\\t{graph.list_extended_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nThe following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\", \"revenue\", \"interest\", \"taxes\"\n", | |
" }),\n", | |
" \"... are referenced in the formulas for these metrics:\",\n", | |
" f\"\\t{graph.list_extended_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" sep='\\n'\n", | |
")" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### List downstream metrics calculated from just the given set of metrics" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'taxes'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t[]\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'capital expenditures'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t[]\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'operating expenses'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t[]\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'revenue', 'operating expenses'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t['operating profit', 'operating margin']\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'revenue', 'taxes', 'operating expenses', 'interest'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t['operating profit', 'operating margin', 'net profit']\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'taxes', 'net profit', 'interest'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t[]\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'increase in working capital', 'net profit', 'non cash expenses'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t['operating cash flow']\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'increase in working capital', 'interest', 'taxes', 'operating expenses', 'non cash expenses', 'revenue'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t['operating margin', 'operating profit', 'operating cash flow', 'net profit']\n", | |
"\n", | |
"Given only the following metrics:\n", | |
"\t{'increase in working capital', 'interest', 'taxes', 'operating expenses', 'non cash expenses', 'revenue', 'capital expenditures'}\n", | |
"... we can directly calculate these downstream metrics:\n", | |
"\t['net profit', 'operating profit', 'free cash flow', 'operating cash flow', 'operating margin']\n" | |
] | |
} | |
], | |
"source": [ | |
"# get the list of downstream metrics that can be calculated directly from a given\n", | |
"# set of input metrics, without requiring any other metrics\n", | |
"\n", | |
"print(\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"taxes\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"capital expenditures\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\", \"revenue\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\", \"revenue\", \"interest\", \"taxes\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"net profit\", \"interest\", \"taxes\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"net profit\", \"non cash expenses\", \"increase in working capital\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\", \"revenue\", \"interest\", \"taxes\", \"non cash expenses\", \"increase in working capital\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" \"\\nGiven only the following metrics:\",\n", | |
" \"\\t\" + str(available_metrics := {\n", | |
" \"operating expenses\", \"revenue\", \"interest\", \"taxes\", \"non cash expenses\", \"increase in working capital\", \"capital expenditures\"\n", | |
" }),\n", | |
" \"... we can directly calculate these downstream metrics:\",\n", | |
" f\"\\t{graph.list_direct_downstream_metrics(available_metrics)}\",\n", | |
"\n", | |
" sep='\\n'\n", | |
")" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Calculations involving concrete values of metrics" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 93, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"operating cash flow when values of upstream derived metrics are set:\n", | |
"operating cash flow = lambda net_profit, noncash_exp, change_wc: net_profit + noncash_exp - change_wc --> 78\n", | |
"operating cash flow when values of upstream root metrics are set:\n", | |
"operating cash flow = lambda net_profit, noncash_exp, change_wc: net_profit + noncash_exp - change_wc --> 54\n" | |
] | |
} | |
], | |
"source": [ | |
"net_profit.value, non_cash_expenses.value, increase_in_working_capital.value = 90, 24, 36\n", | |
"\n", | |
"print(\n", | |
" 'operating cash flow when values of upstream derived metrics are set:',\n", | |
" operating_cash_flow.name + ' = ' + operating_cash_flow.get_formula() + ' --> ' + str(operating_cash_flow.calculate()),\n", | |
" sep='\\n'\n", | |
")\n", | |
"\n", | |
"revenue.value, operating_expenses.value, interest.value, taxes.value = 150, 40, 18, 26\n", | |
"net_profit.value, non_cash_expenses.value, increase_in_working_capital.value = None, 24, 36\n", | |
"\n", | |
"print(\n", | |
" 'operating cash flow when values of upstream root metrics are set:',\n", | |
" operating_cash_flow.name + ' = ' + operating_cash_flow.get_formula() + ' --> ' + str(operating_cash_flow.calculate()),\n", | |
" sep='\\n'\n", | |
")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "llama_index_3.10", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.11" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment