# Examples for versions 0.3.0 and 0.3.1

## August 2014

Initially, make sure to include all directories into the MATLAB classpath, for example using the command:
` addpath(genpath('INSTALLATION_PATH_HERE/kpc-toolbox-0.3.0')) `

or
` addpath(genpath('INSTALLATION_PATH_HERE/kpc-toolbox-0.3.1')) `

depending on what KPC-Toolbox version you are using.
A demonstrator of the tool can be run using the command
```
demo_run
```

Help and examples on fitting tool can be accessed by typing
```
help kpcfit_auto
```

for the Markovian arrival process fitting tool and
```
help kpcfit_ph_auto
```

for the phase-type distribution fitting tool. Note that the syntax and options have changed with respect to version 0.2.4, especially for kpcfit_ph_auto.
**Note**: if you experience an error due to the lack of the smooth function, this is because you have not installed MATLAB's Curve Fitting toolbox. You can simply remove the function from the toolbox code and it should work fine.

# Examples for version 0.2.4

## 1. PH distribution fitting example

Download the BC-pAug89-trace.mat into the KPC-Toolbox directory. The first 6 moments of the trace (E[X], E[X^2], ...) can be fitted into a PH(4) distribution by typing at the MATLAB prompt the following commands:
```
load BC-pAug89-trace.mat;
E=[]; for k=1:6 E(k)=mean(S.^k); end;
PH = kpcfit_ph_auto(E,'MinOrdMom',6,'MinNumStates',4,
'MaxNumStates',4);
```

The fitted moments of the PH distribution are then computed as follows
```
Eph=[]; for k=1:6 Eph(k)=map_moment(PH,k); end; Eph
```

The fitted cumulative distribution function (CDF) of the PH distribution is computed by
```
[F,X]=ecdf(S); X=X(1:10:end); F=F(1:10:end); Fph=map_cdf(PH,X');
figure; semilogx(X,F); hold all; semilogx(X,Fph);
legend('trace','PH');
```

The tail of the PH distribution is computed as follows
```
[F,X]=ecdf(S); X=X(1:10:end); F=F(1:10:end); Fph=map_cdf(PH,X');
figure; loglog(X,1-F); hold all; loglog(X,1-Fph);
legend('trace','PH');
```

## 2. MAP process fitting example

Download the BC-pAug89-trace.mat into the KPC-Toolbox directory. Then fit the time series by a MAP as follows
```
load BC-pAug89-trace.mat;
trace = kpcfit_init(S);
MAP = kpcfit_auto(trace,'OnlyAC',1);
```

For a more accurate, but slower, fitting that accounts for third-order statistics of the trace (bicorrelations/bispectrum), remove the 'OnlyAC' option
```
load BC-pAug89-trace.mat;
trace = kpcfit_init(S);
MAP = kpcfit_auto(trace);
```

Moments, CDF, and tail of the distribution are computed similarly to the PH case
```
Emap=[]; for k=1:6 Emap(k)=map_moment(MAP,k); end; Emap
[F,X]=ecdf(S); X=X(1:10:end); F=F(1:10:end); Fmap=map_cdf(MAP,X');
figure; semilogx(X,F); hold all; semilogx(X,Fmap); legend('trace','MAP');
[F,X]=ecdf(S); X=X(1:10:end); F=F(1:10:end); Fmap=map_cdf(MAP,X');
figure; loglog(X,1-F); hold all; loglog(X,1-Fmap); legend('trace','MAP');
```

Note that MAP fitting focuses on the first 3 moments only, so it is not meant to produce high-quality fitting of the distribution as the PH distribution fitting above. However, MAPs capture also the ordering of the trace, as can be seen from plotting the autocorrelation function
```
ACtrace = trace_acf(S,1:100); ACmap = map_acf(MAP,1:100);
figure; plot(1:100,ACtrace); hold all; plot(1:100,ACmap);
legend('trace','MAP');
```

Note that MAP fitting is harder than distribution fitting. Therefore, you may need a few runs before obtaining good results because the algorithm for MAP fitting is a nonlinear optimization that does not return the same result each time it is run. It is possible to increase the number of runs to be executed by the KPC-Toolbox by typing
```
load BC-pAug89-trace.mat; trace = kpcfit_init(S);
MAP = kpcfit_auto(trace,'OnlyAC',1,'MaxRunsAC',500,'MaxRunsBC',50);
```

where 'MaxRunsAC' controls fitting of second-order properties, while 'MaxRunsBC' controls third-order properties.