I have data:
q = [0.96 0.993 0.994 0.998 0.999];
n = 0.7448;
v = [0:100000];
I am trying to minimize a function where for each value of q the function is summed for all values of v (from 0 to 100000). I am using fminsearch but I think the way I have it written it is only minimizing the function for the last q value in the array. I have tried:
phatK0 = [1 100];
for j=1:length(q)
x{j} = @(phatK) sum((((q(j)).^(v.^n))-((q(j)).^((v+1).^n))).*(1-beta(abs(phatK(1)),abs(phatK(2))+v)./(beta(abs(phatK(1)),abs(phatK(2))))));
DevK = @(phatK) -2.*sum(log(x{j})-log(1-x{j}));
end
[phatK,devK] = fminsearch(DevK,phatK0,options);
phatK are the parameters I am trying to find when the function is minimized. How can I find the parameters when the function is minimized using the entire q array rather than just the last value?

 Réponse acceptée

Star Strider
Star Strider le 28 Fév 2020

0 votes

I am not certain what you want to do.
Try this:
q = [0.96 0.993 0.994 0.998 0.999];
n = 0.7448;
v = [0:100000];
phatK0 = [1 100];
for j=1:length(q)
x = @(phatK) sum((((q(j)).^(v.^n))-((q(j)).^((v+1).^n))).*(1-beta(abs(phatK(1)),abs(phatK(2))+v)./(beta(abs(phatK(1)),abs(phatK(2))))));
DevK = @(phatK) -2.*sum(log(x(phatK)-log(1-x(phatK))));
[phatK(j,:),devK(j,:)] = fminsearch(DevK,phatK0);
end
It does the optimisation in every iteration, and produces a matrix of ‘phatK’ and a vector of ‘devK’.

10 commentaires

Mary Jacketti
Mary Jacketti le 28 Fév 2020
So I would like only one phatK matrix with both values of phatK. So I want to iterate the function to sum the v for each q. But then I want to optimize the entire function with all qs to get one phatK array with 2 values.
I completely fail to understand what ‘... sum the v for each q’ means here. I assume that is what the ‘x’ function does. Note that the ’q’ values are so close to each other that ’phatK’ converges to the same values in every iteration, so one value is essentially what you get:
phatK_mean = mean(phatK)
phatK_var = var(phatK)
produces:
phatK_mean =
1.811938955660990e+00 -1.030794884954796e-08
phatK_var =
0 0
Mary Jacketti
Mary Jacketti le 28 Fév 2020
Say I have a simple function:
This function needs to be summed for each value of v= 0,1,2,3... for each q. The x will be an array that is the same size as the q array. I am trying to implement a more complicated version of this in the code where there are 2 unknown parameters for the entire function. I am trying to find the parameters for the entire x array, not just each value.
In some cases the q values will not be so close so I don't think taking the average is reasonable.
Star Strider
Star Strider le 28 Fév 2020
I am lost.
So ‘v’ is known, and ‘q’ and ‘n’ are not known? Is ‘x’ also known?
I might help to have the original equations (preferably in LaTeX form), and some description of the problem you want to solve.
Mary Jacketti
Mary Jacketti le 28 Fév 2020
v is known and is a 1x100001 array, q is known and is a 1x5 array, n is known and = 0.7448. The phatK parameters are unknown so x is also unknown. I am trying to optimize x to find the phatK parameters. However, I am struggling to write the fminsearch function because q and v are different sizes and I need to sum the function over all v values for each q value. The way I have it written now only finds the parameter values that optimize x using only the last q value. I need the parameter phatK values that optimize x using all of the q values.
Star Strider
Star Strider le 28 Fév 2020
Can we please see the original equations (preferably in LaTeX format)?
Mary Jacketti
Mary Jacketti le 28 Fév 2020
Sorry I don't have LaTeX, but this is the equation I am trying to optimize in matlab. I am summing from v=0 to v=100,000. Alpha and Beta are the unknown parameters.
Try this:
function x = fcn(phatK)
f = @(q,n,v,a,b) sum((q.^v.^n - q.^(v+1).^n) .* (1 - beta(a,b+v)./beta(a,b)));
qv = [0.96 0.993 0.994 0.998 0.999];
n = 0.7448;
v = 0:1E+8;
phatK = phatK(:);
for k = 1:numel(qv)
xv(:,k) = f(qv(k),n,v,phatK(1),phatK(2));
end
x = norm(xv)
end
phatK0 = [10 100];
[phatK,x] = fminsearch(@fcn, phatK0)
I believe it does what the equation you posted does (although I have no idea what that equation is or what you are doing). It takes forever because of the size of ‘v’, and occasionally throws this error:
Error using gammaln
Input must be nonnegative.
You will need to save the ‘fun’ function as ‘fun.m’ (or whatever you want to call it, however it is best if the function and the file it is saved to have the same names).
There may be better functions than fminsearch to optimise this. Since I have no idea what you are doing, I cannot determine that.
If this does not do what you want, post back to that effect and I will delete my Answer. I cannot help further.
Mary Jacketti
Mary Jacketti le 28 Fév 2020
This works! Thank you!!
Star Strider
Star Strider le 28 Fév 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by