fitdist
for a beta distribution gets the distribution parameters from betafit
, which sets up an appropriate likelihood function for the distribution given your data, some heuristic initial guesses for a
and b
and then optimises log(a)
and log(b)
to maximise likelihood using fminsearch
.
Your constraint defining the distribution mean establishes an enforced relationship between a
and b
. From Wikipedia the mean mu
is related to a
and b
thus:
mu = 1 / (1 + b/a)
This can be rearranged to give one distribution parameter given the other:
b = a * (1/mu - 1)
To examine the unconstrained implementation of beta distribution fitting that is available in MATLAB, and that you're seeking to constraint, you can view the betafit
source code using:
edit betafit
At least in MATLAB R2018b you will find that the optimisation of log(a)
and log(b)
takes place at a point which declares:
phat = fminsearch(negloglike,pstart,opts);
phat = exp(phat);
Your constrained distribution fitting problem can be described in terms of the optimised objective function used here, which could allow you to reuse the other aspects of betafit
's behaviour:
negloglike1 = @(loga) negloglike([loga log(exp(loga) * (1/mu - 1))]);
You could either create your own duplicate of betafit
which makes this declaration before the call to fminsearch
, or stop the built-in betafit
on a breakpoint after the line has been called and declare the new likelihood function from the command line. Either way, you can then replace the parameters with constrained ones which maximise likelihood within this constraint:
loga = fminsearch(negloglike1,pstart(1),opts);
phat = exp(loga) * [1 (1/mu - 1)];
The resulting beta distribution parameters phat
will be guaranteed to result in a distribution with mean mu
, and locally maximise the likelihood function for your data given this constraint.