In our previous tutorial we constructed a Normal distribution with a variety of parameterisations. Now we will look at how to access the parameters of the distribution and how to update them.

## Accessing Parameters

First we will construct the Standard Normal distribution, the default parameterisation

N <- Normal$new() To view all parameters in the distribution we use the parameters method N$parameters()
#>      id value support                                 description
#> 1: mean     0       ℝ                   Mean - Location Parameter
#> 2:  var     1      ℝ+          Variance - Squared Scale Parameter
#> 3:   sd     1      ℝ+        Standard Deviation - Scale Parameter
#> 4: prec     1      ℝ+ Precision - Inverse Squared Scale Parameter

Notice how not only the parameters in the given parameterisation are displayed, but all the ones possible. To filter this table for a specific parameter, add the parameter name as an argument

N$parameters("var") #> id value support settable description #> 1: var 1 <PosReals[25]> TRUE Variance - Squared Scale Parameter Or to only see the value of the parameter N$getParameterValue("prec")
#> [1] 1

## Updating Parameters

Any parameter can be updated in distr6 using the setParameterValue method and all others are updated accordingly. For example,

N$setParameterValue(var = 2) N$parameters()
#>      id    value support                                 description
#> 1: mean        0       ℝ                   Mean - Location Parameter
#> 2:  var        2      ℝ+          Variance - Squared Scale Parameter
#> 3:   sd 1.414214      ℝ+        Standard Deviation - Scale Parameter
#> 4: prec      0.5      ℝ+ Precision - Inverse Squared Scale Parameter

Not only has the variance been updated, but so too have the precision and standard deviation parameters. To see the function used for updating you can run N$parameters("prec", update = TRUE) but the output is given in a form that your computer can read, so it may not be straightforward to follow for more complicated functions. See the distribution help page, ?Normal for a more readable version. Internal checks ensure that only valid parameter values are allowed, for example setting the variance to a negative number will throw an error. Finally, multiple parameters can be updated at the same time N$setParameterValue(mean = 4, sd = 2)
N$parameters() #> id value support description #> 1: mean 4 ℝ Mean - Location Parameter #> 2: var 4 ℝ+ Variance - Squared Scale Parameter #> 3: sd 2 ℝ+ Standard Deviation - Scale Parameter #> 4: prec 0.25 ℝ+ Precision - Inverse Squared Scale Parameter But be careful not to try and update conflicting parameters, for example trying to update the variance and precision in the Normal distribution will cause an error: N$setParameterValue(var = 2, prec = 3)
#> Error in FUN(X[[i]], ...): Conflicting parametrisations detected. Only one of {var, sd, prec} should be given.

In rare cases you may want to allow the method to resolve conflicts automatically, but this should be used with care and only with very good reason:

N$setParameterValue(var = 2, prec = 3, resolveConflicts = TRUE) N$print()
#> Norm(mean = 4, var = 2, sd = 1.4142135623731, prec = 0.5)

## Summary

In this tutorial we looked at getting and setting parameters for the Normal distribution. In the next tutorial we look at accessing mathematical and statistical methods including the d/p/q/r functions.