# How to use parameter sampling

Meld supports the Bayesian sampling of parameters. See Bayesian Sampling of Parameters for background.

MELD currently only supports parameter sampling for the number of active restraints in a group and for the number of active groups in a collection. In future other sampled parameters will be added.

## Types of parameters

MELD supports both `ContinuousParameter`

and `DiscreteParameter`

parameters. Continuous parameters can take on any floating point value.
Discrete parameters are restricted to integer values.

Currently, MELD only supports parameter sampling for groups and collections,
which use `DiscreteParameter`

, so only these will be discussed.

## Priors

A prior encodes the prior beleif about the likely values of a parameter. The key quantity is the log of the prior probability, which MELD treats as an energy in units of \(k_BT\).

Note

This means that the prior has the same strength regardless of temperature, whereas the physics-based prior (i.e. the energy) becomes flatter with increasing temperature.

There are two types of priors. A `DiscreteUniformPrior`

encodes a flat
prior with no prefered value. A `DiscreteExponentialPrior`

encodes
a prior that exponentially favors low or high values, depending on the sign
of the parameter `k`

that controls how strong this preference is.

## Samplers

Each parameter must have a sampler, which determines the minimum and maximum allowed values. The sampler also sets the step size of Monte Carlo moves for this parameter.

## Monte Carlo Steps

After each round of molecular dynamics steps, the parameters are updated using a series
of Monte Carlo trials. The number of trials is controlled by the `param_mcmc_steps`

option of the `RunOption`

object.

## Puting it together

Assuming we have a MELD system called `system`

, we can create a new parameter:

```
1from meld.system import param_sampling
2
3prior = param_sampling.DiscreteExponentialPrior(k=1.0)
4sampler = param_sampling.DiscreteSampler(50, 100, 5)
5param = system.param_sampler.add_discrete_parameter("param", 75, prior, sampler)
```

**Line 3**: Creates a prior that favors higher values with \(-1 k_BT\) energy
contribution for each unit of increase in the parameter.

**Line 4**: Creates a sampler with a minimum value of 50 and a maximum value of 100,
both inclusive. It uses a step size of 5, so that random moves are attemped
\(\pm 5\) from the current value.

**Line 5**: Creates the parameter. Each parameter must have a unique name, `"param"`

in this case. We must also specify the initial value, here `75`

. The resulting
object, `param`

, can then be passed to other places in the MELD code base.
Currently this can only be used as `num_active`

when creating a group or collection.