# Guide
- [Start](#start)
- [Modules](#modules)
- [Nodes](#nodes)
- [Tree](#tree)
- [Usage](#usage)
---
## Start
This is a library to create decision trees and calculate the expected value of the best strategy.
To install the library, run the following command:
```bash
pip install precision-tree
```
---
## Modules
The library has two main modules: `nodes` and `tree`.
The `nodes` module contains the classes to create the nodes of the decision tree. The `tree` module contains the class to wrap the root node and calculate the expected value of the best strategy and visualize the tree.
### Nodes
The `nodes` module contains the following classes:
- `DecisionNode`: Represents a decision node.
- `ChanceNode`: Represents a chance node.
- `PayoffNode`: Represents a payoff node.
All of the classes are subclasses of the `Node` class.
#### DecisionNode
The `DecisionNode` class represents a decision node (in graph it looks like a squere).
The following table shows the attributes of the `DecisionNode` class:
| Attribute | Description |
| --- | --- |
| `name` | The name of the node. |
| `branches` | A list of branches. |
The following table shows the methods of the `DecisionNode` class:
| Method | Description |
| --- | --- |
| `add_branch(self, label, child)` | Adds a branch to the node. `label` is the name of the branch and `child` is the child node. |
| `calculate_value` | Calculates the expected value of the best strategy. |
#### ChanceNode
The `ChanceNode` class represents a chance node (in graph it looks like a circle).
The following table shows the attributes of the `ChanceNode` class:
| Attribute | Description |
| --- | --- |
| `name` | The name of the node. |
| `cost` | The cost of the node. |
| `years` | The number of years. |
| `branches` | A list of branches. |
The following table shows the methods of the `ChanceNode` class:
| Method | Description |
| --- | --- |
| `add_branch(self, label, child, probability)` | Adds a branch to the node. `label` is the name of the branch, `child` is the child node, and `probability` is the probability of the branch. |
| `calculate_value` | Calculates the expected value of the best strategy. |
#### PayoffNode
The `PayoffNode` class represents a payoff node (in graph it looks like a triangle).
The following table shows the attributes of the `PayoffNode` class:
| Attribute | Description |
| --- | --- |
| `name` | The name of the node. |
| `value` | The value of the node. |
| `branches` | A list of branches. |
The following table shows the methods of the `PayoffNode` class:
| Method | Description |
| --- | --- |
| `calculate_value` | Calculates the expected value of the best strategy. |
### Tree
The `tree` module contains the `TreeWrapper` class.
The following table shows the attributes of the `TreeWrapper` class:
| Attribute | Description |
| --- | --- |
| `root` | The root node of the tree. |
The following table shows the methods of the `TreeWrapper` class:
| Method | Description |
| --- | --- |
| `show(self, title: str, format: str):` | Shows the tree. `title` is the title of the tree and `format` is the format of the file `png`, `pdf` etc. |
| `get_optimal_path` | Returns the optimal path as a tuple, where the first element is the optimal nodes and second element is the optimal edges. |
| `calculate_value` | Calculates the expected value of the best strategy. |
---
## Usage
**Example**
```python
from precision_tree.nodes import DecisionNode, ChanceNode, PayoffNode
from precision_tree.tree import TreeWrapper
root = DecisionNode("Start Decision")
# Big Factory
big_factory = ChanceNode("Big Factory", cost=700, years=5)
high_demand = PayoffNode("High Demand", 280)
low_demand = PayoffNode("Low Demand", -80)
big_factory.add_branch("High Demand", high_demand, 0.8)
big_factory.add_branch("Low Demand", low_demand, 0.2)
# Small Factory
small_factory = ChanceNode("Small Factory", cost=300, years=5)
high_demand_small = PayoffNode("High Demand Small", 180)
low_demand_small = PayoffNode("Low Demand Small", -55)
small_factory.add_branch("High Demand", high_demand_small, 0.8)
small_factory.add_branch("Low Demand", low_demand_small, 0.2)
# Stop by Year
stop_by_year = DecisionNode("Stop by Year")
negative_info = PayoffNode("Negative Info", 0)
positive_info = DecisionNode("Positive Info")
big_factory_1 = ChanceNode("Big Factory 1", cost=700, years=4)
high_demand_1 = PayoffNode("High Demand 1", 280)
low_demand_1 = PayoffNode("Low Demand 1", -80)
big_factory_1.add_branch("High Demand 1", high_demand_1, 0.9)
big_factory_1.add_branch("Low Demand 1", low_demand_1, 0.1)
small_factory_1 = ChanceNode("Small Factory 1", cost=300, years=4)
high_demand_small_1 = PayoffNode("High Demand Small 1", 1800)
low_demand_small_1 = PayoffNode("Low Demand Small 1", -55)
small_factory_1.add_branch("High Demand 1", high_demand_small_1, 0.9)
small_factory_1.add_branch("Low Demand 1", low_demand_small_1, 0.1)
positive_info.add_branch("Big Factory", big_factory_1)
positive_info.add_branch("Small Factory", small_factory_1)
stop_by_year.add_branch("Positive Info", positive_info, 0.7)
stop_by_year.add_branch("Negative Info", negative_info, 0.3)
root.add_branch("Big Factory", big_factory)
root.add_branch("Small Factory", small_factory)
root.add_branch("Stop by Year", stop_by_year)
tree = TreeWrapper(root)
print(f"Expected Value of the best strategy: {tree.calculate_value():.2f}")
print(f"Optimal branch: {tree.get_optimal_path()[0]}")
```
**Output**
```bash
Expected Value of the best strategy: 86.00
Optimal branch: {'Unfavorable 1', 'Favorable 1', 'Big Factory 1', 'Without Analytics', 'Decision'}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/danylevych/precision-tree",
"name": "precision-tree",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Oleh Danylevych",
"author_email": "danylevych123@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/98/08/195ccb5bf770290c8fa4f4ff62500e822d99d21a855a96e909cd2314544e/precision_tree-0.1.3.tar.gz",
"platform": null,
"description": "# Guide\n - [Start](#start)\n - [Modules](#modules)\n - [Nodes](#nodes)\n - [Tree](#tree)\n - [Usage](#usage)\n\n---\n\n## Start\n\nThis is a library to create decision trees and calculate the expected value of the best strategy.\n\nTo install the library, run the following command:\n```bash\npip install precision-tree\n```\n\n---\n\n## Modules\n\nThe library has two main modules: `nodes` and `tree`.\n\nThe `nodes` module contains the classes to create the nodes of the decision tree. The `tree` module contains the class to wrap the root node and calculate the expected value of the best strategy and visualize the tree.\n\n### Nodes\n\nThe `nodes` module contains the following classes:\n\n- `DecisionNode`: Represents a decision node.\n\n- `ChanceNode`: Represents a chance node.\n\n- `PayoffNode`: Represents a payoff node.\n\nAll of the classes are subclasses of the `Node` class.\n\n#### DecisionNode\n\nThe `DecisionNode` class represents a decision node (in graph it looks like a squere).\n\nThe following table shows the attributes of the `DecisionNode` class:\n\n| Attribute | Description |\n| --- | --- |\n| `name` | The name of the node. |\n| `branches` | A list of branches. |\n\n\nThe following table shows the methods of the `DecisionNode` class:\n\n| Method | Description |\n| --- | --- |\n| `add_branch(self, label, child)` | Adds a branch to the node. `label` is the name of the branch and `child` is the child node. |\n| `calculate_value` | Calculates the expected value of the best strategy. |\n\n\n#### ChanceNode\n\nThe `ChanceNode` class represents a chance node (in graph it looks like a circle).\n\nThe following table shows the attributes of the `ChanceNode` class:\n\n| Attribute | Description |\n| --- | --- |\n| `name` | The name of the node. |\n| `cost` | The cost of the node. |\n| `years` | The number of years. |\n| `branches` | A list of branches. |\n\nThe following table shows the methods of the `ChanceNode` class:\n\n| Method | Description |\n| --- | --- |\n| `add_branch(self, label, child, probability)` | Adds a branch to the node. `label` is the name of the branch, `child` is the child node, and `probability` is the probability of the branch. |\n| `calculate_value` | Calculates the expected value of the best strategy. |\n\n#### PayoffNode\n\nThe `PayoffNode` class represents a payoff node (in graph it looks like a triangle).\n\nThe following table shows the attributes of the `PayoffNode` class:\n\n| Attribute | Description |\n| --- | --- |\n| `name` | The name of the node. |\n| `value` | The value of the node. |\n| `branches` | A list of branches. |\n\nThe following table shows the methods of the `PayoffNode` class:\n\n| Method | Description |\n| --- | --- |\n| `calculate_value` | Calculates the expected value of the best strategy. |\n\n### Tree\n\nThe `tree` module contains the `TreeWrapper` class.\n\nThe following table shows the attributes of the `TreeWrapper` class:\n\n| Attribute | Description |\n| --- | --- |\n| `root` | The root node of the tree. |\n\nThe following table shows the methods of the `TreeWrapper` class:\n\n| Method | Description |\n| --- | --- |\n| `show(self, title: str, format: str):` | Shows the tree. `title` is the title of the tree and `format` is the format of the file `png`, `pdf` etc. |\n| `get_optimal_path` | Returns the optimal path as a tuple, where the first element is the optimal nodes and second element is the optimal edges. |\n| `calculate_value` | Calculates the expected value of the best strategy. |\n\n\n---\n\n## Usage\n\n\n**Example**\n```python\nfrom precision_tree.nodes import DecisionNode, ChanceNode, PayoffNode\nfrom precision_tree.tree import TreeWrapper\n\nroot = DecisionNode(\"Start Decision\")\n\n# Big Factory\nbig_factory = ChanceNode(\"Big Factory\", cost=700, years=5)\nhigh_demand = PayoffNode(\"High Demand\", 280)\nlow_demand = PayoffNode(\"Low Demand\", -80)\nbig_factory.add_branch(\"High Demand\", high_demand, 0.8)\nbig_factory.add_branch(\"Low Demand\", low_demand, 0.2)\n\n# Small Factory\nsmall_factory = ChanceNode(\"Small Factory\", cost=300, years=5)\nhigh_demand_small = PayoffNode(\"High Demand Small\", 180)\nlow_demand_small = PayoffNode(\"Low Demand Small\", -55)\nsmall_factory.add_branch(\"High Demand\", high_demand_small, 0.8)\nsmall_factory.add_branch(\"Low Demand\", low_demand_small, 0.2)\n\n# Stop by Year\nstop_by_year = DecisionNode(\"Stop by Year\")\nnegative_info = PayoffNode(\"Negative Info\", 0)\n\npositive_info = DecisionNode(\"Positive Info\")\nbig_factory_1 = ChanceNode(\"Big Factory 1\", cost=700, years=4)\nhigh_demand_1 = PayoffNode(\"High Demand 1\", 280)\nlow_demand_1 = PayoffNode(\"Low Demand 1\", -80)\nbig_factory_1.add_branch(\"High Demand 1\", high_demand_1, 0.9)\nbig_factory_1.add_branch(\"Low Demand 1\", low_demand_1, 0.1)\n\nsmall_factory_1 = ChanceNode(\"Small Factory 1\", cost=300, years=4)\nhigh_demand_small_1 = PayoffNode(\"High Demand Small 1\", 1800)\nlow_demand_small_1 = PayoffNode(\"Low Demand Small 1\", -55)\nsmall_factory_1.add_branch(\"High Demand 1\", high_demand_small_1, 0.9)\nsmall_factory_1.add_branch(\"Low Demand 1\", low_demand_small_1, 0.1)\n\npositive_info.add_branch(\"Big Factory\", big_factory_1)\npositive_info.add_branch(\"Small Factory\", small_factory_1)\n\nstop_by_year.add_branch(\"Positive Info\", positive_info, 0.7)\nstop_by_year.add_branch(\"Negative Info\", negative_info, 0.3)\n\nroot.add_branch(\"Big Factory\", big_factory)\nroot.add_branch(\"Small Factory\", small_factory)\nroot.add_branch(\"Stop by Year\", stop_by_year)\n\ntree = TreeWrapper(root)\nprint(f\"Expected Value of the best strategy: {tree.calculate_value():.2f}\")\nprint(f\"Optimal branch: {tree.get_optimal_path()[0]}\")\n```\n\n**Output**\n\n```bash\nExpected Value of the best strategy: 86.00\nOptimal branch: {'Unfavorable 1', 'Favorable 1', 'Big Factory 1', 'Without Analytics', 'Decision'}\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Precision Tree Module for precision decision analysis, supporting custom nodes (Decision, Chance, and Payoff) with visualization and optimal path calculation.",
"version": "0.1.3",
"project_urls": {
"Homepage": "https://github.com/danylevych/precision-tree"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "de36115873125da25340bc833a7c7284bb52ed1409f86425fb0da49dc88ec8a8",
"md5": "3bc737f573696afced5f9b5464015581",
"sha256": "0628440ccce74717b6a81b6ffe7080b2dbc410f0c3fb64cdf36c9eb535354868"
},
"downloads": -1,
"filename": "precision_tree-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3bc737f573696afced5f9b5464015581",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6184,
"upload_time": "2024-11-26T18:54:05",
"upload_time_iso_8601": "2024-11-26T18:54:05.592101Z",
"url": "https://files.pythonhosted.org/packages/de/36/115873125da25340bc833a7c7284bb52ed1409f86425fb0da49dc88ec8a8/precision_tree-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9808195ccb5bf770290c8fa4f4ff62500e822d99d21a855a96e909cd2314544e",
"md5": "7b26346f1f22d49edf4d6da5ec8084b5",
"sha256": "9588577b86a5f64558d7cfbcf43f25bd56e484ca53b73962e500a9bfcccd3460"
},
"downloads": -1,
"filename": "precision_tree-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "7b26346f1f22d49edf4d6da5ec8084b5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5895,
"upload_time": "2024-11-26T18:54:08",
"upload_time_iso_8601": "2024-11-26T18:54:08.636836Z",
"url": "https://files.pythonhosted.org/packages/98/08/195ccb5bf770290c8fa4f4ff62500e822d99d21a855a96e909cd2314544e/precision_tree-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-26 18:54:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "danylevych",
"github_project": "precision-tree",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "graphviz",
"specs": [
[
"==",
"0.20.3"
]
]
},
{
"name": "setuptools",
"specs": [
[
"==",
"75.6.0"
]
]
}
],
"lcname": "precision-tree"
}