Struct stochasta::CardDrawTree
source · pub struct CardDrawTree<C>{ /* private fields */ }
Expand description
Implementations§
source§impl<C> CardDrawTree<C>
impl<C> CardDrawTree<C>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty tree.
§Example
use stochasta::CardDrawTree;
let tree: CardDrawTree<i32> = CardDrawTree::new();
assert!(tree.is_empty());
sourcepub fn create_from(card_deck: &CardDeck<C>) -> Self
pub fn create_from(card_deck: &CardDeck<C>) -> Self
Creates a tree with one level from the given card deck.
§Example
use stochasta::{CardDeck, CardDrawTree};
let deck = CardDeck::from(vec!["heads", "tails"]);
let tree = CardDrawTree::create_from(&deck);
sourcepub fn without_shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self
pub fn without_shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self
Creates a new tree with the number of draws
with an unshrinking stack.
The stack won’t shrink from drawing cards; instead every drawn card is put back to the stack.
For a shrinking deck, see Self::shrinking()
.
sourcepub fn shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self
pub fn shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self
Creates a new tree with the number of draws
with a shrinking stack.
The stack will shrink from drawing cards; once a card is drawn it is no longer part of the stack.
For a non-shrinking deck, see Self::without_shrinking()
.
sourcepub fn probability_of(&self, sequence: &[C]) -> Probability
pub fn probability_of(&self, sequence: &[C]) -> Probability
Returns the probability of a certain sequence in the tree.
The order is important as well as the position - the first entry will be searched among the root nodes.
§Example
use stochasta::{CardDeck, CardDrawTree, Probability, PROBABILITY_ONE, PROBABILITY_ZERO};
let coin = CardDeck::from(vec!["H", "T"]);
let tree = CardDrawTree::without_shrinking(&coin, 2);
assert_eq!(tree.probability_of(&[]), PROBABILITY_ONE);
assert_eq!(tree.probability_of(&["H"]), Probability::new(1, 2));
assert_eq!(tree.probability_of(&["H", "H"]), Probability::new(1, 4));
// 3x heads is impossible when only throwing 2x
assert_eq!(tree.probability_of(&["H", "H", "H"]), PROBABILITY_ZERO);
sourcepub fn paths(&self) -> Vec<CardDrawSequence<C>>
pub fn paths(&self) -> Vec<CardDrawSequence<C>>
Returns all paths.
§Example
use stochasta::{CardDeck, CardDrawTree, CardDrawSequence, Probability};
let coin = CardDeck::from(vec!["H", "T"]);
let tree = CardDrawTree::without_shrinking(&coin, 2);
let result = tree.paths();
let one_quarter = Probability::new(1, 4);
assert_eq!(result.len(), 4);
assert!(result.contains(&CardDrawSequence::new(vec!["H", "H"], one_quarter)));
assert!(result.contains(&CardDrawSequence::new(vec!["H", "T"], one_quarter)));
assert!(result.contains(&CardDrawSequence::new(vec!["T", "H"], one_quarter)));
assert!(result.contains(&CardDrawSequence::new(vec!["T", "T"], one_quarter)));
source§impl<C> CardDrawTree<C>
impl<C> CardDrawTree<C>
sourcepub fn to_graphviz(&self) -> String
pub fn to_graphviz(&self) -> String
Creates a Graphviz-graph from the decision tree.
§Example
For a more interesting graph this example covers an oddly weighted coin where heads is twice as likely to be thrown as tails.
use stochasta::{CardDeck, CardDrawTree};
let odd_coin = CardDeck::from(vec!["heads", "heads", "tails"]);
let tree = CardDrawTree::without_shrinking(&odd_coin, 2);
let output = r#"digraph {
_root[label="", shape="circle"];
_root->_heads_2[label="2/3"];
_heads_2[label="heads (2/3)"];
_heads_2->_heads_3[label="2/3"];
_heads_3[label="heads (4/9)"];
_heads_2->_tails_4[label="1/3"];
_tails_4[label="tails (2/9)"];
_root->_tails_5[label="1/3"];
_tails_5[label="tails (1/3)"];
_tails_5->_heads_6[label="2/3"];
_heads_6[label="heads (2/9)"];
_tails_5->_tails_7[label="1/3"];
_tails_7[label="tails (1/9)"];
}"#;
assert_eq!(tree.to_graphviz(), output);
§ASCII Visualisation
This will result in the following graph (here sideways for better visualisation):
2/3
+-----[ heads (4/9) ]
2/3 /
+-----[ heads (2/3) ]
/ \ 1/3
/ +-----[ tails (2/9) ]
/
O
\ 2/3
\ +-----[ heads (2/9) ]
\ 1/3 /
+-----[ tails (1/3) ]
\ 1/3
+-----[ tails (1/9) ]
§Output
- the paths have the probability from their parent node
- the cards have additionally the total probability to reach it from the root node in brackets
Trait Implementations§
source§impl<C> Clone for CardDrawTree<C>
impl<C> Clone for CardDrawTree<C>
source§fn clone(&self) -> CardDrawTree<C>
fn clone(&self) -> CardDrawTree<C>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<C> Debug for CardDrawTree<C>
impl<C> Debug for CardDrawTree<C>
source§impl<C> Default for CardDrawTree<C>
impl<C> Default for CardDrawTree<C>
source§impl<'de, C> Deserialize<'de> for CardDrawTree<C>
impl<'de, C> Deserialize<'de> for CardDrawTree<C>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<C> Display for CardDrawTree<C>
impl<C> Display for CardDrawTree<C>
source§impl<C> Hash for CardDrawTree<C>
impl<C> Hash for CardDrawTree<C>
source§impl<C> Ord for CardDrawTree<C>
impl<C> Ord for CardDrawTree<C>
source§fn cmp(&self, other: &CardDrawTree<C>) -> Ordering
fn cmp(&self, other: &CardDrawTree<C>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<C> PartialEq for CardDrawTree<C>
impl<C> PartialEq for CardDrawTree<C>
source§fn eq(&self, other: &CardDrawTree<C>) -> bool
fn eq(&self, other: &CardDrawTree<C>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<C> PartialOrd for CardDrawTree<C>
impl<C> PartialOrd for CardDrawTree<C>
source§fn partial_cmp(&self, other: &CardDrawTree<C>) -> Option<Ordering>
fn partial_cmp(&self, other: &CardDrawTree<C>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more