Related
I am trying to use mle() function in MATLAB to estimate the parameters of a 6-parameter custom distribution.
The PDF of the custom distribution is
and the CDF is
where Γ(x,y) and Γ(x) are the upper incomplete gamma function and the gamma function, respectively. α, θ, β, a, b, and c are the parameters of the custom distribution. K is given by
Given a data vector 'data', I want to estimate the parameters α, θ, β, a, b, and c.
So, far I have come up with this code:
data = rand(20000,1); % Since I cannot upload the acutal data, we may use this
t = 0:0.0001:0.5;
fun = #(w,a,b,c) w^(a-1)*(1-w)^(b-1)*exp^(-c*w);
% to estimate the parameters
custpdf = #(data,myalpha,mybeta,mytheta,a,b,c)...
((integral(#(t)fun(t,a,b,c),0,1)^-1)*...
mybeta*...
igamma(myalpha,((mytheta/t)^mybeta)^(a-1))*...
(mytheta/t)^(myalpha*mybeta+1)*...
exp(-(mytheta/t)^mybeta-(c*(igamma(myalpha,(mytheta/t)^mybeta)/gamma(myalpha)))))...
/...
(mytheta*...
gamma(myalpha)^(a+b-1)*...
(gamma(myalpha)-igamma(myalpha,(mytheta/t)^mybeta))^(1-b));
custcdf = #(data,myalpha,mybeta,mytheta,a,b,c)...
(integral(#(t)fun(t,a,b,c),0,1)^-1)*...
integral(#(t)fun(t,a,b,c),0,igamma(myalpha,(mytheta/t)^mybeta)^mybeta/gamma(myalpha));
phat = mle(data,'pdf',custpdf,'cdf',custcdf,'start',0.0);
But I get the following error:
Error using mlecustom (line 166)
Error evaluating the user-supplied pdf function
'#(data,myalpha,mybeta,mytheta,a,b,c)((integral(#(t)fun(t,a,b,c),0,1)^-1)*mybeta*igamma(myalpha,((mytheta/t)^mybeta)^(a-1))*(mytheta/t)^(myalpha*mybeta+1)*exp(-(mytheta/t)^mybeta-(c*(igamma(myalpha,(mytheta/t)^mybeta)/gamma(myalpha)))))/(mytheta*gamma(myalpha)^(a+b-1)*(gamma(myalpha)-igamma(myalpha,(mytheta/t)^mybeta))^(1-b))'.
Error in mle (line 245)
phat = mlecustom(data,varargin{:});
Caused by:
Not enough input arguments.
I tried to look into the error lines but I can't figure out where the error actually is.
Which function lacks fewer inputs? Is it referring to fun? Why would mle lack fewer inputs when it is trying to estimate the parameters?
Could someone kindly help me debug the error?
Thanks in advance.
exp() is a function, not a variable, precise the argument
exp^(-c*w) ---> exp(-c*w)
The starting point concerns the 6 parameters, not only one
0.1*ones(1,6)
In custcdf mle requires the upper bound of the integral to be a
scalar, I did some trial and error and the range is [2~9]. for the
trial some values lead to negative cdf or less than 1 discard them.
Then use the right one to compute the upper bound see if it's the
same as the one you predefined.
I re-write all the functions, check them out
The code is as follow
Censored = ones(5,1);% All data could be trusted
data = rand(5,1); % Since I cannot upload the acutal data, we may use this
f = #(w,a,b,c) (w.^(a-1)).*((1-w).^(b-1)).*exp(-c.*w);
% to estimate the parameters
custpdf = #(t,alpha,theta,beta, a,b,c)...
(((integral(#(w)f(w,a,b,c), 0,1)).^-1).*...
beta.*...
((igamma(alpha, (theta./t).^beta)).^(a-1)).*...
((theta./t).^(alpha.*beta + 1 )).*...
exp(-(((theta./t).^beta)+...
c.*igamma(alpha, (theta./t).^beta)./gamma(alpha))))./...
(theta.*...
((gamma(alpha)).^(a+b-1)).*...
((gamma(alpha)-...
igamma(alpha, (theta./t).^beta)).^(1-b)));
custcdf = #(t,alpha,theta,beta, a,b,c)...
((integral(#(w)f(w,a,b,c), 0,1)).^-1).*...
(integral(#(w)f(w,a,b,c), 0,2));
phat = mle(data,'pdf',custpdf,'cdf',custcdf,'start', 0.1.*ones(1,6),'Censoring',Censored);
Result
phat = 0.1017 0.1223 0.1153 0.1493 -0.0377 0.0902
I am trying to find the first term 'p' of a geometric series with common ratio 1.05 in MATLAB as follows. However the solve function is giving the error as below (posted right after the code). I can't seem to figure out the reason for this error, because when I display the expression for 'sum', it is correctly showing an expression in terms of'p', but the problem arises when I try to equate that to a value, and solve for 'p'. Any insights would be appreciated! Thanks.
clear all;
clc;
t=20; %no. of terms in geometric series
sum =0;
jackpot = 1000; %sum of geometric series
%p is first term
syms p
for x=1:t
sum = sum + p*((1.05)^(x-1));
end
disp(sum);
eqn1 = sum == jackpot;
solve(eqn1,p);
Output:
(18614477322052275759*p)/562949953421312000
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169
vc = char(v);
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Error in ==> geometric_trial at 13
solve(eqn1,p);
So, I got the answer for this from a user Walter Roberson on another forum. Posting here, from his answer.
I was trying this on a version of MATLAB which is really old i.e. R2010a. In this, using 'symbolic_expression == symbolic_expression' does not set up an equation for later solving, but instead compares the two expressions for literal equality and returns a logical value immediately.
In versions that old, the easiest fix is to change
eqn1 = sum == jackpot
to
eqn1 = (sum) - (jackpot)
and let solve() deal with the implicit equality to 0.
I am trying to calculate the percentage of sun radiation that is in the visible spectrum.
My code is:
h=6.626e-34; %planck constant
c=3e8; %speed of light
k=1.38066e-23; %boltzman constant
T1=5777; %temperature in surface of sun
syms f
J1 =(2*pi*h/(c^2))*((f^3)/(exp((h*f)/(k*T1))-1)); %planck radiation law
hold on;
ezplot(J1,[0,1.5e15])
a=int(J1,f,0,inf) %total energy radiated
b=int(J1,f,4e14,8e14) %energy between 400-800Thz (visible radiation spectrum)
Vp = (b/a)*100 %percentage of visible/total radiation
While the plot is exactly as it was expected to be, meaning i haven't totally messed it up, the result of the integrals is like this:
Vp = -1888568826285205004703258735621345426367059580820393216707788800000000000*((73396718487075910602267519716779133887030184268951416015625*log(1 - exp(23642358029674224853515625/7114894705749824515342336)))/205953985278202888163058116890711980917418253877248 - (73396718487075910602267519716779133887030184268951416015625*log(1 - exp(23642358029674224853515625/3557447352874912257671168)))/25744248159775361020382264611338997614677281734656 - (2390487322005187985890576650155251369405251302897930145263671875*polylog(2, exp(23642358029674224853515625/3557447352874912257671168)))/1857466834100924357302864708366291649120175241251782656 + (25952248522181378144831874533777514511468878135769288445192954296875*polylog(3, exp(23642358029674224853515625/3557447352874912257671168)))/67008813354583054015095330308295397033299967000474002915328 - (110058495767576691259417256590823904271799518678985866399923893187011219092083*polylog(4, exp(23642358029674224853515625/3557447352874912257671168)))/1888568826285205004703258735621345426367059580820393216707788800000000 + (2390487322005187985890576650155251369405251302897930145263671875*polylog(2, exp(23642358029674224853515625/7114894705749824515342336)))/7429867336403697429211458833465166596480700965007130624 - (25952248522181378144831874533777514511468878135769288445192954296875*polylog(3, exp(23642358029674224853515625/7114894705749824515342336)))/134017626709166108030190660616590794066599934000948005830656 + (110058495767576691259417256590823904271799518678985866399923893187011219092083*polylog(4, exp(23642358029674224853515625/7114894705749824515342336)))/1888568826285205004703258735621345426367059580820393216707788800000000 + 101409666798338314597227594049400067888200283050537109375/22835963083295358096932575511191922182123945984))/(12228721751952965695490806287869322696866613186553985155547099243001246565787*pi^4)
It is just a numerical expression (does not contain any constants) but i was expecting (and I am trying to find) a single value.
Any ideas how to overcome this?
Thanks in advance
double(Vp)
returns 44.3072 which is exactly what I was looking for while
vpa(Vp)
returns 44.307238264260285485868531074049 + 1.5008384323384489567473242679822e-35*i which is overcomed by
norm(vpa(Vp))
giving the same result as double(Vp)
However when i added a few more lines of code:
d=int(J1,f,0,4e14); %infrared energy
e=int(J1,f,8e14,inf); %ultraviolet energy
Ir = (d/a)*100; %percentage of infrared radiation
Uv = (e/a)*100; %percentage of ultraviolet radiation
double(Ir) gives this error:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in sym/double (line 514)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in symplanckradlaw (line 21)
Infrared=double(Ir)
While vpa(Ir) and even norm(vpa(Ir)) are given complicated complex numerical expressions like this
(abs(7.3340024900713941224341547032249e-56*limit((652255981594442743874468745505068648842285814001516463259648*f^2*polylog(2, exp((3873563939581825*f)/466281739436020499437475332096)))/15004497594028668398955870330625 - (608270107310811468411217054651916403272252179121058584901915330886243977872955782952124416*f*polylog(3, exp((3873563939581825*f)/466281739436020499437475332096)))/58120880811771703455030054556291506586990890625 - f^4/4 + (466281739436020499437475332096*f^3*log(1 - exp((3873563939581825*f)/466281739436020499437475332096)))/3873563939581825 + (283625243683820020974472587101002022201492492463545426687503953676410023626686775978867575742611811818507908158310055936*polylog(4, exp((3873563939581825*f)/466281739436020499437475332096)))/225134948049212098682315198853176286979563186266469146812890625, f == 0, Right) - 146.24549829953781858190522266202 - 2.501397387230748261245540446637e-36*i)^2)^(1/2)
You can convert it to a double using, well: double:
Vp_double = double(Vp)
You can also use vpa if you want to choose the precision:
Vp_vpa = vpa(Vp)
The reason why you get that immensely long expression is because MATLAB managed to find a "closed form definite integral". I.e. MATLAB managed to find an expression that accurately calculate the integral with no rounding errors. This is not always possible, and you'll get an error:
Warning: Explicit integral could not be found.
If that happens then you should try the approach given in this answer.
For some reason Matlab while was able to evaluate the limits in this integral
a=int(J1,f,0,inf)
it had problems evaluating limit to 0 in this integral
d=int(J1,f,0,4e14);
and limit to infinite in this integral
e=int(J1,f,8e14,inf);
I worked around this by putting instead of 0 a very low value (1e-45) and instead of infinite a very large one (1e22).
I got very good results for my purposes but I still find it really strange that matlab can evaluate the limits in one case but cannot evaluate the same limits in another case.
I am getting error in executing the code below for calculating one simple integral in matlab. The code is given below.
%Parameter Innitizlation
epsilon0=8.85*10^-12;
d_mos=6*10^-9;
epsilon_mos=5*epsilon0;
d_g=30*10^-9;
epsilon_g=19*epsilon0;
vt=-2;
e=1.6*10^-19;
n=[];
i=1;
t2=[];
u=60; % cm^2/v*S
h=1.05*10^-34; % ev*s
%Capacitor Calaculation
c_g=(epsilon_g/d_g);
c_mos=(epsilon_mos/d_mos);
c_t=1/((1/c_g)+(1/c_mos));
%Input Voltage
t = 0:0.2:10;
vg = 5*sin(t);
%Surface Voltage
fun=1-(c_t/c_g);
vs = integral(fun,0,vg);
figure
plot(t,vs)
title('vs vs time')
Error
The error I am getting is
"??? Undefined function or method 'integral' for input arguments of type 'double'.
Error in ==> MIT at 29
vs = integral(fun,0,vg);""
Reading your title again it sounds like you want to integrate the constant from 0 to different bounds. So there were two things wrong:
You're function wasn't a function just a double, to fix this change make fun an anonymous function like this
fun=#(t) (1-(c_t/c_g))*ones(size(t));
The ones(size(t)) just makes it so that there is an output for each input.
You need to have a single upper and lower bound for integral, to fix this stick your integral in an arrayfun, like this:
vs = arrayfun(#(xu) integral(fun,0,xu), vg);
For more information on arrayfun.
With the entire code here
epsilon0=8.85*10^-12;
d_mos=6*10^-9;
epsilon_mos=5*epsilon0;
d_g=30*10^-9;
epsilon_g=19*epsilon0;
vt=-2;
e=1.6*10^-19;
n=[];
i=1;
t2=[];
u=60; % cm^2/v*S
h=1.05*10^-34; % ev*s
%Capacitor Calaculation
c_g=(epsilon_g/d_g);
c_mos=(epsilon_mos/d_mos);
c_t=1/((1/c_g)+(1/c_mos));
%Input Voltage
t = 0:0.2:10;
vg = 5*sin(t);
%Surface Voltage
fun=#(t) (1-(c_t/c_g))*ones(size(t));
vs = arrayfun(#(xu) integral(fun,0,xu), vg);
figure
plot(t,vs)
title('vs vs time')
I am trying to use Octave's fminsearch function, which I have used in MATLAB before. The function seems not sufficiently documented (for me at least), and I have no idea how to set to options such that it would actually minimize.
I tried fitting a very simple exponential function using the code at the end of this message. I want the following:
I want the function to take as input the x- and y-values, just like MATLAB would do. Furthermore, I want some control over the options, to make sure that it actually minimizes (i.e. to a minimum!).
Of course, in the end I want to fit functions that are more complicated than exponential, but I want to be able to fit exponentials at least.
I have several problems with fminsearch:
I tried handing over the x- and y-value to the function, but a matlab-style thing like this:
[xx,fval]=fminsearch(#exponential,[1000 1],x,y);
or
[xx,fval]=fminsearch(#exponential,[33000 1],options,x,y)
produces errors:
error: options(6) does not correspond to known algorithm
error: called from:
error: /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 72, column 16
error: /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4
Or, respectively (for the second case above):
error: `x' undefined near line 4 column 3
error: called from:
error: /Users/paul/exponential.m at line 4, column 2
error: /opt/local/share/octave/packages/optim-1.0.6/nmsmax.m at line 63, column 6
error: /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 77, column 9
error: /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4
Apparently, the order of arguments that fminsearch takes is different from the one in MATLAB. So, how is this order??
How can I make fminsearch take values and options?
I found a workaround to the problem that the function would not take values: I defined the x- and y values as global. Not elegant, but at least then the values are available in the function.
Nonetheless, fminsearch does not minimize properly.
This is shown below:
Here is the function:
function f=exponential(coeff)
global x
global y
X=x;
Y=y;
a= coeff(1);
b= coeff(2);
Y_fun = a .* exp(-X.*b);
DIFF = Y_fun - Y;
SQ_DIFF = DIFF.^2;
f=sum(SQ_DIFF);
end
Here is the code:
global x
global y
x=[0:1:200];
y=4930*exp(-0.0454*x);
options(10)=10000000;
[cc,fval]=fminsearch(#exponential,[5000 0.01])
This is the output:
cc =
4930.0 5184.6
fval = 2.5571e+08
Why does fminsearch not find the solution?
There is an fminsearch implementation in the octave-forge package "optim".
You can see in its implementation file that the third parameter is always an options vector, the fourth is always a grad vector, so your ,x,y invocations will not work.
You can also see in the implementation that it calls an fmins implementation.
The documentation of that fmins implementation states:
if options(6)==0 && options(5)==0 - regular simplex
if options(6)==0 && options(5)==1 - right-angled simplex
Comment: the default is set to "right-angled simplex".
this works better for me on a broad range of problems,
although the default in nmsmax is "regular simplex"
A recent problem of mine would solve fine with matlab's fminsearch, but not with this octave-forge implementation. I had to specify an options vector [0 1e-3 0 0 0 0] to have it use a regular simplex instead of a 'right-angled simplex'. The octave default makes no sense if your coefficients differ vastly in scale.
The optimization function fminsearch will always try to find a minimum, no matter what the options are. So if you are finding it's not finding a minimum, it's because it failed to do so.
From the code you provide, I cannot determine what goes wrong. The solution with the globals should work, and indeed does work over here, so something else on your side must be going awry. (NOTE: I do use MATLAB, not Octave, so those two functions could be slightly different...)
Anyway, why not do it like this?
function f = exponential(coeff)
x = 0:1:200;
y = 4930*exp(-0.0454*x);
a = coeff(1);
b = coeff(2);
Y_fun = a .* exp(-x.*b);
f = sum((Y_fun-y).^2);
end
Or, if you must pass x and y as external parameters,
x = [0:1:200];
y = 4930*exp(-0.0454*x);
[cc,fval] = fminsearch(#(c)exponential(c,x,y),[5000 0.01])
function f = exponential(coeff,x,y)
a = coeff(1);
b = coeff(2);
Y_fun = a .* exp(-x.*b);
f = sum((Y_fun-y).^2);
end