### Turing.jl Basic Usage Example Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Demonstrates how to use Turing.jl by defining a simple probabilistic model and sampling from it. This showcases the core workflow of defining a model using the `@model` macro and performing inference with the `sample` function. ```julia using Turing @model function my_model() # Define prior distributions for parameters x ~ Normal(0, 1) y ~ Normal(x, 1) return y end # Sample from the model using a specific inference algorithm sample(my_model(), Prior(), 100) ``` -------------------------------- ### Install Turing.jl Source: https://github.com/turinglang/turing.jl/blob/main/README.md Installs the Turing.jl package using Julia's package manager. Requires Julia version 1.10 or higher. ```julia using Pkg Pkg.add("Turing") ``` -------------------------------- ### Using Elliptical Slice Sampling (ESS) Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md An example of how to call the `sample` function with the EllipticalSliceSampling.jl backend for probabilistic models in Turing.jl. ```Julia sample(model, ESS(), 1000) ``` -------------------------------- ### AD Type Specification in Sampler Constructors Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Starting from version 0.30.0, users must specify the desired Automatic Differentiation (AD) type directly within sampler constructors, replacing global backend settings. This allows for more explicit control over AD integration. ```Julia using Turing # Specify AutoForwardDiff with chunksize hmc_forwardiff = HMC(0.1, 10; adtype=AutoForwardDiff(; chunksize=10)) # Specify AutoReverseDiff without compiled tape hmc_reversediff = HMC(0.1, 10; adtype=AutoReverseDiff(false)) ``` -------------------------------- ### Setting AD Backends in Turing.jl Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Demonstrates how to configure the automatic differentiation (AD) backend for Turing.jl. It shows the commands to switch between different AD providers like Zygote and ReverseDiff, and mentions the necessary package imports. ```APIDOC Turing.setadbackend(backend_symbol) Description: Sets the automatic differentiation (AD) backend for Turing.jl. The available backends are `:forwarddiff` (default), `:tracker` (deprecated), `:zygote` (experimental), and `:reversediff` (experimental). Parameters: - backend_symbol: A symbol representing the AD backend to use (e.g., :zygote, :reversediff). Usage: # Enable Zygote backend using Zygote Turing.setadbackend(:zygote) # Enable ReverseDiff backend using ReverseDiff Turing.setadbackend(:reversediff) Notes: - Zygote.jl does not allow mutation within the model. - For ReverseDiff or Zygote, `for` loops are not recommended. Refer to performance tips for details. - The symbols `forward_diff` and `reverse_diff` are deprecated. ``` -------------------------------- ### AD Integration via LogDensityProblemsAD and DynamicPPL Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Version 0.30.5 saw the removal of `essential/ad.jl`. ForwardDiff and ReverseDiff integrations are now managed through package extensions within DynamicPPL, and the single-argument method `LogDensityProblemsAD.ADgradient(ℓ::DynamicPPL.LogDensityFunction)` has been moved to the Inference module. ```Julia using Turing using DynamicPPL using LogDensityProblemsAD # Example of accessing AD gradient via Inference module # Assuming 'ℓ' is a LogDensityFunction from DynamicPPL # ad_gradient = Turing.Inference.ADgradient(ℓ) # ADType is now specified in sampler constructors, e.g.: # sampler = HMC(adtype=AutoForwardDiff()) ``` -------------------------------- ### Turing.Optimisation API Documentation Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api/Optimisation.md Generates API documentation for the Turing.Optimisation module. It specifies which modules to include and the desired order for documentation elements, such as types and functions. ```APIDOC Modules = [Turing.Optimisation] Order = [:type, :function] ``` -------------------------------- ### Turing.jl 0.38.0 DynamicPPL Submodel Prefixing and Conditioning Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Demonstrates how submodels in Turing.jl are now handled with field accessors for variables, and how conditioning can be applied to either the outer or inner model. ```APIDOC DynamicPPL Submodel Prefixing and Conditioning: Variables in submodels are now represented correctly with field accessors. Example: ```julia using Turing @model inner() = x ~ Normal() @model outer() = a ~ to_submodel(inner()) ``` `keys(VarInfo(outer()))` now returns `[@varname(a.x)]` instead of `[@varname(var"a.x")]`. Conditioning on submodels: - Condition on the outer model: `outer() | (@varname(a.x) => 1.0)` - Condition on the inner model: `inner() | (@varname(x) => 1.0)` If the conditioned inner model is used as a submodel, the conditioning will still apply correctly. ``` -------------------------------- ### Turing.Variational Module Documentation Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api/Variational.md Generates API documentation for the `Turing.Variational` module, focusing on types and functions. The documentation order is specified as types first, then functions. ```APIDOC @autodocs Modules = [Turing.Variational] Order = [:type, :function] ``` -------------------------------- ### Turing.jl Distribution Utilities Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Provides functions for creating and managing distributions within Turing.jl. Includes identity matrix, product distributions from arrays or integers, and named distributions. ```APIDOC I: Identity matrix. filldist(dist, n::Integer): Create a product distribution from a distribution and an integer. Parameters: dist: The base distribution. n: The number of times to replicate the distribution. Returns: A product distribution. arraydist(dists::AbstractVector{<:Distribution}): Create a product distribution from an array of distributions. Parameters: dists: An array of distributions. Returns: A product distribution. NamedDist(name::Symbol, dist::Distribution): A distribution that carries the name of the variable it represents. Parameters: name: The symbolic name of the variable. dist: The underlying distribution. Returns: A NamedDist object. ``` -------------------------------- ### Define and Sample a Turing.jl Model Source: https://github.com/turinglang/turing.jl/blob/main/README.md Demonstrates how to define a probabilistic model using the `@model` macro and perform Markov Chain Monte Carlo (MCMC) sampling with the `sample` function. It shows a simple model with a Normal prior for mean and a truncated Cauchy prior for standard deviation, then samples from it. ```julia using Turing @model function my_first_model(data) mean ~ Normal(0, 1) sd ~ truncated(Cauchy(0, 3); lower=0) data ~ Normal(mean, sd) end model = my_first_model(randn()) chain = sample(model, NUTS(), 1000) ``` -------------------------------- ### Minimum Julia Version and Backend Removals Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Updates to Turing.jl v0.35.0 include an increased minimum required Julia version and the removal of Tapir.jl, replaced by Mooncake.jl. Support for Tracker.jl as an AD backend has also been removed. ```APIDOC ``` -------------------------------- ### Turing.jl 0.37.1 maximum_a_posteriori and maximum_likelihood Sanity Checks Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Explains the addition of sanity checks to `maximum_a_posteriori` and `maximum_likelihood` methods, with an option to disable them. ```APIDOC maximum_a_posteriori and maximum_likelihood Sanity Checks: These optimization methods now perform sanity checks on the model before running the optimization process. To disable these checks, set the keyword argument `check_model=false`. Example: ```julia # With sanity checks (default) result_checked = maximum_a_posteriori(model, data) # Without sanity checks result_unchecked = maximum_a_posteriori(model, data, check_model=false) ``` ``` -------------------------------- ### Turing.jl 0.39.0 @addlogprob! Macro Export Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Notes the official export of the `@addlogprob!` macro, making it a part of the public interface for users to add log probability densities. ```APIDOC Exported Macro: @addlogprob! The `@addlogprob!` macro is now officially exported from Turing, making it part of the public interface. This macro allows users to add log probability densities to the current model evaluation context. Example: ```julia using Turing @model mymodel(x) = begin a ~ Normal(0, 1) b ~ Bernoulli(0.5) @addlogprob!(logpdf(Normal(a, 1), x)) return a, b end ``` ``` -------------------------------- ### Turing.jl Core Modelling Macros and Functions Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md This API documentation block details essential macros and functions for defining probabilistic models in Turing.jl. It includes macros for model definition, variable naming, submodel creation, and log-probability manipulation, along with a key struct for advanced users. ```APIDOC Turing.jl Core Modelling API: @model function_name(args...) # Define probabilistic model # Parameters and variables are declared using standard Julia syntax with distributions. # Example: x ~ Normal(0, 1) # Returns: The model definition. @varname expression # Generates a VarName from a Julia expression, used for internal tracking of variables. # Parameters: # expression: A Julia expression representing a variable. # Returns: A VarName object. to_submodel(model_function, args...) # Defines a submodel, allowing for hierarchical or modular model structures. # Parameters: # model_function: The function defining the submodel. # args: Arguments for the submodel. # Returns: A submodel object. prefix(var_name::VarName, prefix_name::Symbol) # Prefixes all variable names within a model with a given VarName. # Parameters: # var_name: The base VarName to prefix. # prefix_name: The symbol to use as the prefix. # Returns: A new VarName with the prefix applied. LogDensityFunction # A struct containing all information about how to evaluate a model's log-probability. # Primarily for advanced users and internal use. # Fields: # - model: The probabilistic model. # - θ: The current parameter values. # - lp: The current log-probability. # - context: The evaluation context. @addlogprob! logprob_expression # Adds arbitrary log-probability terms during model evaluation. # Useful for incorporating custom likelihoods or constraints. # Parameters: # logprob_expression: An expression that evaluates to a log-probability value. ``` -------------------------------- ### Turing.jl 0.38.0 Gibbs Sampler with Complex VarNames Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Illustrates the enhanced support for complex `VarName`s in Turing.jl's Gibbs sampler, allowing for indexed or field-accessed variable names. ```APIDOC Gibbs Sampler with Complex VarNames: Allows for more complex `VarName`s, such as `x[1]` or `x.a`, to be used with the Gibbs sampler. Example: ```julia @model function f() x = Vector{Float64}(undef, 2) x[1] ~ Normal() return x[2] ~ Normal() end sample(f(), Gibbs(@varname(x[1]) => MH(), @varname(x[2]) => MH()), 100) ``` Performance for simple `VarName`s (e.g., `x`) is unaffected. `VarNames` with field accessors (e.g., `x.a`) should be equally fast. `VarNames` with indexing (e.g., `x[1]`) may be slower but are now supported. ``` -------------------------------- ### Parallel Sampling Methods Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Version 0.12.0 deprecated `psample` in favor of a more explicit `sample` function signature for parallel execution. Users can now choose between `MCMCThreads()` for multi-threading within a single machine or `MCMCDistributed()` for multi-processing across different machines or cores. ```Julia using Turing # Sample using multiple threads (requires JULIA_NUM_THREADS environment variable set) sample(model, sampler, MCMCThreads(), n_samples, n_chains) # Sample using distributed processes (requires addprocs() to be called) sample(model, sampler, MCMCDistributed(), n_samples, n_chains) ``` -------------------------------- ### Turing.jl 0.39.0 AdvancedVI Interface Update Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Highlights the update to Turing's variational inference interface to match AdvancedVI.jl v0.4, detailing new features introduced in the latter. ```APIDOC AdvancedVI Interface Update (v0.4): Turing's variational inference interface has been updated to align with AdvancedVI.jl version 0.4. AdvancedVI v0.4 introduces several new features: - Location-scale families with dense scale matrices. - Parameter-free stochastic optimization algorithms like `DoG` and `DoWG`. - Proximal operators for stable optimization. - The sticking-the-landing control variate for faster convergence. - The score gradient estimator for non-differentiable targets. For more details, refer to the [Turing API documentation](https://turinglang.org/Turing.jl/stable/api/#Variational-inference) and [AdvancedVI's documentation](https://turinglang.org/AdvancedVI.jl/stable/). Example usage of variational inference: ```julia using Turing using AdvancedVI @model mymodel(x) = begin theta ~ Normal(0, 1) x ~ Normal(theta, 1) end # Example of using a VI algorithm (e.g., ADVI) # vi_approx = ADVI(1000, 100) # q = vi(mymodel(), vi_approx) ``` ``` -------------------------------- ### Custom Distribution Interface with Bijectors Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md In version 0.12.0, the interface for custom distributions compatible with Turing changed. Users must now define a `bijector(d::CustomDistribution)` method that returns an instance implementing the `Bijectors.Bijector` API to handle constrained support. ```Julia using Turing using Bijectors struct MyCustomDistribution{T} <: Turing.Distributions.UnivariateDistribution # ... distribution parameters ... end # Define the bijector for the custom distribution function Bijectors.bijector(d::MyCustomDistribution) # Return an instance of a Bijector type, e.g., Bijectors.Scale return Bijectors.Scale(1.0) end # Example usage within a Turing model # @model model begin # x ~ MyCustomDistribution() # end ``` -------------------------------- ### Turing.Inference API Documentation Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api/Inference.md This section provides API documentation for the Turing.Inference module. It outlines the structure and content to be documented, specifying modules and ordering preferences for generated documentation. ```APIDOC # API: `Turing.Inference` ```@autodocs Modules = [Turing.Inference] Order = [:type, :function] ``` ``` -------------------------------- ### AD Backend Changes Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Details on changes to Automatic Differentiation (AD) backend support in Turing.jl. Zygote is no longer officially supported, and Mooncake.jl is recommended as a replacement. Tracker.jl support has also been removed. ```APIDOC AD Backend Updates: - Zygote Support: - No longer officially supported as an automatic differentiation backend. - `AutoZygote` is no longer exported. - May continue to work if imported from ADTypes, but is not tested and will not be fixed if broken. - Mooncake.jl is the recommended replacement. - Mooncake.jl Integration: - Recommended replacement for Zygote. - Use by passing `adbackend=AutoMooncake(; config=nothing)` to samplers. - Tracker.jl Support: - Removed as an AD backend. ``` -------------------------------- ### New Gibbs Sampler Implementation Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Introduction of a new Gibbs sampler in Turing.jl v0.36.0, replacing the old one. While the user-facing interface is similar, constructor changes and internal rewrites may introduce subtle breakages. `GibbsConditional` has been removed. ```APIDOC Gibbs Sampler Updates (Turing.jl v0.36.0): - New Implementation: - Replaces the old Gibbs sampler. - Previously available as `Turing.Experimental.Gibbs`. - Internals have been rewritten, potential for unanticipated breakage. - Deprecated Constructors: - Old constructors relying on lists of subsamplers (e.g., `Gibbs(HMC(:x), MH(:y))`) are deprecated and will be removed. - Old constructors for repeated subsampling (e.g., `Gibbs((HMC(0.01, 4, :x), 2), (MH(:y), 1))`) are deprecated. - New Constructor Syntax: - Maps symbols, `VarName`s, or iterables to samplers. - Examples: - `Gibbs(x => HMC(), y => MH())` - `Gibbs(@varname(x) => HMC(), @varname(y) => MH())` - `Gibbs((:x, :y) => NUTS(), :z => MH())` - Uses `RepeatSampler` for specifying repetition counts: - Example: `Gibbs(@varname(x) => RepeatSampler(HMC(0.01, 4), 2), @varname(y) => MH())` - Removed Components: - `GibbsConditional` has been removed. ``` -------------------------------- ### Turing.jl 0.39.3 getparams Performance Improvement Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Details the performance enhancement for `Turing.Inference.getparams` when using an untyped `VarInfo` by converting it to a typed `VarInfo` first. ```APIDOC Turing.Inference.getparams Performance Improvement: Improved the performance of `Turing.Inference.getparams(::Model, ::AbstractVarInfo)` when called with an untyped `VarInfo` as the second argument. The method now first converts the `VarInfo` to a typed `VarInfo`. This change significantly speeds up operations like the post-sampling Chains construction for `Prior()`. Example: ```julia # Before: Potentially slow for untyped VarInfo # params = Turing.Inference.getparams(model, varinfo) # After: Faster due to internal conversion # params = Turing.Inference.getparams(model, varinfo) ``` ``` -------------------------------- ### Turing.jl Model Probability and Quantity Interface Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md API for querying various probabilistic quantities and manipulating model states. Includes calculating log-prior, log-joint, log-likelihoods, and conditioning/fixing variables. ```APIDOC returned(model, spl, chain): Calculate additional quantities defined within a model. Parameters: model: The Turing model. spl: The sampler used. chain: A chain of samples. Returns: Calculated quantities. pointwise_loglikelihoods(model, chain): Compute the log-likelihood for each sample in a chain, element-wise. Parameters: model: The Turing model. chain: A chain of samples. Returns: A structure containing pointwise log-likelihoods. logprior(model, chain): Compute the log-prior probability for samples in a chain. Parameters: model: The Turing model. chain: A chain of samples. Returns: The log-prior probability. logjoint(model, chain): Compute the log-joint probability for samples in a chain. Parameters: model: The Turing model. chain: A chain of samples. Returns: The log-joint probability. condition(model, spl, chain, data): Condition a model on observed data. Parameters: model: The Turing model. spl: The sampler used. chain: A chain of samples. data: The data to condition on. Returns: A conditioned model. decondition(model): Remove conditioning from a model. Parameters: model: The conditioned Turing model. Returns: The deconditioned model. conditioned(model): Return the values of a model that have been conditioned. Parameters: model: The Turing model. Returns: The conditioned values. fix(model, var_name, value): Fix the value of a specific variable in the model. Parameters: model: The Turing model. var_name: The name of the variable to fix. value: The value to assign to the variable. Returns: The model with the fixed variable. unfix(model, var_name): Unfix the value of a specific variable in the model. Parameters: model: The Turing model. var_name: The name of the variable to unfix. Returns: The model with the unfixed variable. OrderedDict(pairs...): An ordered dictionary that preserves insertion order. Parameters: pairs: Key-value pairs. Returns: An OrderedDict object. ``` -------------------------------- ### Turing.jl Debugging Functions Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Utilities for debugging and monitoring inference processes within Turing.jl. ```APIDOC Turing Debugging: setprogress!: Description: Sets or updates progress information during inference. Usage: setprogress!(progress_object, current_step, total_steps) Parameters: - progress_object: An object to store progress information. - current_step: The current step in the inference process. - total_steps: The total number of steps expected. ``` -------------------------------- ### Turing.jl Samplers Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md A collection of probabilistic inference algorithms (samplers) provided by Turing.jl for exploring model posteriors. Each sampler offers different trade-offs in terms of efficiency, convergence, and applicability to various model structures. ```APIDOC Turing Samplers: Prior: Description: Sample from the prior distribution. MH: Description: Metropolis–Hastings sampler. Emcee: Description: Affine-invariant ensemble sampler. ESS: Description: Elliptical slice sampling. Gibbs: Description: Gibbs sampling. HMC: Description: Hamiltonian Monte Carlo sampler. SGLD: Description: Stochastic gradient Langevin dynamics sampler. SGHMC: Description: Stochastic gradient Hamiltonian Monte Carlo sampler. PolynomialStepsize: Description: Returns a function which generates polynomially decaying step sizes for HMC-based samplers. HMCDA: Description: Hamiltonian Monte Carlo with dual averaging. NUTS: Description: No-U-Turn Sampler, an adaptive HMC variant. IS: Description: Importance sampling. SMC: Description: Sequential Monte Carlo sampler. PG: Description: Particle Gibbs sampler. CSMC: Description: Conditional Sequential Monte Carlo, equivalent to PG. RepeatSampler: Description: A sampler that runs multiple times on the same variable, useful for debugging or ensemble methods. externalsampler: Description: Wraps an external sampler for use within Turing.jl. ``` -------------------------------- ### Turing.jl Variational Inference Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Functions for performing variational inference (VI) in Turing.jl, enabling approximate posterior inference. This section includes the main `vi` function and helpers for initializing variational families. ```APIDOC Turing Variational Inference: vi: Description: Perform variational inference on a Turing model. Usage: vi(model, alg, data; optimizer=ADAM, n_samples=1000, n_iterations=1000, ...) Parameters: - model: The Turing model to perform inference on. - alg: The variational inference algorithm (e.g., ADVI). - data: The observed data. - optimizer: The optimization algorithm for VI (defaults to ADAM). - n_samples: Number of samples to draw for gradient estimation. - n_iterations: Number of optimization iterations. Returns: A callable object representing the optimized variational distribution. q_locationscale: Description: Find a numerically non-degenerate initialization for a location-scale variational family. q_meanfield_gaussian: Description: Find a numerically non-degenerate initialization for a mean-field Gaussian variational family. q_fullrank_gaussian: Description: Find a numerically non-degenerate initialization for a full-rank Gaussian variational family. ``` -------------------------------- ### Turing.jl 0.39.5 Externalsampler Log Probability Bug Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Addresses a bug in Turing.jl where sampling with an `externalsampler` would fail to set the log probability density within the resulting chain. ```APIDOC Bug Fix: Externalsampler Log Probability Density Fixed a bug where sampling with an `externalsampler` would not correctly set the log probability density inside the resulting chain. Note: There may still be issues with the log-Jacobian term not being correctly included, and a fix is under development. Example scenario: ```julia # Previously, the following might have resulted in missing log_prob values: # chain = sample(model, ExternalSampler(), 1000) # The fix ensures log_prob is populated correctly. ``` ``` -------------------------------- ### Removed Optimization Functions Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md In Turing.jl v0.33.0, several functions related to optimization have been removed. Their functionality is now provided by new, consolidated functions: `maximum_likelihood` and `maximum_a_posteriori`. ```APIDOC Turing.jl v0.33.0 Removed Functions: - Removed Exported Functions: - `constrained_space` - `get_parameter_bounds` - `optim_objective` - `optim_function` - `optim_problem` - New Functionality: - The same functionality is now offered by: - `maximum_likelihood` - `maximum_a_posteriori` ``` -------------------------------- ### DynamicPPL v0.35 Breaking Changes Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Key breaking changes introduced with DynamicPPL v0.35, which is used by Turing.jl v0.37. These changes affect distribution syntax, VarInfo indexing, submodel prefix application, and LogDensityFunction constructors. ```APIDOC DynamicPPL v0.35 Breaking Changes: - Distribution Syntax: - The right-hand side of `.~` must now be a univariate distribution. - VarInfo Indexing: - Indexing `VarInfo` objects by samplers has been removed. - Submodel Prefixes: - The order of application for nested submodel prefixes has been reversed. - LogDensityFunction Constructor: - Arguments for the `LogDensityFunction` constructor have changed. - `LogDensityFunction` now satisfies the `LogDensityProblems` interface directly. ``` -------------------------------- ### Renaming of getADbackend to getADType Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md In version 0.30.5, the `getADbackend` function was renamed to `getADType` to better reflect its purpose. While the interface is preserved, users are advised to update their code to use the new function name for future compatibility. ```Julia using Turing # Old usage (deprecated) # ad_backend = Turing.getADbackend() # New usage ad_type = Turing.getADType() ``` -------------------------------- ### Turing.jl Inference Functions Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md This API documentation block covers the primary functions and types used for performing inference on probabilistic models defined in Turing.jl. It includes the main sampling function and enumerates different parallelization strategies for MCMC. ```APIDOC Turing.jl Inference API: sample(model, alg, n_samples; kwargs...) # Samples from a probabilistic model using a specified algorithm. # Parameters: # model: The probabilistic model to sample from. # alg: The inference algorithm (e.g., Prior(), NUTS(), HMC()). # n_samples: The number of samples to draw. # kwargs: Additional keyword arguments for the sampler. # Returns: An object containing the samples (e.g., MCMCChains.Chains). # Related: See AbstractMCMC.jl documentation for detailed algorithm options. MCMCThreads # Type representing the strategy to run MCMC using multiple threads. # Used as an algorithm parameter in `sample`. # Example: sample(model, NUTS(), MCMCThreads(), 1000) MCMCDistributed # Type representing the strategy to run MCMC using multiple processes. # Used as an algorithm parameter in `sample`. # Example: sample(model, NUTS(), MCMCDistributed(), 1000) MCMCSerial # Type representing the strategy to run MCMC without parallelism (serially). # Used as an algorithm parameter in `sample`. # Example: sample(model, NUTS(), MCMCSerial(), 1000) ``` -------------------------------- ### Turing.jl Custom Distributions Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Distributions defined within Turing.jl that are not part of the standard Distributions.jl package. ```APIDOC Turing Custom Distributions: Flat: Description: Represents a uniform distribution over the entire real line (improper prior). FlatPos: Description: Represents a uniform distribution over the positive real line (improper prior). BinomialLogit: Description: A Binomial distribution parameterized by the logit of the success probability. OrderedLogistic: Description: A distribution for ordered categorical data, often used in ordinal regression. LogPoisson: Description: A Poisson distribution where the mean parameter is on the log scale. ``` -------------------------------- ### Accessing Internal Variables in @model Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md The macros `@varinfo`, `@logpdf`, and `@sampler` were removed in version 0.12.0. Instead, internal variables like `_varinfo`, `_model`, `_sampler`, and `_context` are now directly accessible within the `@model` definition. ```Julia using Turing @model model(data) # Access internal variables directly # For example, to inspect the current variable information: # println(_varinfo) # Use _context for context-specific operations # if _context.sampler.alg.adtype == AutoForwardDiff() # # ... specific logic ... # end x ~ Normal(0, 1) return x end ``` -------------------------------- ### Turing.jl Export List Changes Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Modifications to Turing.jl's export list, affecting what is available via unqualified `using Turing`. Several modules and macros are no longer exported, requiring more explicit imports. ```APIDOC Turing.jl Export List Changes: - Removed Exports: - `DynamicPPL` module - `AbstractMCMC` module - `@logprob_str` macro - `@prob_str` macro - Re-exporting all from `Bijectors` and `Libtask` (requires explicit imports like `using Bijectors` or `using Libtask`). - `Bijectors.ordered` (requires manual import: `using Bijectors: ordered`). - Added Exports: - `DynamicPPL.returned` (for submodels) - `DynamicPPL.prefix` (for submodels) - `LinearAlgebra.I` (for convenience) ``` -------------------------------- ### Turing.jl 0.39.2 OrderedLogistic Minimum Fix Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Corrects a bug in the support for the `OrderedLogistic` distribution by changing its minimum value from 0 to 1. ```APIDOC Distribution Fix: OrderedLogistic Minimum Value Fixed a bug in the support of the `OrderedLogistic` distribution. The minimum value has been corrected from 0 to 1. This ensures the distribution adheres to its intended parameter constraints. Example of correct usage: ```julia using Turing @model ordered_model() # Assuming 'k' is the number of categories # The parameter 'k' for OrderedLogistic should be >= 1 # For example, if categories are 1, 2, 3, then k=3 # y ~ OrderedLogistic(cutpoints, k) # where cutpoints are parameters defining the thresholds. end ``` ``` -------------------------------- ### Turing.jl BibTeX Citation Source: https://github.com/turinglang/turing.jl/blob/main/README.md Provides the BibTeX entry for citing the Turing.jl project in academic publications. This format is commonly used for academic references and includes details about authors, title, and publication venue. ```bibtex @article{10.1145/3711897, author = {Fjelde, Tor Erlend and Xu, Kai and Widmann, David and Tarek, Mohamed and Pfiffer, Cameron and Trapp, Martin and Axen, Seth D. and Sun, Xianda and Hauru, Markus and Yong, Penelope and Tebbutt, Will and Ghahramani, Zoubin and Ge, Hong}, title = {Turing.jl: a general-purpose probabilistic programming language}, year = {2025}, publisher = {Association for Computing Machinery}, address = {New York, NY, USA}, url = {https://doi.org/10.1145/3711897}, doi = {10.1145/3711897}, note = {Just Accepted}, journal = {ACM Trans.probab. Mach. Learn.}, month = feb, } @InProceedings{pmlr-v84-ge18b, title = {Turing: A Language for Flexible Probabilistic Inference}, author = {Ge, Hong and Xu, Kai and Ghahramani, Zoubin}, booktitle = {Proceedings of the Twenty-First International Conference on Artificial Intelligence and Statistics}, pages = {1682--1690}, year = {2018}, editor = {Storkey, Amos and Perez-Cruz, Fernando}, volume = {84}, series = {Proceedings of Machine Learning Research}, month = {09--11 Apr}, publisher = {PMLR}, pdf = {http://proceedings.mlr.press/v84/ge18b/ge18b.pdf}, url = {https://proceedings.mlr.press/v84/ge18b.html}, } ``` -------------------------------- ### Turing.jl Prediction API Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Functionality for generating samples from the posterior predictive distribution of a probabilistic model. This is crucial for model checking and forecasting. ```APIDOC predict(model, chain, vars...): Generate samples from the posterior predictive distribution. Parameters: model: The Turing model. chain: A chain of samples from the posterior distribution. vars: Optional variables to condition on or extract predictions for. Returns: Samples from the posterior predictive distribution. ``` -------------------------------- ### Interpolating Expressions in @model Source: https://github.com/turinglang/turing.jl/blob/main/HISTORY.md Version 0.12.0 introduced the ability to interpolate custom expressions and macros directly within `@model` definitions using `$` or `@.` for assumptions and observations, simplifying model construction. ```Julia using Turing # Example of interpolating an expression my_param = 10 @model model(data) # Interpolate a variable x ~ Normal(my_param, 1.0) # Interpolate a macro call @.` y ~ MvNormal(data, 1.0) return x, y end ``` -------------------------------- ### Turing.jl Automatic Differentiation Types Source: https://github.com/turinglang/turing.jl/blob/main/docs/src/api.md Specifies the automatic differentiation (AD) backend to be used by Turing.jl for gradient computations. These types interface with external AD libraries. ```APIDOC Turing Automatic Differentiation Backends: AutoForwardDiff: Description: Uses the ForwardDiff.jl library for forward-mode automatic differentiation. Usage: Specify as the AD backend when calling inference functions. AutoReverseDiff: Description: Uses the ReverseDiff.jl library for reverse-mode automatic differentiation. Usage: Specify as the AD backend when calling inference functions. AutoMooncake: Description: Uses the Mooncake.jl library for automatic differentiation. Usage: Specify as the AD backend when calling inference functions. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.