-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
build based on b924a17
Documenter.jl
committed
Feb 14, 2024
1 parent
5e6bb6a
commit 8e7cda4
Showing
15 changed files
with
2,585 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v0.24.6 | ||
v0.24.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v0.24.6 | ||
v0.24.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"documenter":{"julia_version":"1.10.0","generation_timestamp":"2024-02-14T16:34:31","documenter_version":"1.2.1"}} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Small function to quickly swap out themes. Gets put into the <head> tag.. | ||
function set_theme_from_local_storage() { | ||
// Initialize the theme to null, which means default | ||
var theme = null; | ||
// If the browser supports the localstorage and is not disabled then try to get the | ||
// documenter theme | ||
if (window.localStorage != null) { | ||
// Get the user-picked theme from localStorage. May be `null`, which means the default | ||
// theme. | ||
theme = window.localStorage.getItem("documenter-theme"); | ||
} | ||
// Check if the users preference is for dark color scheme | ||
var darkPreference = | ||
window.matchMedia("(prefers-color-scheme: dark)").matches === true; | ||
// Initialize a few variables for the loop: | ||
// | ||
// - active: will contain the index of the theme that should be active. Note that there | ||
// is no guarantee that localStorage contains sane values. If `active` stays `null` | ||
// we either could not find the theme or it is the default (primary) theme anyway. | ||
// Either way, we then need to stick to the primary theme. | ||
// | ||
// - disabled: style sheets that should be disabled (i.e. all the theme style sheets | ||
// that are not the currently active theme) | ||
var active = null; | ||
var disabled = []; | ||
var primaryLightTheme = null; | ||
var primaryDarkTheme = null; | ||
for (var i = 0; i < document.styleSheets.length; i++) { | ||
var ss = document.styleSheets[i]; | ||
// The <link> tag of each style sheet is expected to have a data-theme-name attribute | ||
// which must contain the name of the theme. The names in localStorage much match this. | ||
var themename = ss.ownerNode.getAttribute("data-theme-name"); | ||
// attribute not set => non-theme stylesheet => ignore | ||
if (themename === null) continue; | ||
// To distinguish the default (primary) theme, it needs to have the data-theme-primary | ||
// attribute set. | ||
if (ss.ownerNode.getAttribute("data-theme-primary") !== null) { | ||
primaryLightTheme = themename; | ||
} | ||
// Check if the theme is primary dark theme so that we could store its name in darkTheme | ||
if (ss.ownerNode.getAttribute("data-theme-primary-dark") !== null) { | ||
primaryDarkTheme = themename; | ||
} | ||
// If we find a matching theme (and it's not the default), we'll set active to non-null | ||
if (themename === theme) active = i; | ||
// Store the style sheets of inactive themes so that we could disable them | ||
if (themename !== theme) disabled.push(ss); | ||
} | ||
var activeTheme = null; | ||
if (active !== null) { | ||
// If we did find an active theme, we'll (1) add the theme--$(theme) class to <html> | ||
document.getElementsByTagName("html")[0].className = "theme--" + theme; | ||
activeTheme = theme; | ||
} else { | ||
// If we did _not_ find an active theme, then we need to fall back to the primary theme | ||
// which can either be dark or light, depending on the user's OS preference. | ||
var activeTheme = darkPreference ? primaryDarkTheme : primaryLightTheme; | ||
// In case it somehow happens that the relevant primary theme was not found in the | ||
// preceding loop, we abort without doing anything. | ||
if (activeTheme === null) { | ||
console.error("Unable to determine primary theme."); | ||
return; | ||
} | ||
// When switching to the primary light theme, then we must not have a class name | ||
// for the <html> tag. That's only for non-primary or the primary dark theme. | ||
if (darkPreference) { | ||
document.getElementsByTagName("html")[0].className = | ||
"theme--" + activeTheme; | ||
} else { | ||
document.getElementsByTagName("html")[0].className = ""; | ||
} | ||
} | ||
for (var i = 0; i < document.styleSheets.length; i++) { | ||
var ss = document.styleSheets[i]; | ||
// The <link> tag of each style sheet is expected to have a data-theme-name attribute | ||
// which must contain the name of the theme. The names in localStorage much match this. | ||
var themename = ss.ownerNode.getAttribute("data-theme-name"); | ||
// attribute not set => non-theme stylesheet => ignore | ||
if (themename === null) continue; | ||
// we'll disable all the stylesheets, except for the active one | ||
ss.disabled = !(themename == activeTheme); | ||
} | ||
} | ||
set_theme_from_local_storage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function maybeAddWarning() { | ||
// DOCUMENTER_NEWEST is defined in versions.js, DOCUMENTER_CURRENT_VERSION and DOCUMENTER_STABLE | ||
// in siteinfo.js. | ||
// If either of these are undefined something went horribly wrong, so we abort. | ||
if ( | ||
window.DOCUMENTER_NEWEST === undefined || | ||
window.DOCUMENTER_CURRENT_VERSION === undefined || | ||
window.DOCUMENTER_STABLE === undefined | ||
) { | ||
return; | ||
} | ||
|
||
// Current version is not a version number, so we can't tell if it's the newest version. Abort. | ||
if (!/v(\d+\.)*\d+/.test(window.DOCUMENTER_CURRENT_VERSION)) { | ||
return; | ||
} | ||
|
||
// Current version is newest version, so no need to add a warning. | ||
if (window.DOCUMENTER_NEWEST === window.DOCUMENTER_CURRENT_VERSION) { | ||
return; | ||
} | ||
|
||
// Add a noindex meta tag (unless one exists) so that search engines don't index this version of the docs. | ||
if (document.body.querySelector('meta[name="robots"]') === null) { | ||
const meta = document.createElement("meta"); | ||
meta.name = "robots"; | ||
meta.content = "noindex"; | ||
|
||
document.getElementsByTagName("head")[0].appendChild(meta); | ||
} | ||
|
||
const div = document.createElement("div"); | ||
div.classList.add("outdated-warning-overlay"); | ||
const closer = document.createElement("button"); | ||
closer.classList.add("outdated-warning-closer", "delete"); | ||
closer.addEventListener("click", function () { | ||
document.body.removeChild(div); | ||
}); | ||
const href = window.documenterBaseURL + "/../" + window.DOCUMENTER_STABLE; | ||
div.innerHTML = | ||
'This documentation is not for the latest stable release, but for either the development version or an older release.<br><a href="' + | ||
href + | ||
'">Click here to go to the documentation for the latest stable release.</a>'; | ||
div.appendChild(closer); | ||
document.body.appendChild(div); | ||
} | ||
|
||
if (document.readyState === "loading") { | ||
document.addEventListener("DOMContentLoaded", maybeAddWarning); | ||
} else { | ||
maybeAddWarning(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Home · DynamicPPL</title><meta name="title" content="Home · DynamicPPL"/><meta property="og:title" content="Home · DynamicPPL"/><meta property="twitter:title" content="Home · DynamicPPL"/><meta name="description" content="Documentation for DynamicPPL."/><meta property="og:description" content="Documentation for DynamicPPL."/><meta property="twitter:description" content="Documentation for DynamicPPL."/><script data-outdated-warner src="assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.050/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="assets/documenter.js"></script><script src="search_index.js"></script><script src="siteinfo.js"></script><script src="../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href><img src="assets/logo.svg" alt="DynamicPPL logo"/></a><div class="docs-package-name"><span class="docs-autofit"><a href>DynamicPPL</a></span></div><button class="docs-search-query input is-rounded is-small is-clickable my-2 mx-auto py-1 px-2" id="documenter-search-query">Search docs (Ctrl + /)</button><ul class="docs-menu"><li class="is-active"><a class="tocitem" href>Home</a></li><li><a class="tocitem" href="api/">API</a></li><li><span class="tocitem">Tutorials</span><ul><li><a class="tocitem" href="tutorials/prob-interface/">The Probability Interface</a></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><a class="docs-sidebar-button docs-navbar-link fa-solid fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href>Home</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Home</a></li></ul></nav><div class="docs-right"><a class="docs-navbar-link" href="https://github.com/TuringLang/DynamicPPL.jl" title="View the repository on GitHub"><span class="docs-icon fa-brands"></span><span class="docs-label is-hidden-touch">GitHub</span></a><a class="docs-navbar-link" href="https://github.com/TuringLang/DynamicPPL.jl/blob/master/docs/src/index.md" title="Edit source on GitHub"><span class="docs-icon fa-solid"></span></a><a class="docs-settings-button docs-navbar-link fa-solid fa-gear" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-article-toggle-button fa-solid fa-chevron-up" id="documenter-article-toggle-button" href="javascript:;" title="Collapse all docstrings"></a></div></header><article class="content" id="documenter-page"><h1 id="DynamicPPL.jl"><a class="docs-heading-anchor" href="#DynamicPPL.jl">DynamicPPL.jl</a><a id="DynamicPPL.jl-1"></a><a class="docs-heading-anchor-permalink" href="#DynamicPPL.jl" title="Permalink"></a></h1><p>A domain-specific language and backend for probabilistic programming languages, used by <a href="https://github.com/TuringLang/Turing.jl">Turing.jl</a>.</p></article><nav class="docs-footer"><a class="docs-footer-nextpage" href="api/">API »</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="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="auto">Automatic (OS)</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.2.1 on <span class="colophon-date" title="Wednesday 14 February 2024 16:34">Wednesday 14 February 2024</span>. Using Julia version 1.10.0.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var DOCUMENTER_CURRENT_VERSION = "v0.24.7"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>The Probability Interface · DynamicPPL</title><meta name="title" content="The Probability Interface · DynamicPPL"/><meta property="og:title" content="The Probability Interface · DynamicPPL"/><meta property="twitter:title" content="The Probability Interface · DynamicPPL"/><meta name="description" content="Documentation for DynamicPPL."/><meta property="og:description" content="Documentation for DynamicPPL."/><meta property="twitter:description" content="Documentation for DynamicPPL."/><script data-outdated-warner src="../../assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.050/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="../.."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../../assets/documenter.js"></script><script src="../../search_index.js"></script><script src="../../siteinfo.js"></script><script src="../../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../../assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="../../"><img src="../../assets/logo.svg" alt="DynamicPPL logo"/></a><div class="docs-package-name"><span class="docs-autofit"><a href="../../">DynamicPPL</a></span></div><button class="docs-search-query input is-rounded is-small is-clickable my-2 mx-auto py-1 px-2" id="documenter-search-query">Search docs (Ctrl + /)</button><ul class="docs-menu"><li><a class="tocitem" href="../../">Home</a></li><li><a class="tocitem" href="../../api/">API</a></li><li><span class="tocitem">Tutorials</span><ul><li class="is-active"><a class="tocitem" href>The Probability Interface</a><ul class="internal"><li><a class="tocitem" href="#Conditioning-and-Deconditioning"><span>Conditioning and Deconditioning</span></a></li><li><a class="tocitem" href="#Probabilities-and-Densities"><span>Probabilities and Densities</span></a></li><li><a class="tocitem" href="#Example:-Cross-validation"><span>Example: Cross-validation</span></a></li></ul></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><a class="docs-sidebar-button docs-navbar-link fa-solid fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a><nav class="breadcrumb"><ul class="is-hidden-mobile"><li><a class="is-disabled">Tutorials</a></li><li class="is-active"><a href>The Probability Interface</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>The Probability Interface</a></li></ul></nav><div class="docs-right"><a class="docs-navbar-link" href="https://github.com/TuringLang/DynamicPPL.jl" title="View the repository on GitHub"><span class="docs-icon fa-brands"></span><span class="docs-label is-hidden-touch">GitHub</span></a><a class="docs-navbar-link" href="https://github.com/TuringLang/DynamicPPL.jl/blob/master/docs/src/tutorials/prob-interface.md" title="Edit source on GitHub"><span class="docs-icon fa-solid"></span></a><a class="docs-settings-button docs-navbar-link fa-solid fa-gear" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-article-toggle-button fa-solid fa-chevron-up" id="documenter-article-toggle-button" href="javascript:;" title="Collapse all docstrings"></a></div></header><article class="content" id="documenter-page"><h1 id="The-Probability-Interface"><a class="docs-heading-anchor" href="#The-Probability-Interface">The Probability Interface</a><a id="The-Probability-Interface-1"></a><a class="docs-heading-anchor-permalink" href="#The-Probability-Interface" title="Permalink"></a></h1><p>The easiest way to manipulate and query DynamicPPL models is via the DynamicPPL probability interface.</p><p>Let's use a simple model of normally-distributed data as an example.</p><pre><code class="language-julia hljs">using DynamicPPL | ||
using Distributions | ||
using FillArrays | ||
|
||
using LinearAlgebra | ||
using Random | ||
|
||
@model function gdemo(n) | ||
μ ~ Normal(0, 1) | ||
x ~ MvNormal(Fill(μ, n), I) | ||
return nothing | ||
end</code></pre><p>We generate some data using <code>μ = 0</code>:</p><pre><code class="language-julia hljs">Random.seed!(1776) | ||
dataset = randn(100)</code></pre><h2 id="Conditioning-and-Deconditioning"><a class="docs-heading-anchor" href="#Conditioning-and-Deconditioning">Conditioning and Deconditioning</a><a id="Conditioning-and-Deconditioning-1"></a><a class="docs-heading-anchor-permalink" href="#Conditioning-and-Deconditioning" title="Permalink"></a></h2><p>Bayesian models can be transformed with two main operations, conditioning and deconditioning (also known as marginalization). Conditioning takes a variable and fixes its value as known. We do this by passing a model and a collection of conditioned variables to <a href="../../api/#Base.:|-Tuple{Model, Any}"><code>|</code></a> or its alias <a href="../../api/#AbstractPPL.condition"><code>condition</code></a>:</p><pre><code class="language-julia hljs">model = gdemo(length(dataset)) | (x=dataset, μ=0)</code></pre><p>This operation can be reversed by applying <a href="../../api/#AbstractPPL.decondition"><code>decondition</code></a>:</p><pre><code class="language-julia hljs">decondition(model)</code></pre><p>We can also decondition only some of the variables:</p><pre><code class="language-julia hljs">decondition(model, :μ)</code></pre><div class="admonition is-info"><header class="admonition-header">Note</header><div class="admonition-body"><p>Sometimes it is helpful to define convenience functions for conditioning on some variable(s). For instance, in this example we might want to define a version of <code>gdemo</code> that conditions on some observations of <code>x</code>:</p><pre><code class="language-julia hljs">gdemo(x::AbstractVector{<:Real}) = gdemo(length(x)) | (; x)</code></pre><p>For illustrative purposes, however, we do not use this function in the examples below.</p></div></div><h2 id="Probabilities-and-Densities"><a class="docs-heading-anchor" href="#Probabilities-and-Densities">Probabilities and Densities</a><a id="Probabilities-and-Densities-1"></a><a class="docs-heading-anchor-permalink" href="#Probabilities-and-Densities" title="Permalink"></a></h2><p>We often want to calculate the (unnormalized) probability density for an event. This probability might be a prior, a likelihood, or a posterior (joint) density. DynamicPPL provides convenient functions for this. For example, we can calculate the joint probability of a set of samples (here drawn from the prior) with <a href="../../api/#DynamicPPL.logjoint"><code>logjoint</code></a>:</p><pre><code class="language-julia hljs">model = gdemo(length(dataset)) | (x=dataset,) | ||
|
||
Random.seed!(124) | ||
sample = rand(model) | ||
logjoint(model, sample)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">-181.7247437162069</code></pre><p>For models with many variables <code>rand(model)</code> can be prohibitively slow since it returns a <code>NamedTuple</code> of samples from the prior distribution of the unconditioned variables. We recommend working with samples of type <code>DataStructures.OrderedDict</code> in this case:</p><pre><code class="language-julia hljs">using DataStructures | ||
|
||
Random.seed!(124) | ||
sample_dict = rand(OrderedDict, model) | ||
logjoint(model, sample_dict)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">-181.7247437162069</code></pre><p>The prior probability and the likelihood of a set of samples can be calculated with the functions <a href="../../api/#StatsAPI.loglikelihood"><code>loglikelihood</code></a> and <a href="../../api/#DynamicPPL.logjoint"><code>logjoint</code></a>, respectively:</p><pre><code class="language-julia hljs">logjoint(model, sample) ≈ loglikelihood(model, sample) + logprior(model, sample)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><pre><code class="language-julia hljs">logjoint(model, sample_dict) ≈ | ||
loglikelihood(model, sample_dict) + logprior(model, sample_dict)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><h2 id="Example:-Cross-validation"><a class="docs-heading-anchor" href="#Example:-Cross-validation">Example: Cross-validation</a><a id="Example:-Cross-validation-1"></a><a class="docs-heading-anchor-permalink" href="#Example:-Cross-validation" title="Permalink"></a></h2><p>To give an example of the probability interface in use, we can use it to estimate the performance of our model using cross-validation. In cross-validation, we split the dataset into several equal parts. Then, we choose one of these sets to serve as the validation set. Here, we measure fit using the cross entropy (Bayes loss).<sup class="footnote-reference"><a id="citeref-1" href="#footnote-1">[1]</a></sup></p><pre><code class="language-julia hljs">using MLUtils | ||
|
||
function cross_val( | ||
dataset::AbstractVector{<:Real}; | ||
nfolds::Int=5, | ||
nsamples::Int=1_000, | ||
rng::Random.AbstractRNG=Random.default_rng(), | ||
) | ||
# Initialize `loss` in a way such that the loop below does not change its type | ||
model = gdemo(1) | (x=[first(dataset)],) | ||
loss = zero(logjoint(model, rand(rng, model))) | ||
|
||
for (train, validation) in kfolds(dataset, nfolds) | ||
# First, we train the model on the training set, i.e., we obtain samples from the posterior. | ||
# For normally-distributed data, the posterior can be computed in closed form. | ||
# For general models, however, typically samples will be generated using MCMC with Turing. | ||
posterior = Normal(mean(train), 1) | ||
samples = rand(rng, posterior, nsamples) | ||
|
||
# Evaluation on the validation set. | ||
validation_model = gdemo(length(validation)) | (x=validation,) | ||
loss += sum(samples) do sample | ||
logjoint(validation_model, (μ=sample,)) | ||
end | ||
end | ||
|
||
return loss | ||
end | ||
|
||
cross_val(dataset)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">-212760.30282411768</code></pre><section class="footnotes is-size-7"><ul><li class="footnote" id="footnote-1"><a class="tag is-link" href="#citeref-1">1</a>See <a href="https://github.com/TuringLang/ParetoSmooth.jl">ParetoSmooth.jl</a> for a faster and more accurate implementation of cross-validation than the one provided here.</li></ul></section></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../../api/">« API</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="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="auto">Automatic (OS)</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.2.1 on <span class="colophon-date" title="Wednesday 14 February 2024 16:34">Wednesday 14 February 2024</span>. Using Julia version 1.10.0.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters