Problems in HMM toolbox - matlab

Recently I'm doing some training of HMM, I used the HMM toolbox. But I have some problems and couldn't resolve them.
I train my hmm as shown below. There's no problems here.
[LL, prior1, transmatrix1, observematrix1] = dhmm_em(data, prior0, transmatrix0, observematrix0);
I use the Viterbi algorithm to find the most-probable path through the HMM state trellis.
function path = viterbi_path(prior, transmat, obslik);
Now there's a problem. I don't know what the "obslik" means. Is it the observematrix1?
I want to get the probability of a sequence, but I don't know whether I should use the "fwdback" function or not. If I should, what the "obslik" means then?
function [alpha, beta, gamma, loglik, xi_summed, gamma2] = fwdback(init_state_distrib, transmat, obslik, varargin);
Thanks!!!

I didn't understand the comments. Now I understand it.
The "obslik" here isn't equal to the observematrix1. Before using Viterbi_path function, we should compute the obslik:
obslik = multinomial_prob(data(m,:), observematrix1);
the data matrix is the observematrix0, observe-matrix before training.
Am I right?

Related

how to transform output SVM into probability using 'Platt scaling' method?

I worked on the problem of handwritten recognition images. For this, I use support vector machines as a classifier . the matrix score shows an example of the scores returned by svm for 5 samples. the number of classes is also 5. I want to transform this matrix into probabilities.
score=[ 0,2590 -0,6033 -1,1350 -1,2347 -0,9776
-1,4727 -0,2136 -0,9649 0,1480 -1,4761
-0,9637 -0,8662 0,0674 -1,0051 -1,1293
-2,1230 -0,8805 -0,9808 -0,0520 -0,0836
-1,6976 -1,1578 -0,9205 -1,1101 1,0796]
According to research on existing methods, I found that the Platt's scaling method is most appropriate in my case. I found an implementation of this method on this link Platt scaling but the problem is that I don't understand the third parameter to enter. Please, help me to understand this implementation and to make it executable
I await your answers and thank you in advance

LIBSVM in MATLAB/Octave - what's the output of libsvmread?

The second output of the libsvmread command is a set of features for each given training example.
For example, in the following MATLAB command:
[heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
This second variable (heart_scale_inst) holds content in a form that I don't understand, for example:
<1, 1> -> 0.70833
What is the meaning of it? How is it to be used (I can't plot it, the way it is)?
PS. If anyone could please recommend a good LIBSVM tutorial, I'd appreciate it. I haven't found anything useful and the README file isn't very clear... Thanks.
The definitive tutorial for LIBSVM for beginners is called: A Practical Guide to SVM Classification it is available from the site of the authors of LIBSVM.
The second parameter returned is called the instance matrix. It is a matrix, let call it M, M(1,:) are the features of data point 1 and so on. The matrix is sparse that is why it prints out weirdly. If you want to see it fully print full(M).
[heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
with heart_scale_label and heart_scale_inst you should be able to train an SVM by issuing:
mod = svmtrain(heart_scale_label,heart_scale_inst,'-c 1 -t 0');
I strong suggest you read the above linked guide to learn how to set the c parameter (and possibly, in case of RBF kernel the gamma parameter), but the above line is how you would train with that data.
I think it is the probability with which test case has been predicted to heart_scale label category

One sample Kolmogorov-Smirnov to test gof of theoretical distribtion (makedist error) matlab

I have few continuous variables that look like this:
durs1=[3,40933 0,033630 0,25103 0,6361 0,71971 1,18311 1,91946 0,12842 0,97639 1,1383 0,46871 3,05241 2,34907 1,03788 0,76434 1,08798 1,462 0,4241 2,32128 0,29017..]
Each has more than 1000 values (all positive). I used
[a, b]=gamfit(durs1)
a =
2.3812 0.4200
b =
2.2316 0.3907
2.5408 0.4514
to find parameters of gamma distribution. Now I want to make a goodness of fit test in order to see how well the model fits my data. Matlab provides the one sample Kolmogorov-Smirnov test to solve the problem (http://www.mathworks.com/help/stats/kstest.html#btnyrvz-1)
But when I run my code (based on their examples):
test_cdf=makedist('Gamma','a',2.38,'b',0.42)
[h, p]=kstest(durs1,'CDF',test_cdf)
I have this error: "Undefined function 'makedist' for input arguments of type 'char'."
Can somebody help me to fix my code?
It seems like the function makedist of the statistics toolbox is available only from Matlab version r2013a. Looking in the documentation of earlier versions, even as late as r2012b, there is no mention of makedist. So I think updating to the latest version of matlab should solve your problem.

using options in lsqcurvefit

I set my options to
options=optimset('LevenbergMarquardt', 'on')
and then employ lsqcurvefit like below,
[x,resnorm,residual,exitflag,output] = lsqcurvefit(#myfun, [0.01 0.3], xdata, ydata, [-inf -inf], [inf inf], options)
but the problem is that I don't now why I will get for output :
output =
firstorderopt: 3.4390e-07
iterations: 4
funcCount: 15
cgiterations: 0
algorithm: 'large-scale: trust-region reflective Newton'
message: [1x425 char]
Does this mean Matlab did not use the algorithm Levenberg Marquardt?
But I did set my options to levenberg Marquardt algorithm!!!
I'd appreciate any help.
Sometimes a specific algorithm is not suitable for a specific configuration of an optimization problem. In these cases Matlab "falls back" to its default optimization algorithm.
It might be the case that for your specific problem/configuration Matlab is unable to use Levenberg-Marquardt algorithm.
Read the docs carefully to see if this is the case.
I can't say for certain, but the constaints ([-inf -inf], [inf inf]) could be your problem. The documentation for lsqcurvefit strictly says the LMA cannot be used for constrained problems. If constraints are included, it will fall back to trust-region.
Yes, your constraints are mathematically equivalent to 'no constaints', but I have no idea how the MATLAB function itself will interpret those. I tried to recreate the problem on my end, but optimset('LevenbergMarquardt', 'on') has been deprecated and generates an error (implying you have a relatively old version). Even when using the new syntax (optimset('Algorithm', 'levenberg-marquardt')), it behaves correctly on my end (using 2011b). To not have constraints, the correct approach is to use empty matrices (i.e. []).
Yes, the question is a month old, but someone else may find the answer useful.

Using coupled system of PDEs in modelica

Just few questions, i hope someone will find time to answer :).
What if we have COUPLED model example: system of n indepedent variables X and n nonlinear partial differential equations PDEf(X,PDEf(X)) with respect to TIME that depends of X,PDEf(X)(partial differential equation depending of variables X ). Can you give some advice? Here is one example:
Let’s say that c is output, or desired variable. Let’s say that r is independent variable.Partial differential equation looks like:
∂c/∂t=D*1/r+∂c/∂r+2(D* (∂^2 c)/(∂r^2 ))
D=constant
r=0:0.1:Rp- Matlab syntaxis, how to represent same in Modelica (I use integrator,but didn't work)?
Here is a code (does not work):
model PDEtest
/* Boundary conditions
1. delta(c)/delta(r)=0 for r=0
2. delta(c)/delta(r)=-j*d for r=Rp*/
parameter Real Rp=88*1e-3; // length
parameter Real initialConc=1000;
parameter Real Dp=1e-14;
parameter Integer np=10; // num. of points
Real cp[np](start=fill(initialConc,np));
Modelica.Blocks.Continuous.Integrator r(k=1); // independent x1
Real j;
protected
parameter Real dr=Rp/np;
parameter Real ts= 0.01; // for using when loop (sample(0,ts) )
algorithm
j:=sin(time); // this should be indepedent variable like x2
r.u:=dr;
while r.y<=Rp loop
for i in 2:np-1 loop
der(cp[i]):=2*Dp/r.y+(cp[i]-cp[i-1])/dr+2*(Dp*(cp[i+1]-2*cp[i]+cp[i-1])/dr^2);
end for;
if r.y==Rp then
cp[np]:=-j*Dp;
end if;
cp[1]:=if time >=0 then initialConc else initialConc;
end while;
annotation (uses(Modelica(version="3.2")));
end PDEtest;
Here are more questions:
This code don’t work in OpenModelica 1.8.1, also don’t work in Dymola 2013demo. How can we have continuos function of variable c, not array of functions ?
Can we place values of array cp in combiTable? And how?
If instead “algorithm” stay “equation” code can’t be succesfull checked.Why? In OpenModelica, error is :could not flattening model :S.
Is there any simplified way to use a set of equation (PDE’s) that are coupled? I know for PDEs library in Modelica, but I think they are complicated. I want to write a function for solving PDE and call these function in “main model”, so that output of function be continuos function of “c”.I don’t know what for doing with array of functions.
Can you give me advice how to understand Modelica language, if we “speak” like in Matlab? For example: Values of independent variable r,we can specife in Matlab, like r=0:TimeStep:Rp…How to do same in Modelica? And please explain me how section “equation” works, is there similarity with Matlab, and is there necessary sequancial approach?
Cheers :)
It's hard to answer your question, since you assuming that Modelica ~ Matlab, but that's not the case. So I won't comment your code, since it's really wrong. Let me give you an example model to the burger equation. Maybe you could use it as starting point.
model burgereqn
Real u[N+2](start=u0);
parameter Real h = 1/(N+1);
parameter Integer N = 10;
parameter Real v = 234;
parameter Real Pi = 3.14159265358979;
parameter Real u0[N+2]={((sin(2*Pi*x[i]))+0.5*sin(Pi*x[i])) for i in 1:N+2};
parameter Real x[N+2] = { h*i for i in 1:N+2};
equation
der(u[1]) = 0;
for i in 2:N+1 loop
der(u[i]) = - ((u[i+1]^2-u[i-1]^2)/(4*(x[i+1]-x[i-1])))
+ (v/(x[i+1]-x[i-1])^2)*(u[i+1]-2*u[i]+u[i+1]);
end for;
der(u[N+2]) = 0;
end burgereqn;
Your further questions:
cp is an continuous variable and the array is representing
every discretization point.
Why you should want to do that, as far as I understand cp is
your desired solution variable.
You should try to use almost always equation section
algorithm sections are usually used in functions. I'm pretty
sure you can represent your desire behaviour with equations.
I don't know that library, but the hard thing on a pde is the
discretization and the solving it self. You may run into issues
while solving the pde with a modelica tool, since usually
a Modelica tool has no specialized solving algorithm for pdes.
Please consider for that question further references. You could
start with Modelica.org.