Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference probability are NaNs #149

Open
SEICS opened this issue Oct 20, 2022 · 1 comment
Open

Inference probability are NaNs #149

SEICS opened this issue Oct 20, 2022 · 1 comment

Comments

@SEICS
Copy link

SEICS commented Oct 20, 2022

Hi,

When I do inference with BayesNets.jl, sometimes the inference results for some attributes are NaNs instead of expected numeric values. I wonder if I misuse the BayesNets.jl or if this is a bug. Thanks for help!

Here is the example code I run:

using Random
Random.seed!(0)
using BayesNets
using TikzGraphs
using TikzPictures
bn = DiscreteBayesNet()
push!(bn, DiscreteCPD(:dAge, [0.17949424090371946, 0.1645598455834047, 0.15089991599834846, 0.13475166630394766, 0.13130454768263242, 0.12725334938788627, 0.09865048194167886, 0.013085952198382206]))
push!(bn, DiscreteCPD(:iYearwrk, [:dAge], [8], [Categorical([0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),Categorical([0.8203468179514751, 0.0, 0.021612468636829942, 0.055394465250225566, 0.023723534428417813, 0.03338895274821709, 0.02730542474693166, 0.01822833623790278]),Categorical([0.7876534889676645, 0.0, 0.00294644903020582, 0.09520022644256042, 0.045986709978299255, 0.018759687832755993, 0.024741545470474854, 0.024711892278039115]),Categorical([0.6252256549275791, 0.0, 0.11417686516250174, 0.07416273720181853, 0.03724891172439609, 0.06545653237053897, 0.054567738741403984, 0.029161559871761587]),Categorical([0.1272120055517002, 0.0, 0.46134256964409637, 0.07278241796371568, 0.08649437394666402, 0.16209911271934171, 0.06924754634678298, 0.020821973827699018]),Categorical([0.8255286502037881, 0.0, 0.04177095820346839, 0.04507951730200591, 0.02279549268760489, 0.027922960121473668, 0.02211140413969472, 0.014791017341964358]),Categorical([0.2879415779078062, 0.41679758856299304, 0.0004082289050805943, 0.10642816202151655, 0.1757157407292865, 0.0, 0.00251534981918346, 0.010193352054133627]),Categorical([0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),]))
push!(bn, DiscreteCPD(:iWork89, [:iYearwrk], [8], [Categorical([0.9679611027659126, 0.0320388972340874, 0.0]),Categorical([0.0, 0.0, 1.0]),Categorical([0.0, 1.0, 0.0]),Categorical([1.0, 0.0, 0.0]),Categorical([0.0, 1.0, 0.0]),Categorical([0.0, 1.0, 0.0]),Categorical([0.0, 1.0, 0.0]),Categorical([0.0, 1.0, 0.0]),]))
push!(bn, DiscreteCPD(:dHour89, [:iWork89], [3], [Categorical([0.0, 0.46940072548949124, 0.16054074434946475, 0.15767350831452562, 0.11810733359285554, 0.09427768825366283]),Categorical([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),Categorical([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),]))
push!(bn, DiscreteCPD(:dRearning, [:dHour89], [6], [Categorical([0.9997349563851179, 0.00022420082832647116, 2.5200868300262264e-05, 1.0427945503556798e-05, 8.689954586297332e-07, 4.344977293148666e-06]),Categorical([0.0026932526932526932, 0.3859530497461532, 0.3790838704631808, 0.2010962010962011, 0.02935368452609832, 0.0018199414751138888]),Categorical([0.011195167498761385, 0.8991053393803118, 0.058529288463737186, 0.018307671786272343, 0.006555127863104539, 0.006307405007812798]),Categorical([0.005287078670760515, 0.22725707328667122, 0.3159490306214015, 0.31060859611084435, 0.1334284037387892, 0.007469817571533205]),Categorical([0.00515447228176055, 0.6442896088169968, 0.23491054141385362, 0.09549372203407391, 0.016732608512649825, 0.0034190469406652894]),Categorical([0.002401213586326062, 0.22021400004867325, 0.3838129000332601, 0.32645958903553957, 0.06475975695824647, 0.0023525403379545877]),]))
push!(bn, DiscreteCPD(:iDisabl2, [:iYearwrk], [8], [Categorical([0.9979821559938382, 0.0, 0.0020178440061618392]),Categorical([0.0, 1.0, 0.0]),Categorical([0.6244552727688831, 0.0, 0.3755447272311169]),Categorical([0.9034771030090051, 0.0, 0.09652289699099495]),Categorical([0.800708052598193, 0.0, 0.199291947401807]),Categorical([0.7373918435765016, 0.0, 0.2626081564234984]),Categorical([0.7848751081711377, 0.0, 0.21512489182886227]),Categorical([0.8434072112141126, 0.0, 0.15659278878588742]),]))
push!(bn, DiscreteCPD(:iDisabl1, [:iDisabl2], [3], [Categorical([0.9530721448732365, 0.0, 0.0469278551267635]),Categorical([0.0, 1.0, 0.0]),Categorical([0.0, 0.0, 1.0]),]))
push!(bn, DiscreteCPD(:iMobillim, [:iDisabl1], [3], [Categorical([0.9950014236258344, 0.0, 0.0049985763741655295]),Categorical([0.058545331116893966, 0.9409951348575706, 0.00045953402553547027]),Categorical([0.6431508068823748, 0.0, 0.35684919311762514]),]))
push!(bn, DiscreteCPD(:iPerscare, [:iMobillim], [3], [Categorical([0.9721623211135072, 0.0, 0.027837678886492846]),Categorical([0.0, 1.0, 0.0]),Categorical([0.46369177593111943, 0.0, 0.5363082240688806]),]))
push!(bn, DiscreteCPD(:iYearsch, [:dAge], [8], [Categorical([0.0, 0.0, 0.14026579157299296, 4.532598448038292e-06, 0.3382315613895134, 0.17601212923344695, 0.0, 0.0, 0.16597695626949016, 0.00022209732395387627, 0.0, 2.266299224019146e-06, 4.532598448038292e-06, 0.0, 0.09279815432591196, 0.0864819783885706, 0.0, 0.0]),Categorical([0.30475978592705205, 0.22067311851879318, 0.02687530127183062, 0.16321208300888676, 0.00601678470342492, 0.0, 0.02811376024324224, 0.028432644888575772, 0.008441791192356657, 0.019110830954058362, 0.05254427923418246, 0.029854029935605077, 0.046811771540163395, 0.03927472282991583, 0.00013843054371068017, 6.427132386567294e-05, 0.01955084232513874, 0.006125551559197597]),Categorical([0.32390182097559, 0.2703939830976803, 0.025908802954536264, 0.1346713213192975, 0.00391691714628459, 0.0, 0.03440579045975927, 0.03830114164790878, 0.008248978986669542, 0.02293539647666159, 0.015220174953835371, 0.03914760550471082, 0.04065722257416668, 0.034306047903384505, 8.626383254033508e-05, 0.00010782979067541886, 0.006898410858459921, 0.000892291517839091]),Categorical([0.334247021958715, 0.15672074334808517, 0.09428904358536247, 0.09549354279745696, 0.018327104552946647, 0.0, 0.0539398293777056, 0.04273708106672141, 0.012826860030550205, 0.039005850424744457, 0.04574983849446655, 0.038374922266028295, 0.02208248555506584, 0.019960272657566006, 9.056385053342108e-05, 5.1319515302271944e-05, 0.01620489165544681, 0.009898628863302924]),Categorical([0.2793230147714881, 0.12014845841181719, 0.20824142460592843, 0.06432784772479429, 0.035100872410032714, 0.0, 0.06213443045504114, 0.04291724496877169, 0.02104193516407257, 0.05622955288985823, 0.02350798056904927, 0.046325096659066124, 0.013061366114801229, 0.01018018241300684, 0.00016419649053236838, 5.576484584118172e-05, 0.012274462179042331, 0.00496616932685635]),Categorical([0.30037880604171663, 0.20589466954367458, 0.04109965635738832, 0.14539119315911453, 0.008420043155118677, 0.0, 0.03160233357308399, 0.02890433948693359, 0.009049788220250939, 0.023022456645089107, 0.07301206744985216, 0.028664588827619276, 0.037676016942379924, 0.03412131383361304, 7.991688643810437e-05, 4.1556780947814276e-05, 0.020669703508351314, 0.011971549588428035]),Categorical([0.10122839788710615, 0.08839186676068302, 0.3282242867333853, 0.0003216348949119834, 0.006131680624796401, 0.0, 0.1451315610425919, 0.14040600220196198, 0.00316274313330117, 0.14727579367533844, 1.237057288123013e-05, 0.03656741343691627, 0.0019009446994156967, 0.0010762398406670213, 4.5358767231177145e-05, 0.0001072116316373278, 8.247048587486754e-06, 8.247048587486754e-06]),Categorical([0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),]))
push!(bn, DiscreteCPD(:iLang1, [:iYearsch], [18], [Categorical([0.9042760159690995, 0.0957239840309006, 0.0]),Categorical([0.8983901277190836, 0.10160987228091643, 0.0]),Categorical([0.8010582513969138, 0.1989417486030862, 0.0]),Categorical([0.8926199279240539, 0.1073753978900725, 4.674185873675453e-06]),Categorical([0.8210222555581639, 0.17787269614607248, 0.0011050482957635998]),Categorical([0.0, 0.0, 1.0]),Categorical([0.8637135763752777, 0.13628642362472232, 0.0]),Categorical([0.8680432177829195, 0.13195678221708051, 0.0]),Categorical([0.2760861040158557, 0.13891744655594535, 0.584996449428199]),Categorical([0.8223988663286318, 0.17760113367136815, 0.0]),Categorical([0.8791524114336166, 0.1208475885663834, 0.0]),Categorical([0.80625520110957, 0.19374479889042995, 0.0]),Categorical([0.8852402290697071, 0.11474246959289953, 1.730133739338051e-05]),Categorical([0.8745323275426214, 0.1254676724573786, 0.0]),Categorical([0.8492734606599601, 0.12392477037469019, 0.02680176896534966]),Categorical([0.5060052219321148, 0.04571801566579634, 0.44827676240208875]),Categorical([0.8599710652554633, 0.14002893474453668, 0.0]),Categorical([0.8206854345165239, 0.17931456548347613, 0.0]),]))
push!(bn, DiscreteCPD(:iEnglish, [:iLang1], [3], [Categorical([1.0, 0.0, 0.0, 0.0, 0.0]),Categorical([0.0, 0.5623532815613144, 0.22946599420030642, 0.14966760259605333, 0.05851312164232593]),Categorical([1.0, 0.0, 0.0, 0.0, 0.0]),]))
push!(bn, DiscreteCPD(:dAncstry1, [:iLang1], [3], [Categorical([0.6040022632298726, 0.20761175966122594, 0.09577217495883307, 0.017528360147871118, 0.05942094807003834, 0.0070596533604650276, 0.002409375658100054, 0.0028325894952642167, 0.000648521923136451, 0.0012945065930524931, 0.0010661538032157147, 0.0003536930989249658]),Categorical([0.18981174010534138, 0.07348908776475076, 0.0681924288353926, 0.43413862714282897, 0.05919369792933843, 0.1216423259269975, 0.022307778296520843, 0.00777895408247138, 0.015058161326170296, 0.002926149253338769, 0.004149213885071379, 0.0013118354517777178]),Categorical([0.4565004141779657, 0.22656079696560144, 0.1253869294153551, 0.10707045385185508, 0.04366852683437241, 0.024403801717748617, 0.005733094999346035, 0.0032534769150281205, 0.0029264943105026813, 0.002310677071979771, 0.0016676112830797403, 0.0005177224571652788]),]))
push!(bn, DiscreteCPD(:iRspouse, [:dAge], [8], [Categorical([0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),Categorical([0.6702831646211082, 0.0, 0.16309590023112958, 0.1082452692597674, 0.005552053592396208, 0.0317772257035856, 0.021046386592013053]),Categorical([0.3795123397716704, 0.0, 0.5219150570823954, 0.045741397204512674, 0.0019651979350595087, 0.0226739092342737, 0.02819209877208826]),Categorical([0.7374553973036123, 0.0, 0.04656189435424956, 0.10259374867927717, 0.0780298136195956, 0.021161753074642724, 0.014197392968622645]),Categorical([0.5291619163279468, 0.0, 0.048670318231386935, 0.04936737880440171, 0.34494584613859425, 0.009857985525924456, 0.01799655497174581]),Categorical([0.7300695276912011, 0.0, 0.0714968432829857, 0.13663869575641333, 0.016657875809158475, 0.0293422840246144, 0.01579477343562695]),Categorical([0.016213697522998956, 0.27701836205368, 0.6958447245691948, 0.001336021871172854, 0.0004948229152492052, 0.0016782743875535544, 0.0074140966801505915]),Categorical([0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),]))
push!(bn, DiscreteCPD(:iMarital, [:iRspouse], [7], [Categorical([1.0, 0.0, 0.0, 0.0, 0.0]),Categorical([0.0, 1.0, 0.0, 0.0, 0.0]),Categorical([0.0, 1.0, 0.0, 0.0, 0.0]),Categorical([0.0, 0.0, 1.0, 0.0, 0.0]),Categorical([0.0, 0.0, 0.0, 1.0, 0.0]),Categorical([0.0, 0.0, 0.0, 0.0, 1.0]),Categorical([1.0, 0.0, 0.0, 0.0, 0.0]),]))
prob = infer(ExactInference(), bn, [:dAncstry1], evidence=Assignment(:iEnglish=>1,:iMarital=>1,:iPerscare=>1,:dRearning=>1,:iRspouse=>5,:iYearsch=>3)).potential
print(prob)
@tawheeler
Copy link
Contributor

Hello SEICS. Some of your variables have a zero likelihoods, ex Categorical([0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]). It is generally not recommended to allow an outcome to have zero likelihood unless you are 100% certain that value actually cannot ever be set. I suspect that is what is causing this issue.
You can check this by adding, say 1e-5, to all Categorical entries and re-normalizing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants