Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
PVGrammar.Prob.Simple
Description
This module contains a simple (and musically rather naive) probabilistic model of protovoice derivations. This model can be used to sample a derivation, evaluate a derivations probability, or infer posterior distributions of the model parmeters from given derivations (i.e., "learn" the model's probabilities).
This model is a locally conjugate model: It samples a derivation using a sequence of random decisions with certain probabilities. These probabilities are generally unknown, so they are themselves modeled as random variables with prior distributions. The full model \(p(d, \theta)\) thus splits into \[p(D, \theta) = p(d \mid \theta) \cdot p(\theta),\] the prior over the probability variables \[p(\theta) = \prod_i p(\theta_i),\] and the likelihood of the derivation(s) given these probabilities \[p(D \mid \theta) = \prod_{d \in D} p(d \mid \theta) = \prod_{d \in D} \prod_i p(d_i \mid \theta, d_0, \ldots, d_{i-1}).\] Given all prior decisions, the likelihood of a decision \(d_i\) based on some parameter \(\theta_a\) \[p(d_i \mid \theta, d_{<i})\] is conjugate with the prior of that parameter \(p(\theta_a)\), which means that the posterior of the parameters given one (or several) derivation(s) \(p(\theta \mid D)\) can be computed analytically.
The parameters \(\theta\) and their prior distributions
are represented by the higher-kinded type PVParams
.
Different instantiations of this type (using Hyper
or Probs
) results in concrete record types
that represent prior or posterior distributions
or concrete values (probabilities) for the parameters.
PVParams
also supports jeffreysPrior
and uniformPrior
as default priors,
as well as sampleProbs
for sampling from a prior (see Inferenc.Conjugate).
The likelihood \(p(d \mid \theta)\) of a derivation is represented by
sampleDerivation
.
It can be executed under different "modes" (probability monads)
for sampling, inference, or tracing (see Inference.Conjugate).
The decisions during the derivation are represented by a Trace
(here Trace PVParams
).
In order to learn from a given derivation,
the corresponding trace can be obtained using observeDerivation
.
A combination of getting a trace and learning from it
is provided by trainSinglePiece
.
Synopsis
- data PVParams f = PVParams {
- _pOuter :: PVParamsOuter f
- _pInner :: PVParamsInner f
- data PVParamsOuter f = PVParamsOuter {
- _pSingleFreeze :: f Beta
- _pDoubleLeft :: f Beta
- _pDoubleLeftFreeze :: f Beta
- _pDoubleRightSplit :: f Beta
- data PVParamsInner f = PVParamsInner {
- _pElaborateRegular :: f Beta
- _pElaborateL :: f Beta
- _pElaborateR :: f Beta
- _pRootFifths :: f Beta
- _pKeepL :: f Beta
- _pKeepR :: f Beta
- _pRepeatOverNeighbor :: f Beta
- _pNBChromatic :: f Beta
- _pNBAlt :: f Beta
- _pRepeatLeftOverRight :: f Beta
- _pRepeatAlter :: f Beta
- _pRepeatAlterUp :: f Beta
- _pRepeatAlterSemis :: f Beta
- _pConnect :: f Beta
- _pConnectChromaticLeftOverRight :: f Beta
- _pPassUp :: f Beta
- _pPassLeftOverRight :: f Beta
- _pNewPassingLeft :: f Beta
- _pNewPassingRight :: f Beta
- _pNewPassingMid :: f Beta
- _pNoteSpreadDirection :: f (Dirichlet 3)
- _pNotesOnOtherSide :: f Beta
- _pSpreadRepetitionEdge :: f Beta
- sampleDerivation :: _ => Path (Edges SPitch) (Notes SPitch) -> m (Either String [PVLeftmost SPitch])
- sampleDerivation' :: _ => m (Either String [PVLeftmost SPitch])
- observeDerivation :: [PVLeftmost SPitch] -> Path (Edges SPitch) (Notes SPitch) -> Either String (Trace PVParams)
- observeDerivation' :: [PVLeftmost SPitch] -> Either String (Trace PVParams)
- roundtrip :: FilePath -> IO (Either String [PVLeftmost SPitch])
- trainSinglePiece :: FilePath -> IO (Maybe (PVParams HyperRep))
Model Parameters
A higher-kinded type that represents the global parameters (probabilities) of the model. Use it as 'Hyper PVParams' to represent hyperparameters (priors and posteriors) or as 'Probs PVParams' to represent actual probabilites. Each record field corresponds to one parameter that influences a specific type of decision in the generation process.
The combined parameters for inner and outer operations.
Constructors
PVParams | |
Fields
|
Instances
Generic (PVParams f) Source # | |
(Show (f Beta), Show (f Beta), Show (f Beta), Show (f (Dirichlet 3)), Show (f Beta)) => Show (PVParams f) Source # | |
type Rep (PVParams f) Source # | |
Defined in PVGrammar.Prob.Simple type Rep (PVParams f) = D1 ('MetaData "PVParams" "PVGrammar.Prob.Simple" "proto-voice-model-0.1.0.0-IpbUkUcWDicKjQK7SlZCoP" 'False) (C1 ('MetaCons "PVParams" 'PrefixI 'True) (S1 ('MetaSel ('Just "_pOuter") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (PVParamsOuter f)) :*: S1 ('MetaSel ('Just "_pInner") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (PVParamsInner f)))) |
data PVParamsOuter f Source #
Parameters for decisions about outer operations (split, spread, freeze).
Constructors
PVParamsOuter | |
Fields
|
Instances
data PVParamsInner f Source #
Parameters for decisions about inner operations (elaboration and distribution within splits and spreads).
Constructors
PVParamsInner | |
Fields
|
Instances
Likelihood Model
sampleDerivation
represents a probabilistic program that samples a derivation.
that can be interpreted in various modes for
- sampling (
sampleTrace
,sampleResult
), - inference (
evalTraceLogP
,getPosterior
), - tracing (
showTrace
,traceTrace
).
observeDerivation
takes and existing derivation and returns the corresponding trace.
Arguments
:: _ | |
=> Path (Edges SPitch) (Notes SPitch) | root path |
-> m (Either String [PVLeftmost SPitch]) | a probabilistic program |
A probabilistic program that samples a derivation starting from a given root path. Can be interpreted by the interpreter functions in Inference.Conjugate.
sampleDerivation' :: _ => m (Either String [PVLeftmost SPitch]) Source #
A shorthand for sampleDerivation
starting from ⋊——⋉.
observeDerivation :: [PVLeftmost SPitch] -> Path (Edges SPitch) (Notes SPitch) -> Either String (Trace PVParams) Source #
Walk through a derivation (starting at a given root path)
and return the corresponding Trace
(if possible).
The trace can be used together with sampleDerivation
for inference (getPosterior
) or for showing the trace (printTrace
).
observeDerivation' :: [PVLeftmost SPitch] -> Either String (Trace PVParams) Source #
A shorthand for observeDerivation
starting from ⋊——⋉.
Utilities
roundtrip :: FilePath -> IO (Either String [PVLeftmost SPitch]) Source #
A helper function that tests whether observeDerivation'
followed by sampleDerivation'
restores the original derivation.
Useful for testing the compatibility of the two functions.