Files
boc/iom/intelligence/__pycache__/causal_engine.cpython-39.pyc
T

88 lines
9.2 KiB
Plaintext
Raw Normal View History

a
¶Y>jC3ã@sndZddlmZmZmZmZddlmZddlmZeGdddƒƒZ GdddƒZ
d d
Z e d krje ƒd S)
zQ
Causal Engine
Understands cause-and-effect relationships, not just correlations
é)ÚDictÚListÚOptionalÚTuple)Ú dataclass)Údatetimec@sTeZdZUdZeed<eed<eed<eed<eeed<eed<edœd d
Z d S) ÚCausalRelationshipz%A causal relationship between signalsÚcauseÚeffectÚstrengthÚ mechanismÚevidenceÚ
confidence©ÚreturncCs.|j|jt|jdƒ|jt|jƒt|jdƒdœS))r r
r r Zevidence_countr)r r
Úroundr r Úlenr
r©Úself©rúA/home/bernt/.openclaw/workspace/iom/intelligence/causal_engine.pyÚto_dicts

úzCausalRelationship.to_dictN)
Ú__name__Ú
__module__Ú __qualname__Ú__doc__ÚstrÚ__annotations__Úfloatrrrrrrrr s
 rc@eZdZdZddZedœddZeeeedœdd „Z ee
ed
œd d Z ee
ed
œddZ eeee
fedœddZee
e
e
edœddZe
edœddZdS)Ú CausalEnginez0Identifies causal relationships in urban systemscCs| ¡|_dS)N)Ú_build_causal_graphÚ causal_graphrrrrÚ__init__#szCausalEngine.__init__rc Cdddddœdddœgdœd d
gdœd œd d
dddœdddœgdœddgdœdddddœgdœdœdddddœgdœdddd dœgdœd!œd"d#gdœd$d%d&d'dœgdœd(d)gdœd*œd+d,d-d.dœgdœd/d0gdœd1œd2œS)3z-Build knowledge graph of causal relationshipsg¸…ëQ¸æ?zbBetter lighting increases visibility, deters criminal activity, and improves pedestrian confidencezChicago Alley Lightingz7% crime reduction)Ústudyr
zNYC Street Lightingz/36% reduction in outdoor nighttime index crimes)r r r
gÍÌÌÌÌÌä?zKImproved lighting extends usable hours for commercial and social activities)Ú safety_indexZnight_activityg=
×£p=ê?zPTrees provide shade and evapotranspiration, reducing surface and air temperaturezUrban Heat Islandu2-8°C cooling effectzBarcelona Tree Strategyu"4°C reduction in pedestrian areasgÂõ(\â?zFShaded walkways encourage walking and extend comfortable walking hoursgÍÌÌÌÌÌÜ?zNTree-lined streets are associated with higher property values and desirabilityzPortland Treesz$$1.35B total property value increase)Z heat_stressÚ walkabilityÚproperty_valuegö(\Âõè?z:More pedestrians = more potential customers = higher saleszHigh Street Retailz*1% footfall increase = 1.3% sales increaseg¤p=
×£à?zOEyes on the street effect - more people watching increases natural surveillancez Jane Jacobsz"Natural surveillance reduces crime)Zretail_revenuer%g)\Âõ(ä?zUWell-maintained areas signal investment and care, attracting residents and businessesgš™™™™™á?zYBroken windows theory - visible neglect signals low enforcement and invites more disorderzBroken Windowsz+Maintenance prevents escalation of disorderg¸…ëQ¸Þ?z4Tourists avoid areas that appear neglected or unsafe)r'r%Ztourismgq=
×£på?zHTransit access reduces commute costs and increases location desirabilityz TOD Valuez*10-20% property value premium near transitg)\Âõ(Ü?zCTransit hubs create walkable destinations and mixed-use development)r'r&)ÚlightingÚ
tree_coverageÚpedestrian_flowÚmaintenance_qualityÚtransit_accessrrrrrr!&s~þý ý÷þý ýÿýòÿýÿýøýÿýýóÿýýø®z CausalEngine._build_causal_graph)r r
rc CsR||jvrN||j|vrN|j||}|||d|d|d| |d¡dœSdS)z
Explain why A causes B
Returns:
Causal explanation or None if no relationship known
r r r
)r r
r r r
ÚinterpretationN)r"Ú_interpret_strength)rr r
Ú relationshiprrrÚexplain_causalityŠs ú zCausalEngine.explain_causality)r
rcCsdg}|j ¡D]>\}}||vr||}| ||d|dddddœ¡q|jddd d
|S) z"Find all known causes of an effectr r Nédú...)r r r cSs|dS©Nr r©ÚxrrrÚ<lambda>­óz*CausalEngine.find_causes.<locals>.<lambda>T©ÚkeyÚreverse©r"ÚitemsÚappendÚsort)rr
Úcausesr Úeffectsr/rrrÚ find_causesŸsý
zCausalEngine.find_causes)r rcCsf||jvrgSg}|j| ¡D].\}}| ||d|dddddœ¡q |jddd d
|S) z!Find all known effects of a causer r Nr1r2)r
r r cSs|dSr3rr4rrrr6¿r7z+CausalEngine.find_effects.<locals>.<lambda>Tr8r;)rr r@r
r/rrrÚ find_effects±s
ý
zCausalEngine.find_effects)r
Ú
signal_valuesrc C| |¡}|s |dd|dœSg}tdd|Dƒƒ}|D]j}|d}| |d¡}td|ƒd} |d|d | }
| |t|
d
d ƒ|t| d
d ƒ|d d œ¡q:|jd
ddd|| |d¡ttdd|Dƒƒd ƒ||rì|ddnddœS)u
Calculate how much each cause contributes to an effect
Example:
"Safety Index = 45. What causes this?"
→ Lighting: 31% of variation
→ Maintenance: 22% of variation
→ Pedestrian flow: 18% of variation
ÚunknownzNo causal model for )r
ÚstatusÚmessagecss|]}|dVqdS)r Nr)Ú.0ÚcrrrÚ <genexpr>Ür7z5CausalEngine.calculate_attribution.<locals>.<genexpr>r é2r ér1r )r Úattribution_percentÚ
current_valueÚpotential_improvementr cSs|dS)NrLrr4rrrr6ïr7z4CausalEngine.calculate_attribution.<locals>.<lambda>Tr8css|]}|dVqdS)rLNr)rGÚarrrrIôr7rN)r
rMZtotal_attributionÚ attributionsZ
top_driver)rAÚsumÚgetÚabsr=rr>) rr
rCr?rPZtotal_strengthr Ú
cause_namerMZ deviationÚ attributionrrrÚcalculate_attributionÃs6
ý   û

ûz"CausalEngine.calculate_attribution)ÚtargetrMÚ target_valuerc C| |¡}g}|D]b}|d}|d}||} | |}
| d||t|
dƒt|dƒ|d|
| dkrjdnd d
œ¡q|jd d d
d|S)u3
Recommend interventions to achieve a target
Example:
"Improve safety from 45 to 70"
→ Install lighting (expected improvement: +15)