Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168703
Image: ubuntu2004
import pprint import itertools from collections import namedtuple
We have a car that has as usual a fuel warning light. We know that our light is ON if the car is in reserve, but can be ON too if the car is traveling up or if the acceleration is high. Sometime the sensor can be broken or the light burned and the light is always OFF.
vr = namedtuple('VR', 'v r') sar = namedtuple('SAR', 's a r') VR = {vr(0,0):20, vr(0,1):1, vr(1,0):1, vr(1,1):20} SAR = {sar(0,0,0):12, sar(0,0,1):12, sar(0,1,0):5, sar(0,1,1):2, sar(1,0,0):2, sar(1,0,1):2, sar(1,1,0):2, sar(1,1,1):5}
sarv = namedtuple('SARV', 's a r v') world = [sarv(*x) for x in itertools.product(*[(0,1)]*4)]
Z = sum([float(VR[vr(x.v, x.r)]*SAR[sar(x.s, x.a, x.r)]) for x in world])
Px = [(x, VR[vr(x.v, x.r)]*SAR[sar(x.s, x.a, x.r)]/Z) for x in world]
pprint.pprint(Px)
[(SARV(s=0, a=0, r=0, v=0), 0.27210884353741499), (SARV(s=0, a=0, r=0, v=1), 0.013605442176870748), (SARV(s=0, a=0, r=1, v=0), 0.013605442176870748), (SARV(s=0, a=0, r=1, v=1), 0.27210884353741499), (SARV(s=0, a=1, r=0, v=0), 0.11337868480725624), (SARV(s=0, a=1, r=0, v=1), 0.0056689342403628117), (SARV(s=0, a=1, r=1, v=0), 0.0022675736961451248), (SARV(s=0, a=1, r=1, v=1), 0.045351473922902494), (SARV(s=1, a=0, r=0, v=0), 0.045351473922902494), (SARV(s=1, a=0, r=0, v=1), 0.0022675736961451248), (SARV(s=1, a=0, r=1, v=0), 0.0022675736961451248), (SARV(s=1, a=0, r=1, v=1), 0.045351473922902494), (SARV(s=1, a=1, r=0, v=0), 0.045351473922902494), (SARV(s=1, a=1, r=0, v=1), 0.0022675736961451248), (SARV(s=1, a=1, r=1, v=0), 0.0056689342403628117), (SARV(s=1, a=1, r=1, v=1), 0.11337868480725624)]