proto-voice-model-0.1.0.0
Safe HaskellSafe-Inferred
LanguageGHC2021

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

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.

data PVParams f Source #

The combined parameters for inner and outer operations.

Constructors

PVParams 

Instances

Instances details
Generic (PVParams f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

Associated Types

type Rep (PVParams f) :: Type -> Type #

Methods

from :: PVParams f -> Rep (PVParams f) x #

to :: Rep (PVParams f) x -> PVParams f #

(Show (f Beta), Show (f Beta), Show (f Beta), Show (f (Dirichlet 3)), Show (f Beta)) => Show (PVParams f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

Methods

showsPrec :: Int -> PVParams f -> ShowS #

show :: PVParams f -> String #

showList :: [PVParams f] -> ShowS #

type Rep (PVParams f) Source # 
Instance details

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).

Instances

Instances details
Generic (PVParamsOuter f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

Associated Types

type Rep (PVParamsOuter f) :: Type -> Type #

Show (f Beta) => Show (PVParamsOuter f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

type Rep (PVParamsOuter f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

type Rep (PVParamsOuter f) = D1 ('MetaData "PVParamsOuter" "PVGrammar.Prob.Simple" "proto-voice-model-0.1.0.0-IpbUkUcWDicKjQK7SlZCoP" 'False) (C1 ('MetaCons "PVParamsOuter" 'PrefixI 'True) ((S1 ('MetaSel ('Just "_pSingleFreeze") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pDoubleLeft") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta))) :*: (S1 ('MetaSel ('Just "_pDoubleLeftFreeze") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pDoubleRightSplit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)))))

data PVParamsInner f Source #

Parameters for decisions about inner operations (elaboration and distribution within splits and spreads).

Instances

Instances details
Generic (PVParamsInner f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

Associated Types

type Rep (PVParamsInner f) :: Type -> Type #

(Show (f Beta), Show (f Beta), Show (f Beta), Show (f (Dirichlet 3)), Show (f Beta)) => Show (PVParamsInner f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

type Rep (PVParamsInner f) Source # 
Instance details

Defined in PVGrammar.Prob.Simple

type Rep (PVParamsInner f) = D1 ('MetaData "PVParamsInner" "PVGrammar.Prob.Simple" "proto-voice-model-0.1.0.0-IpbUkUcWDicKjQK7SlZCoP" 'False) (C1 ('MetaCons "PVParamsInner" 'PrefixI 'True) ((((S1 ('MetaSel ('Just "_pElaborateRegular") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pElaborateL") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta))) :*: (S1 ('MetaSel ('Just "_pElaborateR") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: (S1 ('MetaSel ('Just "_pRootFifths") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pKeepL") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta))))) :*: ((S1 ('MetaSel ('Just "_pKeepR") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: (S1 ('MetaSel ('Just "_pRepeatOverNeighbor") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pNBChromatic") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)))) :*: (S1 ('MetaSel ('Just "_pNBAlt") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: (S1 ('MetaSel ('Just "_pRepeatLeftOverRight") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pRepeatAlter") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)))))) :*: (((S1 ('MetaSel ('Just "_pRepeatAlterUp") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: (S1 ('MetaSel ('Just "_pRepeatAlterSemis") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pConnect") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)))) :*: (S1 ('MetaSel ('Just "_pConnectChromaticLeftOverRight") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: (S1 ('MetaSel ('Just "_pPassUp") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pPassLeftOverRight") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta))))) :*: ((S1 ('MetaSel ('Just "_pNewPassingLeft") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: (S1 ('MetaSel ('Just "_pNewPassingRight") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pNewPassingMid") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)))) :*: (S1 ('MetaSel ('Just "_pNoteSpreadDirection") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f (Dirichlet 3))) :*: (S1 ('MetaSel ('Just "_pNotesOnOtherSide") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta)) :*: S1 ('MetaSel ('Just "_pSpreadRepetitionEdge") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f Beta))))))))

Likelihood Model

sampleDerivation represents a probabilistic program that samples a derivation. that can be interpreted in various modes for

observeDerivation takes and existing derivation and returns the corresponding trace.

sampleDerivation Source #

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.

trainSinglePiece :: FilePath -> IO (Maybe (PVParams HyperRep)) Source #

Helper function: Load a single derivation and infer the corresponding posterior for a uniform prior.