Parameter estimation of a mixture of a Normal and a Uniform distribution

조회 수: 3 (최근 30일)
Gorbz
Gorbz 2022년 6월 13일
댓글: Jeff Miller 2022년 6월 14일
I am trying to estimate the parameters of a 1d Normal and Uniform distribution mixture based on a given dataset.
I asked the same question on Cross Validated and Stack Overflow for a Python solution: https://stats.stackexchange.com/questions/578352/extracting-statistical-parameters-from-a-mixture-of-two-distributions-of-differe
However, a similar Cross Validated question appeared in my search. The following one: https://stats.stackexchange.com/questions/173404/modeling-a-mixture-of-a-gaussian-and-uniform-matlab
There, the OP generates a dataset from a normal distribution and a uniform one as:
Data = [5+3*randn(2000,1); unifrnd(-100,100,2000,1)];
where the normal has mean 5 and standard deviation 3. In total, there are 4000 samples. Then, the OP essentially creates the mixture as:
MixPDF = @(x,m,s,weight) (weight*normpdf(x,m,s) + (1-weight)*(1/( max(Data)-min(Data))));
And proceeds to the parameter estimation using MLE:
p = mle(Data, 'pdf', MixPDF, 'start', [0 1 0.5],'lowerbound', [-20 0.1 0], 'upperbound', [20 50 1])
As per the rest of the discussion in that thread, an indicator function is needed in order to ensure the uniform is well behaved. The code given reads:
density = 1/(UL - LL) * indicator(LL <= x <= UL). indicator(LL <= x <= UL)
Questions:
(1) In this example, when trying to implement the indicator function I get errors:
Unrecognized function or variable 'x'.
Error in Normal_Uniform (line 14)
density = 1/(UL - LL) * indicator(LL <= x <= UL). indicator(LL <= x <= UL)
My question is how to correctly implement this function in conjunction with the previous code?
(2) Most importantly I am interested in a similar situation with a dataset b given below which is less "well" behaved. The histogram for this dataset looks like:
It is my belief that indeed this corresponds to a normal and a uniform distribution where I would like to extract the standard deviation of the gaussian and the mixture parameters for each. Assume I know the mean is -2.25 while the uniformdistribution is located between say -2.25 and -1.25 .
However, the code above seems to think that there is 0% mix of the normal one and all I have is a uniform.
How can I adapt this code so that I get something meaningful out of this distribution mixture based on b?
b =[-2.587890625, -2.4765625, -2.427734375, -2.349609375, -1.509765625, -1.79296875, -1.537109375, -1.833984375, -2.212890625, -2.17578125, -1.810546875, -2.859375, -1.720703125, -1.373046875, -1.677734375, -1.84765625, -2.197265625, -2.767578125, -1.921875, -2.26171875, -2.00390625, -1.41015625, -2.52734375, -1.798828125, -2.302734375, -1.46484375, -2.9140625, -2.220703125, -2.4296875, -1.2734375, -1.806640625, -1.484375, -2.966796875, -2.8046875, -1.841796875, -1.572265625, -1.595703125, -1.98046875, -2.330078125, -1.6484375, -2.2890625, -1.515625, -1.666015625, -1.767578125, -1.673828125, -1.564453125, -1.734375, -2.6328125, -2.833984375, -1.564453125, -1.56640625, -2.595703125, -1.50390625, -1.943359375, -1.85546875, -2.041015625, -3.169921875, -1.33984375, -1.498046875, -1.890625, -2.35546875, -1.396484375, -1.49609375, -2.15625, -2.484375, -2.828125, -2.03125, -2.560546875, -2.970703125, -2.548828125, -2.287109375, -2.337890625, -1.537109375, -1.7734375, -2.666015625, -2.1640625, -1.845703125, -2.498046875, -2.365234375, -1.48828125, -1.68359375, -1.416015625, -3.16015625, -2.21484375, -1.3203125, -2.337890625, -1.33203125, -1.369140625, -1.869140625, -1.390625, -2.119140625, -2.966796875, -2.513671875, -1.81640625, -2.48828125, -2.91015625, -2.1875, -2.48046875, -1.337890625, -2.35546875, -1.62109375, -1.568359375, -2.083984375, -1.24609375, -2.505859375, -2.078125, -1.892578125, -1.833984375, -1.951171875, -2.6953125, -1.44140625, -3.01953125, -1.849609375, -3.06640625, -1.451171875, -2.16015625, -2.392578125, -2.939453125, -2.578125, -1.396484375, -1.529296875, -1.833984375, -1.59765625, -1.595703125, -2.033203125, -1.427734375, -2.68359375, -3.1640625, -1.818359375, -3.115234375, -2.9609375, -2.0078125, -1.857421875, -1.83203125, -2.392578125, -1.84765625, -1.33984375, -2.583984375, -2.05859375, -1.27734375, -2.314453125, -2.62890625, -1.64453125, -1.52734375, -1.46875, -2.306640625, -2.369140625, -2.296875, -1.80859375, -3.037109375, -1.96875, -2.166015625, -3.154296875, -1.9453125, -1.88671875, -1.29296875, -1.435546875, -2.01953125, -1.9921875, -2.197265625, -1.94921875, -1.751953125, -2.71875, -2.529296875, -2.2578125, -1.51953125, -2.357421875, -1.62109375, -2.068359375, -2.09375, -1.470703125, -2.236328125, -3.1171875, -2.009765625, -2.08203125, -2.96484375, -1.728515625, -3.064453125, -2.017578125, -2.232421875, -1.51171875, -1.796875, -2.05078125, -1.953125, -1.65234375, -2.130859375, -2.083984375, -1.970703125, -3.119140625, -1.33984375, -2.458984375, -1.76171875, -2.095703125, -1.921875, -2.044921875, -1.677734375, -3.16796875, -1.61328125, -1.662109375, -1.796875, -1.7734375, -2.4140625, -1.986328125, -2.33203125, -1.234375, -2.708984375, -2.2109375, -2.001953125, -1.302734375, -2.01171875, -1.298828125, -3.146484375, -2.462890625, -3.111328125, -2.84765625, -1.720703125, -1.78125, -2.3671875, -1.3671875, -2.009765625, -1.423828125, -1.275390625, -1.6171875, -2.306640625, -2.732421875, -2.328125, -2.80859375, -2.060546875, -1.98046875, -2.0625, -2.01171875, -1.857421875, -2.26171875, -2.083984375, -2.982421875, -2.6953125, -1.916015625, -1.3125, -1.296875, -3.177734375, -2.701171875, -2.21875, -1.7265625, -1.982421875, -2.015625, -1.376953125, -2.232421875, -3.16015625, -1.80859375, -2.015625, -3.1875, -2.248046875, -1.974609375, -2.23046875, -1.765625, -3.12890625, -2.17578125, -1.9140625, -1.669921875, -2.224609375, -1.97265625, -2.34765625, -2.83984375, -2.767578125, -1.986328125, -3.166015625, -1.943359375, -2.498046875, -2.830078125, -2.201171875, -1.53125, -2.05859375, -3.08203125, -2.48046875, -1.541015625, -1.94921875, -1.76953125, -1.736328125, -2.09765625, -1.880859375, -2.01171875, -1.55859375, -1.322265625, -1.310546875, -1.8125, -1.65625, -2.189453125, -1.763671875, -1.634765625, -1.59375, -1.3046875, -1.58984375, -1.658203125, -2.560546875, -1.375, -2.76171875, -3.166015625, -2.03125, -2.59375, -2.01953125, -1.814453125, -1.3828125, -2.259765625, -2.03515625, -1.779296875, -2.205078125, -2.71875, -2.37890625, -2.55078125, -1.876953125, -1.4765625, -2.240234375, -2.259765625, -2.97265625, -2.22265625, -1.787109375, -1.7734375, -2.369140625, -1.6484375, -1.900390625, -2.064453125, -2.1796875, -1.810546875, -2.53125, -2.08203125, -2.103515625, -1.453125, -2.615234375, -2.21484375, -1.48046875, -2.666015625, -1.912109375, -2.294921875, -2.0546875, -1.35546875, -2.044921875, -2.0234375, -1.6171875, -2.009765625, -2.322265625, -1.931640625, -1.8359375, -1.748046875, -1.8984375, -1.890625, -1.92578125, -1.845703125, -1.7265625, -1.646484375, -1.966796875, -2.828125, -2.26171875, -2.04296875, -1.88671875, -1.662109375, -2.265625, -1.408203125, -1.447265625, -1.900390625, -1.470703125, -1.599609375, -1.791015625, -1.802734375, -2.0703125, -2.04296875, -1.6875, -1.701171875, -2.08203125, -2.62890625, -2.34765625, -1.669921875, -1.828125, -2.736328125, -2.35546875, -1.537109375, -2.2265625, -2.66796875, -2.162109375, -2.392578125, -1.66796875, -2.134765625, -1.90625, -1.884765625, -2.265625, -2.330078125, -2.005859375, -1.79296875, -2.06640625, -1.642578125, -1.658203125, -1.447265625, -1.578125, -2.65234375, -2.259765625, -1.546875, -2.904296875, -1.900390625, -1.75, -2.458984375, -2.08203125, -1.53515625, -1.87109375, -1.3671875, -2.75390625, -1.330078125, -1.408203125, -1.73046875, -3.126953125, -1.87890625, -1.494140625, -1.87890625, -1.578125, -1.712890625, -2.375, -2.244140625, -1.67578125, -2.771484375, -2.00390625, -2.46875, -2.666015625, -1.4375, -1.7734375, -3.0625, -2.1875, -2.998046875, -2.212890625, -1.958984375, -1.611328125, -2.2109375, -1.564453125, -1.263671875, -2.0625, -1.6796875, -2.0, -2.998046875, -1.654296875, -1.912109375, -2.951171875, -1.416015625, -2.390625, -2.576171875, -2.017578125, -1.90234375, -2.388671875, -1.419921875, -1.326171875, -2.35546875, -1.986328125, -2.880859375, -3.111328125, -2.501953125, -2.69140625, -2.265625, -2.14453125, -2.2265625, -1.619140625, -1.638671875, -2.232421875, -1.4765625, -2.798828125, -2.927734375, -2.03515625, -2.25390625, -2.064453125, -2.35546875, -2.056640625, -1.47265625, -1.537109375, -2.263671875, -1.701171875, -2.40234375, -1.255859375, -1.75390625, -2.896484375, -2.236328125, -1.716796875, -1.673828125, -2.7109375, -1.931640625, -2.439453125, -2.33984375, -1.361328125, -1.7109375, -1.677734375, -2.046875, -1.6875, -2.251953125, -2.693359375, -2.111328125, -2.072265625, -1.48046875, -2.744140625, -2.5390625, -1.76953125, -1.6875, -1.953125, -1.357421875, -2.2109375, -1.521484375, -1.603515625, -1.4375, -2.1953125, -1.462890625, -1.818359375, -2.23046875, -2.8125, -1.953125, -1.6640625, -1.919921875, -2.583984375, -3.169921875];
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 6월 14일
(LL <= x <= UL)
Unless you are using the Symbolic Toolbox, that code would be interpreted as
((LL <= x) <= UL)
The first part would return an array of logical values, each 0 (false) or 1 (true), and it would be the 0 1 that would be tested <= UL

댓글을 달려면 로그인하십시오.

답변 (1개)

Jeff Miller
Jeff Miller 2022년 6월 14일
편집: Jeff Miller 2022년 6월 14일
Not sure how to modify your code, but you can do the estimation with Cupid like this:
% Parameters stipulated to be known:
KnownMu = -2.25;
KnownULower = -2.25;
KnownUUpper = -1.25;
% Guesses for the unknown parameters:
GuessSigma = 1;
GuessProbNormal = 0.5;
Theory = Mixture(GuessProbNormal,Normal(KnownMu,GuessSigma),Uniform(KnownULower,KnownUUpper));
b =[-2.587890625, -2.4765625, -2.427734375, -2.349609375, -1.509765625, -1.79296875, -1.537109375, -1.833984375, -2.212890625, -2.17578125, -1.810546875, -2.859375, -1.720703125, -1.373046875, -1.677734375, -1.84765625, -2.197265625, -2.767578125, -1.921875, -2.26171875, -2.00390625, -1.41015625, -2.52734375, -1.798828125, -2.302734375, -1.46484375, -2.9140625, -2.220703125, -2.4296875, -1.2734375, -1.806640625, -1.484375, -2.966796875, -2.8046875, -1.841796875, -1.572265625, -1.595703125, -1.98046875, -2.330078125, -1.6484375, -2.2890625, -1.515625, -1.666015625, -1.767578125, -1.673828125, -1.564453125, -1.734375, -2.6328125, -2.833984375, -1.564453125, -1.56640625, -2.595703125, -1.50390625, -1.943359375, -1.85546875, -2.041015625, -3.169921875, -1.33984375, -1.498046875, -1.890625, -2.35546875, -1.396484375, -1.49609375, -2.15625, -2.484375, -2.828125, -2.03125, -2.560546875, -2.970703125, -2.548828125, -2.287109375, -2.337890625, -1.537109375, -1.7734375, -2.666015625, -2.1640625, -1.845703125, -2.498046875, -2.365234375, -1.48828125, -1.68359375, -1.416015625, -3.16015625, -2.21484375, -1.3203125, -2.337890625, -1.33203125, -1.369140625, -1.869140625, -1.390625, -2.119140625, -2.966796875, -2.513671875, -1.81640625, -2.48828125, -2.91015625, -2.1875, -2.48046875, -1.337890625, -2.35546875, -1.62109375, -1.568359375, -2.083984375, -1.24609375, -2.505859375, -2.078125, -1.892578125, -1.833984375, -1.951171875, -2.6953125, -1.44140625, -3.01953125, -1.849609375, -3.06640625, -1.451171875, -2.16015625, -2.392578125, -2.939453125, -2.578125, -1.396484375, -1.529296875, -1.833984375, -1.59765625, -1.595703125, -2.033203125, -1.427734375, -2.68359375, -3.1640625, -1.818359375, -3.115234375, -2.9609375, -2.0078125, -1.857421875, -1.83203125, -2.392578125, -1.84765625, -1.33984375, -2.583984375, -2.05859375, -1.27734375, -2.314453125, -2.62890625, -1.64453125, -1.52734375, -1.46875, -2.306640625, -2.369140625, -2.296875, -1.80859375, -3.037109375, -1.96875, -2.166015625, -3.154296875, -1.9453125, -1.88671875, -1.29296875, -1.435546875, -2.01953125, -1.9921875, -2.197265625, -1.94921875, -1.751953125, -2.71875, -2.529296875, -2.2578125, -1.51953125, -2.357421875, -1.62109375, -2.068359375, -2.09375, -1.470703125, -2.236328125, -3.1171875, -2.009765625, -2.08203125, -2.96484375, -1.728515625, -3.064453125, -2.017578125, -2.232421875, -1.51171875, -1.796875, -2.05078125, -1.953125, -1.65234375, -2.130859375, -2.083984375, -1.970703125, -3.119140625, -1.33984375, -2.458984375, -1.76171875, -2.095703125, -1.921875, -2.044921875, -1.677734375, -3.16796875, -1.61328125, -1.662109375, -1.796875, -1.7734375, -2.4140625, -1.986328125, -2.33203125, -1.234375, -2.708984375, -2.2109375, -2.001953125, -1.302734375, -2.01171875, -1.298828125, -3.146484375, -2.462890625, -3.111328125, -2.84765625, -1.720703125, -1.78125, -2.3671875, -1.3671875, -2.009765625, -1.423828125, -1.275390625, -1.6171875, -2.306640625, -2.732421875, -2.328125, -2.80859375, -2.060546875, -1.98046875, -2.0625, -2.01171875, -1.857421875, -2.26171875, -2.083984375, -2.982421875, -2.6953125, -1.916015625, -1.3125, -1.296875, -3.177734375, -2.701171875, -2.21875, -1.7265625, -1.982421875, -2.015625, -1.376953125, -2.232421875, -3.16015625, -1.80859375, -2.015625, -3.1875, -2.248046875, -1.974609375, -2.23046875, -1.765625, -3.12890625, -2.17578125, -1.9140625, -1.669921875, -2.224609375, -1.97265625, -2.34765625, -2.83984375, -2.767578125, -1.986328125, -3.166015625, -1.943359375, -2.498046875, -2.830078125, -2.201171875, -1.53125, -2.05859375, -3.08203125, -2.48046875, -1.541015625, -1.94921875, -1.76953125, -1.736328125, -2.09765625, -1.880859375, -2.01171875, -1.55859375, -1.322265625, -1.310546875, -1.8125, -1.65625, -2.189453125, -1.763671875, -1.634765625, -1.59375, -1.3046875, -1.58984375, -1.658203125, -2.560546875, -1.375, -2.76171875, -3.166015625, -2.03125, -2.59375, -2.01953125, -1.814453125, -1.3828125, -2.259765625, -2.03515625, -1.779296875, -2.205078125, -2.71875, -2.37890625, -2.55078125, -1.876953125, -1.4765625, -2.240234375, -2.259765625, -2.97265625, -2.22265625, -1.787109375, -1.7734375, -2.369140625, -1.6484375, -1.900390625, -2.064453125, -2.1796875, -1.810546875, -2.53125, -2.08203125, -2.103515625, -1.453125, -2.615234375, -2.21484375, -1.48046875, -2.666015625, -1.912109375, -2.294921875, -2.0546875, -1.35546875, -2.044921875, -2.0234375, -1.6171875, -2.009765625, -2.322265625, -1.931640625, -1.8359375, -1.748046875, -1.8984375, -1.890625, -1.92578125, -1.845703125, -1.7265625, -1.646484375, -1.966796875, -2.828125, -2.26171875, -2.04296875, -1.88671875, -1.662109375, -2.265625, -1.408203125, -1.447265625, -1.900390625, -1.470703125, -1.599609375, -1.791015625, -1.802734375, -2.0703125, -2.04296875, -1.6875, -1.701171875, -2.08203125, -2.62890625, -2.34765625, -1.669921875, -1.828125, -2.736328125, -2.35546875, -1.537109375, -2.2265625, -2.66796875, -2.162109375, -2.392578125, -1.66796875, -2.134765625, -1.90625, -1.884765625, -2.265625, -2.330078125, -2.005859375, -1.79296875, -2.06640625, -1.642578125, -1.658203125, -1.447265625, -1.578125, -2.65234375, -2.259765625, -1.546875, -2.904296875, -1.900390625, -1.75, -2.458984375, -2.08203125, -1.53515625, -1.87109375, -1.3671875, -2.75390625, -1.330078125, -1.408203125, -1.73046875, -3.126953125, -1.87890625, -1.494140625, -1.87890625, -1.578125, -1.712890625, -2.375, -2.244140625, -1.67578125, -2.771484375, -2.00390625, -2.46875, -2.666015625, -1.4375, -1.7734375, -3.0625, -2.1875, -2.998046875, -2.212890625, -1.958984375, -1.611328125, -2.2109375, -1.564453125, -1.263671875, -2.0625, -1.6796875, -2.0, -2.998046875, -1.654296875, -1.912109375, -2.951171875, -1.416015625, -2.390625, -2.576171875, -2.017578125, -1.90234375, -2.388671875, -1.419921875, -1.326171875, -2.35546875, -1.986328125, -2.880859375, -3.111328125, -2.501953125, -2.69140625, -2.265625, -2.14453125, -2.2265625, -1.619140625, -1.638671875, -2.232421875, -1.4765625, -2.798828125, -2.927734375, -2.03515625, -2.25390625, -2.064453125, -2.35546875, -2.056640625, -1.47265625, -1.537109375, -2.263671875, -1.701171875, -2.40234375, -1.255859375, -1.75390625, -2.896484375, -2.236328125, -1.716796875, -1.673828125, -2.7109375, -1.931640625, -2.439453125, -2.33984375, -1.361328125, -1.7109375, -1.677734375, -2.046875, -1.6875, -2.251953125, -2.693359375, -2.111328125, -2.072265625, -1.48046875, -2.744140625, -2.5390625, -1.76953125, -1.6875, -1.953125, -1.357421875, -2.2109375, -1.521484375, -1.603515625, -1.4375, -2.1953125, -1.462890625, -1.818359375, -2.23046875, -2.8125, -1.953125, -1.6640625, -1.919921875, -2.583984375, -3.169921875];
Theory.EstML(b,'rfrff');
% 'rfrff' means:
% r: adjust mixture probability as a real number
% f: do not adjust normal mean--treat as fixed
% r: adjust normal sigma as a real number
% f: do not adjust lower limit of uniform
% f: do not adjust upper limit of uniform
Theory.StringName
ans =
'Mixture(0.60529,Normal(-2.25,0.47743),0.39471,Uniform(-2.25,-1.25))'
so the maximum likelihood estimates are that about 60% of the scores come from a normal with sigma=0.477 and the rest come from your uniform.
  댓글 수: 3
Gorbz
Gorbz 2022년 6월 14일
Ok, there is a typo in your answer: `KnownUUpper` in the beginning should be `KnownUpper`. Thanks.
Jeff Miller
Jeff Miller 2022년 6월 14일
Thanks for spotting the typo--sorry about that.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by