## Metodiev Martin

### 2025-07-14

## Preparing the data

Load the galaxy dataset:

![plot of chunk load data](figure/load data-1.png)

## Computing the sample from MCMC output using bayesmix

Define the logposterior as a function of G and y:

Calculating the MCMC output for 2 components:

    ##
    ## Attaching package: 'bayesmix'

    ## The following object is masked from 'package:Rfast':
    ##
    ##     Sort

    ## Compiling model graph
    ##    Declaring variables
    ##    Resolving undeclared variables
    ##    Allocating nodes
    ## Graph information:
    ##    Observed stochastic nodes: 82
    ##    Unobserved stochastic nodes: 88
    ##    Total graph size: 345
    ##
    ## Initializing model

![plot of chunk mcmc output for 2 components](figure/mcmc output for 2 components-1.png)![plot of chunk mcmc output for 2 components](figure/mcmc output for 2 components-2.png)![plot of chunk mcmc output for 2 components](figure/mcmc output for 2 components-3.png)

Same for 3 components:

    ## Compiling model graph
    ##    Declaring variables
    ##    Resolving undeclared variables
    ##    Allocating nodes
    ## Graph information:
    ##    Observed stochastic nodes: 82
    ##    Unobserved stochastic nodes: 90
    ##    Total graph size: 348
    ##
    ## Initializing model

![plot of chunk mcmc output for 3 components](figure/mcmc output for 3 components-1.png)![plot of chunk mcmc output for 3 components](figure/mcmc output for 3 components-2.png)![plot of chunk mcmc output for 3 components](figure/mcmc output for 3 components-3.png)

## Computing the THAMES

Estimates of the logarithm of the marginal likelihood:

    ## [1] 2.44949
    ## [1] 2

    ## [1] 3
    ## [1] 3

    ##      G = 2     G = 3
    ## 1 -235.254 -226.6771

It is also possible to plot the overlap graphs and compute the criterion of overlap (co):

![plot of chunk plot overlap g2 and g3](figure/plot overlap g2 and g3-1.png)![plot of chunk plot overlap g2 and g3](figure/plot overlap g2 and g3-2.png)

    ##   G = 2 G = 3
    ## 1     2     3

This can also be done directly, without computing the THAMES:

![plot of chunk plot overlap g2 and g3 using overlapgraph function](figure/plot overlap g2 and g3 using overlapgraph function-1.png)![plot of chunk plot overlap g2 and g3 using overlapgraph function](figure/plot overlap g2 and g3 using overlapgraph function-2.png)

    ##   G = 2 G = 3
    ## 1     2     3