@@ -46,7 +46,9 @@ fn contextual_cancer_risk_logic(
4646
4747pub fn run_rung3_counterfactual ( ) {
4848 println ! ( "--- Rung 3: Counterfactual ---" ) ;
49- println ! ( "Query: Given a smoker with high tar, what would their cancer risk be if they hadn't smoked?" ) ;
49+ println ! (
50+ "Query: Given a smoker with high tar, what would their cancer risk be if they hadn't smoked?"
51+ ) ;
5052
5153 // 1. Define the Causaloid with our contextual logic
5254 let cancer_risk_causaloid = Causaloid :: new_with_context (
@@ -58,15 +60,28 @@ pub fn run_rung3_counterfactual() {
5860
5961 // 2. Create Factual Context: A person who smokes and has high tar.
6062 let mut factual_context = BaseContext :: with_capacity ( 1 , "Factual" , 5 ) ;
61- factual_context. add_node ( Contextoid :: new ( 1 , ContextoidType :: Datoid ( Data :: new ( NICOTINE_ID , 0.8 ) ) ) ) . unwrap ( ) ;
62- factual_context. add_node ( Contextoid :: new ( 2 , ContextoidType :: Datoid ( Data :: new ( TAR_ID , 0.8 ) ) ) ) . unwrap ( ) ;
63+ factual_context
64+ . add_node ( Contextoid :: new (
65+ 1 ,
66+ ContextoidType :: Datoid ( Data :: new ( NICOTINE_ID , 0.8 ) ) ,
67+ ) )
68+ . unwrap ( ) ;
69+ factual_context
70+ . add_node ( Contextoid :: new (
71+ 2 ,
72+ ContextoidType :: Datoid ( Data :: new ( TAR_ID , 0.8 ) ) ,
73+ ) )
74+ . unwrap ( ) ;
6375
6476 // 3. Create Counterfactual Context: Same person, but we hypothetically set smoking to zero.
6577 let mut counterfactual_context = factual_context. clone ( ) ;
6678 // To update, we need to know the index. In this simple case, it's 0.
6779 // A real implementation might use a HashMap<ID, Index> for lookup.
68- let new_nicotine_datoid = Contextoid :: new ( 1 , ContextoidType :: Datoid ( Data :: new ( NICOTINE_ID , 0.1 ) ) ) ;
69- counterfactual_context. update_node ( 1 , new_nicotine_datoid) . unwrap ( ) ;
80+ let new_nicotine_datoid =
81+ Contextoid :: new ( 1 , ContextoidType :: Datoid ( Data :: new ( NICOTINE_ID , 0.1 ) ) ) ;
82+ counterfactual_context
83+ . update_node ( 1 , new_nicotine_datoid)
84+ . unwrap ( ) ;
7085
7186 // 4. Evaluate Both Scenarios
7287 let mut factual_causaloid = cancer_risk_causaloid. clone ( ) ;
@@ -75,16 +90,28 @@ pub fn run_rung3_counterfactual() {
7590 let mut counterfactual_causaloid = cancer_risk_causaloid. clone ( ) ;
7691 counterfactual_causaloid. set_context ( Some ( Arc :: new ( counterfactual_context) ) ) ;
7792
78- let factual_risk = factual_causaloid. evaluate ( & PropagatingEffect :: None ) . unwrap ( ) ;
79- let counterfactual_risk = counterfactual_causaloid. evaluate ( & PropagatingEffect :: None ) . unwrap ( ) ;
93+ let factual_risk = factual_causaloid
94+ . evaluate ( & PropagatingEffect :: None )
95+ . unwrap ( ) ;
96+ let counterfactual_risk = counterfactual_causaloid
97+ . evaluate ( & PropagatingEffect :: None )
98+ . unwrap ( ) ;
8099
81100 // 5. Assert and Explain
82- println ! ( "Factual Result (smoker with high tar): Cancer risk is high -> {}" , factual_risk. as_bool( ) . unwrap( ) ) ;
83- println ! ( "Counterfactual Result (non-smoker with high tar): Cancer risk is high -> {}" , counterfactual_risk. as_bool( ) . unwrap( ) ) ;
101+ println ! (
102+ "Factual Result (smoker with high tar): Cancer risk is high -> {}" ,
103+ factual_risk. as_bool( ) . unwrap( )
104+ ) ;
105+ println ! (
106+ "Counterfactual Result (non-smoker with high tar): Cancer risk is high -> {}" ,
107+ counterfactual_risk. as_bool( ) . unwrap( )
108+ ) ;
84109
85110 assert_eq ! ( factual_risk, PropagatingEffect :: Deterministic ( true ) ) ;
86111 assert_eq ! ( counterfactual_risk, PropagatingEffect :: Deterministic ( true ) ) ;
87112
88- println ! ( "Conclusion: The cancer risk remains high in the counterfactual world because the direct cause (tar) was not undone." ) ;
113+ println ! (
114+ "Conclusion: The cancer risk remains high in the counterfactual world because the direct cause (tar) was not undone."
115+ ) ;
89116 println ! ( "\n " ) ;
90117}
0 commit comments