Skip to main content

Guide


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:

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

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

Expected Value of the best strategy: 86.00
Optimal branch: {'Unfavorable 1', 'Favorable 1', 'Big Factory 1', 'Without Analytics', 'Decision'}

Metadata

Release files for precision-tree 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for precision-tree 0.1.3
File Size Uploaded Provenance Actions
precision_tree-0.1.3.tar.gz 5.9 kB Without provenance Details for precision_tree-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for precision-tree 0.1.3
File Interpreter ABI Platform Actions
precision_tree-0.1.3-py3-none-any.whl Python 3 none any Details for precision_tree-0.1.3-py3-none-any.whl

Total release size: 12.1 kB

Release files / precision_tree-0.1.3.tar.gz

Download URL precision_tree-0.1.3.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9588577b86a5f64558d7cfbcf43f25bd56e484ca53b73962e500a9bfcccd3460
BLAKE2b-256 checksum
How to use checksums
9808195ccb5bf770290c8fa4f4ff62500e822d99d21a855a96e909cd2314544e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / precision_tree-0.1.3-py3-none-any.whl

Download URL precision_tree-0.1.3-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0628440ccce74717b6a81b6ffe7080b2dbc410f0c3fb64cdf36c9eb535354868
BLAKE2b-256 checksum
How to use checksums
de36115873125da25340bc833a7c7284bb52ed1409f86425fb0da49dc88ec8a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Security for precision-tree 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page