|
24 | 24 | Factor(6, 7), size = (2, 2) |
25 | 25 | Factor(5, 6, 8), size = (2, 2, 2)</code></pre><hr/><p>Create a tensor network representation of the loaded model.</p><pre><code class="language-julia hljs">tn = TensorNetworkModel(model)</code></pre><pre><code class="nohighlight hljs">TensorNetworkModel{OMEinsum.DynamicNestedEinsum{Int64}, Array{Float64}} |
26 | 26 | variables: 1, 2, 3, 4, 5, 6, 7, 8 |
27 | | -contraction time = 2^6.044, space = 2^2.0, read-write = 2^7.098</code></pre><hr/><p>Calculate the partition function. Since the factors in this model are normalized, the partition function is the same as the total probability, <span>$1$</span>.</p><pre><code class="language-julia hljs">probability(tn) |> first</code></pre><pre><code class="nohighlight hljs">1.0</code></pre><hr/><p>Calculate the marginal probabilities of each random variable in the model.</p><pre><code class="language-julia hljs">marginals(tn)</code></pre><pre><code class="nohighlight hljs">Dict{Vector{Int64}, Vector{Float64}} with 8 entries: |
| 27 | +contraction time = 2^6.087, space = 2^2.0, read-write = 2^7.14</code></pre><hr/><p>Calculate the partition function. Since the factors in this model are normalized, the partition function is the same as the total probability, <span>$1$</span>.</p><pre><code class="language-julia hljs">probability(tn) |> first</code></pre><pre><code class="nohighlight hljs">1.0</code></pre><hr/><p>Calculate the marginal probabilities of each random variable in the model.</p><pre><code class="language-julia hljs">marginals(tn)</code></pre><pre><code class="nohighlight hljs">Dict{Vector{Int64}, Vector{Float64}} with 8 entries: |
28 | 28 | [8] => [0.435971, 0.564029] |
29 | 29 | [3] => [0.5, 0.5] |
30 | 30 | [1] => [0.01, 0.99] |
|
42 | 42 | 7 |
43 | 43 | 8</code></pre><hr/><p>Set the evidence: Assume that the "X-ray" result (variable 7) is negative. Since setting the evidence may affect the contraction order of the tensor network, recompute it.</p><pre><code class="language-julia hljs">tn = TensorNetworkModel(model, evidence = Dict(7 => 0))</code></pre><pre><code class="nohighlight hljs">TensorNetworkModel{OMEinsum.DynamicNestedEinsum{Int64}, Array{Float64}} |
44 | 44 | variables: 1, 2, 3, 4, 5, 6, 7 (evidence → 0), 8 |
45 | | -contraction time = 2^6.044, space = 2^2.0, read-write = 2^7.109</code></pre><hr/><p>Calculate the maximum log-probability among all configurations.</p><pre><code class="language-julia hljs">maximum_logp(tn)</code></pre><pre><code class="nohighlight hljs">0-dimensional Array{Float64, 0}: |
| 45 | +contraction time = 2^6.0, space = 2^2.0, read-write = 2^7.066</code></pre><hr/><p>Calculate the maximum log-probability among all configurations.</p><pre><code class="language-julia hljs">maximum_logp(tn)</code></pre><pre><code class="nohighlight hljs">0-dimensional Array{Float64, 0}: |
46 | 46 | -3.6522217920023303</code></pre><hr/><p>Generate 10 samples from the posterior distribution.</p><pre><code class="language-julia hljs">sample(tn, 10)</code></pre><pre><code class="nohighlight hljs">10-element TensorInference.Samples{Int64}: |
47 | | - [1, 1, 0, 0, 0, 0, 0, 0] |
| 47 | + [1, 1, 0, 0, 1, 0, 0, 0] |
48 | 48 | [1, 1, 0, 1, 0, 1, 0, 1] |
49 | | - [1, 1, 0, 0, 0, 0, 0, 1] |
| 49 | + [1, 0, 1, 1, 0, 0, 0, 0] |
| 50 | + [1, 1, 0, 1, 1, 1, 0, 1] |
| 51 | + [1, 1, 0, 0, 0, 0, 0, 0] |
50 | 52 | [1, 1, 0, 1, 0, 1, 0, 0] |
51 | | - [1, 1, 0, 0, 1, 0, 0, 0] |
52 | | - [1, 1, 0, 1, 1, 1, 0, 0] |
53 | | - [1, 1, 0, 0, 1, 0, 0, 1] |
54 | | - [1, 1, 0, 1, 1, 1, 0, 0] |
| 53 | + [1, 0, 0, 1, 1, 0, 0, 0] |
55 | 54 | [1, 1, 0, 0, 0, 0, 0, 0] |
56 | | - [1, 1, 0, 0, 1, 0, 0, 1]</code></pre><hr/><p>Retrieve both the maximum log-probability and the most probable configuration.</p><pre><code class="language-julia hljs">logp, cfg = most_probable_config(tn)</code></pre><pre><code class="nohighlight hljs">(-3.6522217920023303, [1, 1, 0, 0, 0, 0, 0, 0])</code></pre><hr/><p>Compute the most probable values of certain variables (e.g., 4 and 7) while marginalizing over others. This is known as Maximum a Posteriori (MAP) estimation.</p><pre><code class="language-julia hljs">mmap = MMAPModel(model, evidence=Dict(7=>0), queryvars=[4,7])</code></pre><pre><code class="nohighlight hljs">MMAPModel{Int64, Array{Float64}} |
| 55 | + [1, 1, 0, 0, 1, 0, 0, 0] |
| 56 | + [1, 0, 1, 1, 1, 0, 0, 0]</code></pre><hr/><p>Retrieve both the maximum log-probability and the most probable configuration.</p><pre><code class="language-julia hljs">logp, cfg = most_probable_config(tn)</code></pre><pre><code class="nohighlight hljs">(-3.6522217920023303, [1, 1, 0, 0, 0, 0, 0, 0])</code></pre><hr/><p>Compute the most probable values of certain variables (e.g., 4 and 7) while marginalizing over others. This is known as Maximum a Posteriori (MAP) estimation.</p><pre><code class="language-julia hljs">mmap = MMAPModel(model, evidence=Dict(7=>0), queryvars=[4,7])</code></pre><pre><code class="nohighlight hljs">MMAPModel{Int64, Array{Float64}} |
57 | 57 | variables: 4, 7 (evidence → 0) |
58 | 58 | query variables: 4 |
59 | | -contraction time = 2^6.0, space = 2^2.0, read-write = 2^7.0</code></pre><hr/><p>Get the most probable configurations for variables 4 and 7.</p><pre><code class="language-julia hljs">most_probable_config(mmap)</code></pre><pre><code class="nohighlight hljs">(-2.8754627318176693, [1, 0])</code></pre><hr/><p>Compute the total log-probability of having lung cancer. The results suggest that the probability is roughly half.</p><pre><code class="language-julia hljs">log_probability(mmap, [1, 0]), log_probability(mmap, [0, 0])</code></pre><pre><code class="nohighlight hljs">(-2.8754627318176693, -2.9206248010671856)</code></pre><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p><section class="footnotes is-size-7"><ul><li class="footnote" id="footnote-lauritzen1988local"><a class="tag is-link" href="#citeref-lauritzen1988local">lauritzen1988local</a>Steffen L Lauritzen and David J Spiegelhalter. Local computations with probabilities on graphical structures and their application to expert systems. <em>Journal of the Royal Statistical Society: Series B (Methodological)</em>, 50(2):157–194, 1988.</li></ul></section></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../../../examples-overview/">« Overview</a><a class="docs-footer-nextpage" href="../../hard-core-lattice-gas/main/">Hard-core Lattice Gas »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.11.0 on <span class="colophon-date" title="Monday 12 May 2025 11:23">Monday 12 May 2025</span>. Using Julia version 1.11.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
| 59 | +contraction time = 2^6.022, space = 2^2.0, read-write = 2^7.033</code></pre><hr/><p>Get the most probable configurations for variables 4 and 7.</p><pre><code class="language-julia hljs">most_probable_config(mmap)</code></pre><pre><code class="nohighlight hljs">(-2.8754627318176693, [1, 0])</code></pre><hr/><p>Compute the total log-probability of having lung cancer. The results suggest that the probability is roughly half.</p><pre><code class="language-julia hljs">log_probability(mmap, [1, 0]), log_probability(mmap, [0, 0])</code></pre><pre><code class="nohighlight hljs">(-2.8754627318176693, -2.920624801067186)</code></pre><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p><section class="footnotes is-size-7"><ul><li class="footnote" id="footnote-lauritzen1988local"><a class="tag is-link" href="#citeref-lauritzen1988local">lauritzen1988local</a>Steffen L Lauritzen and David J Spiegelhalter. Local computations with probabilities on graphical structures and their application to expert systems. <em>Journal of the Royal Statistical Society: Series B (Methodological)</em>, 50(2):157–194, 1988.</li></ul></section></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../../../examples-overview/">« Overview</a><a class="docs-footer-nextpage" href="../../hard-core-lattice-gas/main/">Hard-core Lattice Gas »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.11.0 on <span class="colophon-date" title="Monday 12 May 2025 11:23">Monday 12 May 2025</span>. Using Julia version 1.11.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
0 commit comments