Skip to main content

A rule-based decision tree implementation for Python

Project description

PyDecisionGraph

PyDecisionGraph is an easy-to-use library to create custom decision trees, primarily designed for trading and financial decision-making. This package helps you build rule-based decision processes and visualize them effectively.

Installation

To install the package, run:

pip install PyDecisionGraph

Requirements

  • Python 3.12 or higher

Documentation

For detailed documentation, visit https://pydecisiongraph.readthedocs.io/.

Quick Start

Here is a quick demo on how to use PyDecisionGraph for building a decision tree based on various conditions:

from decision_graph.decision_tree import LogicNode, LOGGER, AttrExpression, LongAction, ShortAction, NoAction, RootLogicNode, LogicMapping

# Mapping of attribute names to their values
LogicMapping.AttrExpression = AttrExpression

state = {
    "exposure": 0,  # Current exposure
    "working_order": 0,  # Current working order
    "up_prob": 0.8,  # Probability of price going up
    "down_prob": 0.2,  # Probability of price going down
    "volatility": 0.24,  # Current market volatility
    "ttl": 15.3  # Time to live (TTL) of the decision tree
}

# Root of the logic tree
with RootLogicNode() as root:
    # Define root logic mapping with state data
    with LogicMapping(name='Root', data=state) as lg_root:
        lg_root: LogicMapping

        # Condition for zero exposure
        with lg_root.exposure == 0:
            root: LogicNode
            with LogicMapping(name='check_open', data=state) as lg:
                with lg.working_order != 0:
                    break_point = NoAction()  # No action if there's a working order
                    lg.break_(scope=lg)  # Exit the current scope

                with lg.volatility > 0.25:  # Check if volatility is high
                    with lg.down_prob > 0.1:  # Action for down probability
                        LongAction()

                    with lg.up_prob < -0.1:  # Action for up probability
                        ShortAction()

        # Condition when TTL is greater than 30
        with lg_root.ttl > 30:
            with lg_root.working_order > 0:
                ShortAction()  # Action to short if working order exists
            LongAction()  # Always take long action
            lg_root.break_(scope=lg_root)  # Exit scope

        # Closing logic based on exposure and probabilities
        with LogicMapping(name='check_close', data=state) as lg:
            with (lg.exposure > 0) & (lg.down_prob > 0.):
                ShortAction()  # Short action for positive exposure and down probability

            with (lg.exposure < 0) & (lg.up_prob > 0.):
                LongAction()  # Long action for negative exposure and up probability

# Visualize the decision tree
root.to_html()

# Log the evaluation result
LOGGER.info(root())

Explanation of the Script:

  • LogicNode & LogicMapping:

    • LogicNode: Represents a node in the decision tree where conditions are evaluated.
    • LogicMapping: Associates logical conditions with the state (data) used in decision-making.
  • State:

    • A dictionary containing the variables used for decision-making, such as exposure, working_order, up_prob, etc.
  • RootLogicNode:

    • The entry point for the decision tree where all logical decisions are linked.
  • Decision Conditions:

    • Inside each with block, logical conditions are evaluated (e.g., lg.volatility > 0.25, lg.up_prob < -0.1) to determine which action to take.
    • Actions like LongAction() or ShortAction() are taken based on the conditions.
  • Action Handling:

    • LongAction(), ShortAction(), and NoAction() represent different actions you can trigger in the decision tree based on the conditions.
  • Logging:

    • The result of the tree evaluation is logged using the LOGGER object, which outputs to the console.
  • Visualization:

    • root.to_html() generates an HTML representation of the decision tree for visualization.

Features

  • Easily define custom decision rules.
  • Actionable outcomes like LongAction, ShortAction, and NoAction.
  • Log outputs for debugging and tracking.
  • Visualize decision paths through HTML export.

Contributing

Feel free to fork, submit issues, and create pull requests. Contributions are always welcome!

License

This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pydecisiongraph-0.2.9-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pydecisiongraph-0.2.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pydecisiongraph-0.2.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pydecisiongraph-0.2.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file pydecisiongraph-0.2.9-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pydecisiongraph-0.2.9-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3853b9c8028317b55bda5b1920103b607fc8d210946f98e5843576c09c93df85
MD5 365eea2e9065d9a34645c78d0c5b8428
BLAKE2b-256 a5169bdb87e33acace84a237ee2a3f41f57e0c661dabbb58215973cafae3fd0b

See more details on using hashes here.

File details

Details for the file pydecisiongraph-0.2.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pydecisiongraph-0.2.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1c99e029e2d0c9fd7621f8a3ff48cfc6b8b3d121e14bec11bd1e82013a13e9ff
MD5 e5aeb3c80b128400d44f515c3a8daa31
BLAKE2b-256 61cf9df81b790a4a4a94d8b7b500ed83a01a4c95aa8207d9b77b6dde13b04376

See more details on using hashes here.

File details

Details for the file pydecisiongraph-0.2.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pydecisiongraph-0.2.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7873633f7aa3d3e2e46992477a07ff12a8a76503ca48c880ea25391d734d62b8
MD5 9085ba27bd38cda80087ce506906a58f
BLAKE2b-256 718172db8107a322254dcbf242b6341b8e1aa92f7c01d9dd59aa87b9b7bf2fc5

See more details on using hashes here.

File details

Details for the file pydecisiongraph-0.2.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pydecisiongraph-0.2.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b244ebd9995dfd8215bf8aeba11a882365a62d0e10ca253e881f75229db634b3
MD5 4789c7f8eb402addfa9fd42f43f186ce
BLAKE2b-256 40397a16c4da37e2cf829856a36bc999f0d708de270e3183b52d845f32a4edf5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page