function Est=Estimate(trajec, dimHmm, previterbi, steps, G_SDE)

%% Estimates the input trajectorie $trajec with the HMM Variants class of
%% the Metamacs Package (change javaclasspath to your metamacs lib)

% * Eike Meeerbach et al.: Sequential change point detection in molecular dynamics trajectories, 
% * submitted to the Journal of multivariate Analysis (2008). 

% $dimHmm is the number of States, 
% §steps is the aboirt criterioin for the EM algorithm


javaclasspath({
   '/home/cocktail/luedge/NetBeansProjects/Metamacs/lib/colt.jar'...
   '/home/cocktail/luedge/NetBeansProjects/Metamacs/lib/concurrent.jar'...
   '/home/cocktail/luedge/NetBeansProjects/Metamacs/netbeans_project/build/classes/'...
   '/home/cocktail/luedge/NetBeansProjects/Metamacs/netbeans_project/dist',...

 });


import biocomp.metamacs.hmm.newImplementation.HMMVAR;
precision=1.0000e-04;

%% -------- Create random Viterbipath if not given (poor results)
%
if  previterbi==0;
previterbi=randViterbi(dimHmm,size(trajec,1));
Est.preVit=previterbi;      
else
end

%% -       Estimate Maximum Likelihood of HMMSDE - Model with HMMVAR( , , 1, )   -
hmm=HMMVAR(trajec, dimHmm, G_SDE, []); 
hmm.initializeModel(previterbi)
Est.hmm=hmm;
% Est.MLH = hmm.EMcorrect(steps, precision, true);
Est.MLH = hmm.EM(steps, precision, true);

%% --------------------Get-estimated parameter------------
Est.TransitionMatrix=hmm.getTransitionMatrix();
% Est.ObsProb = hmm.getObservationProbabilities();
Est.FwBw = hmm.getForwardBackwardVariables();

CovM=hmm.getCovariances();
Phi=hmm.getRegressionMatrices;

Est.Phi=Phi;
Est.RegM = squeeze(Phi(1,:,:));
if G_SDE==1
for i=1:size(Phi,1)
Cov(i)=squeeze(CovM(i,:,:)); %#ok<AGROW>
S(:,i)=squeeze(Est.Phi(i,:,:)); %#ok<AGROW>
mu(i)=S(1,i)/(1-S(2,i)); %#ok<AGROW>
% muM=inv(eye(size(S))-S)*Phi(1,:);
end
Est.expTauF=S;
Est.mu=mu;
Est.Cov=Cov;
else

% Est.muM=muM;
Est.CovM=CovM;


end

%% -       final Viterbi-path as initial distribution        -
Est.viterbi=hmm.computeViterbiPath(); 
Est.InitDist= hmm.getInitialDistribution();

