Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation and demos #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
452 changes: 452 additions & 0 deletions macros/adf_case1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
225 changes: 225 additions & 0 deletions macros/adf_case2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
219 changes: 219 additions & 0 deletions macros/adf_case3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 136 additions & 0 deletions macros/arch_fit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# arch_fit
## Calling Sequence

- ` [a, b] = arch_fit (y, x, p) `
- ` [a, b] = arch_fit (y, x, p, iter, gamma, a0, b0) `

## Description
Fit an ARCH regression model to the time series y using the scoring algorithm in Engle’s original ARCH paper.

The model is

y(t) = b(1) * x(t,1) + … + b(k) * x(t,k) + e(t),
h(t) = a(1) + a(2) * e(t-1)^2 + … + a(p+1) * e(t-p)^2
in which e(t) is N(0, h(t)), given a time-series vector y up to time t-1 and a matrix of (ordinary) regressors x up to t. The order of the regression of the residual variance is specified by p.

If invoked as arch_fit (y, k, p) with a positive integer k, fit an ARCH(k, p) process, i.e., do the above with the t-th row of x given by

[1, y(t-1), …, y(t-k)]
Optionally, one can specify the number of iterations iter, the updating factor gamma, and initial values a0 and b0 for the scoring algorithm.
## Parameters
- `y`(vector) : A time-series data vector up to time t-1 .
- `x` (Matrix): A matrix of (ordinary) regressors x up to t.
- `p` (scalar): The order of the regression of the residual variance.
- `iter` (scaler) : Number of iterations
- `gamma` (real number) : updating factor
- `a0 b0` (real numbers) : Initial values for the scoring algorithm
## Dependencies:
ols autoreg_matrix


## Examples
1.
```scilab
t = linspace(0,10,1000);

y = cos(t);

x = sin(t);

[a,b] = arch_fit(y,x',1)

```
```output
pval =

0.0175825
0.9357096
lm =

0.0242719

```

2.
```scilab
t = linspace(0,10,1000);
y = sin(t);
x = [ zeros(1,300) ones(1,400) zeros(1,300);zeros(1,200) ones(1,200) zeros(1,600);zeros(1,100) ones(1,400) zeros(1,500)];
[a,b] = arch_fit(y,x',3)
```
```output
a =

0.0020139
1.0353154
-0.0009762
-0.0334181
b =

-0.7849689
0.2267450
0.3037736
```
3.
```scilab
t = linspace(-10,10,2000);
y = exp(t);
x = [sin(t);cos(t);tan(t)];
[a,b] = arch_fit(y,x',5)

```
```output
a =

69311.100
0.8883086
0.3352075
0.1473639
-0.0385322
-0.3485546
b =

334.06505
-1464.5987
-0.5365645
```
4.
```scilab
t = linspace(-10,10,2000);
y = exp(t);
x = [sin(t);cos(t);tan(t)];
[a,b] = arch_fit(y,x',5,1000,0.32,[1;0;1;0;1;0],[1;0;1]);
a,b
```
```output
a =

0.0001950
1.1362016
-0.1143480
0.0180131
-0.0284946
0.0200169
b =

1.
-6.465D-22
1.

```
5.
```scilab

t= linspace(0,100,100);

y = t .* t + 2*t + 3;

x = 3;

[a,b] = arch_fit(y,x,1,1000,0.2,[1 ;1],[0; 0 ;0 ;0])
```
```output
//Octave will return Nan vectors which means no solution

inv: Problem is singular. // implies no solution
```
131 changes: 131 additions & 0 deletions macros/arch_test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# arch_test
## Description
Perform a Lagrange Multiplier (LM) test for conditional heteroscedasticity.

For a linear regression model

y = x * b + e
perform a Lagrange Multiplier (LM) test of the null hypothesis of no conditional heteroscedascity against the alternative of CH(p).

I.e., the model is

y(t) = b(1) * x(t,1) + … + b(k) * x(t,k) + e(t),
given y up to t-1 and x up to t, e(t) is N(0, h(t)) with

h(t) = v + a(1) * e(t-1)^2 + … + a(p) * e(t-p)^2,
and the null is a(1) == … == a(p) == 0.

If the second argument is a scalar integer, k, perform the same test in a linear autoregression model of order k, i.e., with

[1, y(t-1), …, y(t-k)]
as the t-th row of x.

Under the null, LM approximately has a chisquare distribution with p degrees of freedom and pval is the p-value (1 minus the CDF of this distribution at LM) of the test.

If no output argument is given, the p-value is displayed.
## Calling Sequence

- ` [pval, lm] = arch_test (y, x, p) `


## Parameters
- `y`: Array-like. Dependent variable of the regression model.
- `x`: Array-like. Independent variables of the regression model.
If x is a scalar integer k, it represents the order of autoregression.
- `p` : Integer. Number of lagged squared residuals to include in the heteroscedasticity model.


Returns:
- `pval`: Float. p-value of the LM test.
- `lm`: Float. Lagrange Multiplier test statistic.


## Dependencies:
ols , autoreg_matrix

## Examples
1.
```scilab
t = linspace(0,10,1000);

y = cos(t);

x = sin(t);

[pval,lm] = arch_test(y,x',1)

```
```output
pval = 0
lm = 229.01679
```

2.
```scilab
t = linspace(0,10,1000);
y = sin(t);
x = [ zeros(1,300) ones(1,400) zeros(1,300);zeros(1,200) ones(1,200) zeros(1,600);zeros(1,100) ones(1,400) zeros(1,500)];
[pval,lm] = arch_test(y,x',3)
```
```output
pval =

0.
lm =

447.99516

```
3.
```scilab
t = linspace(-10,10,2000);
y = exp(t);
x = [sin(t);cos(t);tan(t)];
[pval,lm] = arch_test(y,x',5)
```
```output
Warning :
matrix is close to singular or badly scaled. rcond = 6.6096E-17
pval =

0.
lm =

16935.914

```
4.
```scilab
t= linspace(0,100,100);

y = t .* t + 2*t + 3;

x = 3;

[pval,lm] = arch_test(y,x,1)

```
```output
pval =

0.
lm =

208.60268

```
5.
```scilab
t= linspace(0,10,1000);
x=[sin(t);cos(t);tan(t)];
[pval,lm] = arch_test(t,x',1)
```
```output
pval =

0.
lm =

345.23194

```
40 changes: 40 additions & 0 deletions macros/cohere.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# cohere
## Description
Estimate (mean square) coherence of signals "x" and "y".
Use the Welch (1967) periodogram/FFT method.
Compatible with Matlab R11 cohere and earlier.
See "help pwelch" for description of arguments, hints and references — especially hint (7) for Matlab R11 defaults.

## Calling Sequence
- ` [Pxx, freq] = cohere(x,y,Nfft,Fs,window,overlap,range,plot_type,detrend) `

## Dependencies:
pwelch
## Examples
1.
```scilab
subplot(2,2,1)
t = linspace(0,10,1000); x = sin(t) ; y = cos(t);
cohere(x,y)
subplot(2,2,2)
cohere(t,x,400)
subplot(2,2,3)
cohere(t,y,300,500)
subplot(2,2,4)
t = linspace(1,10,1000); x =sin(t);
y = filter(0.23,x,t);
cohere(x,y,500,100,6)
```
<img src="cohere_t_case1.svg">

2.
```scilab
subplot(2,2,1)
t = linspace(1,10,1000); x =cos(t);
y = filter(0.9999,x,t);
cohere(x,y,800,300,7,0.56)
subplot(2,2,2)
t = linspace(1,10,1000); x =filter(0.3245,cos(t),t); y = filter(0.0034,x,sin(t));
cohere(x,y,700,1000,4,0.67,"half")
```
<img src="cohere_t_case2.svg">
Loading