# Given two time series find some time-varying linear combinations that are stationary.

### Description

Find some time-varying linear combinations of two
time series that are stationary. The complexity of the
time-varying combinations is restricted by the `Ncoefs`

argument.

### Usage

1 2 3 4 5 6 7 | ```
findstysols(Nsims = 100, Ncoefs = 3, tsx, tsy, sf=100, plot.it = FALSE,
print.it=FALSE, verbose = FALSE, lctsfn=LCTS, prodcomb.fn=prodcomb,
filter.number=1, family=c("DaubExPhase", "DaubLeAsymm"),
my.maxit=500, spec.filter.number=1,
spec.family=c("DaubExPhase","DaubLeAsymm"),
optim.control=list(maxit=my.maxit, reltol=1e-6),
irng=rnorm, lapplyfn=lapply, Bsims=200, ...)
``` |

### Arguments

`Nsims` |
Number of searches attempted |

`Ncoefs` |
Number of Haar wavelet coefficients to use. Must be >= 1. Should only increase in steps of powers of two. E.g. can only supply the values 1, 3, 7, 15, etc. So, "1" means only one coarse scale coefficient (corresponds to piecewise constant with one centrally located jump), "3" means one coarse, and two next coarse scale coefficients (corresponds to piecewise constant with 4 equally sized piece with jumps at 1/4, 1/2 and 3/4), "7" means one coarse, two next coarse, four next coarse, and so on. |

`tsx` |
One of the time series |

`tsy` |
The other time series, values at the same time locations
as |

`sf` |
A scale factor to multiply both time series by (not really of much use) |

`plot.it` |
If |

`print.it` |
Not currently used in this function, reserved for future use |

`verbose` |
If |

`lctsfn` |
The function to compute the 'linear combination test of stationarity'. I.e. it is the function that combines the two series and returns the value of the test statistic on the combination. |

`prodcomb.fn` |
The function that can produce the linear combination of the two time series and return the combination, and optionally vectors containing the combination functions. |

`filter.number` |
Gets passed to |

`family` |
Gets passed to |

`my.maxit` |
Maximum number of iterations in the optimization. May need to be increased to, e.g. 1000 or 2000 for longer time series (e.g. T=2048) |

`spec.filter.number` |
Wavelet filter number. This argument
gets passed to the |

`spec.family` |
Same as |

`optim.control` |
Argument passed to the |

`irng` |
Random number generator used to generate coefficients for starting parameters for the linear combination of time series (actually wavelet coefficients of the combination functions) |

`lapplyfn` |
Function to use to process lists. If this argument
is |

`Bsims` |
The number of bootstrap simulations for the (single)
test of stationarity |

`...` |
Other arguments, passed to the |

### Details

The function searches for time-varying linear combinations of
two time series, `tsa`

and `tsy`

, such that the combination
is stationary (according to the `TOSts`

test statistic).

Each linear combination is parametrised by a coarse scale
Haar wavelet decomposition (controlled by `Ncoefs`

).
Initially, the Haar wavelet coefficients (up to a fixed finite scale,
controlled by `Ncoefs`

) are randomly chosen. These coefficients
are converted to functions *α_t, β_t*
by the `coeftofn`

function and then a linear combination
with the time series is formed out of those and the time series,
i.e.
*Z_t = α_t x_t + β_t y_t*
The non-stationarity of *Z_t* is measured using the
`TOSts`

test statistic and this value is minimized
over the coarse scale Haar wavelet coefficients.

This optimization procedure is repeated `Nsims`

times.
If the `lapplyfn`

is set to `mclapply`

then this
function from the `multicore`

package is used to process
the lists in parallel.

This function can be called multiple times (e.g. on different
processors in a multiprocessor environment. The result sets from
different runs can be combined using the
`mergexy`

function.

The variance `Ncoefs`

is very important, it controls the
complexity of the linear combinations. If it is too big the
linear combinations themselves can be extremely oscillatory and
stationarity is easy to obtain. Small values of `Ncoefs`

results in piecewise constant functions with fewer jumps.

The `Ncoefs`

value must take the value of
*2^k-1*. If this is the case the *k*
is the number of scale levels present in the Haar representation
of the combining function *α_t, β_t*
(excluding the scaling function coefficient, just the wavelet
coefficients from the coarsest scale).

The functions to compute the linear combination and also the
test statistic on that combination, and just to compute the
combination and return also (optionally) the combination
vectors are supplied in `lctsfn`

and `prodcomb.fn`

.
By default, these are just the `LCTS`

and `prodcomb`

functions. However, it is possible to recode these to
look at operating on combinations that operate on portfolios.
I.e. rather than look at linear combinations of log-returns
(which if `tsx`

and `tsy`

were) one can look at
linear combinations of actual series (ie portfolios) and
then look for stationarity of log-returns of the portfolios.
These functions will be made available in a later package.

### Value

An object of class `csFSS`

which is a
list with the following components.

`startpar` |
A matrix with |

`endpar` |
Same dimension as |

`convergence` |
A vector of length |

`minvar` |
A vector of length |

`pvals` |
A vector of length |

`tsx` |
The |

`tsy` |
The |

`tsxname` |
The name of the |

`tsyname` |
The name of the |

`filter.number` |
The filter number that was used |

`family` |
The wavelet family that was used |

`spec.filter.number` |
The filter number that was used |

`spec.family` |
The wavelet family that was used |

### Author(s)

Guy Nason

### References

Cardinali, A. and Nason, Guy P. (2013) Costationarity of
Locally Stationary Time Series Using costat.
*Journal of Statistical Software*, **55**, Issue 1.

Cardinali, A. and Nason, G.P. (2010) Costationarity of locally stationary
time series. *J. Time Series Econometrics*, **2**, Issue 2, Article 1.

### See Also

`LCTS`

, `BootTOS`

,`plotBS`

, `prodcomb`

, `COEFbothscale`

,
`LCTSres`

, `print.csFSS`

, `summary.csFSS`

,
`plot.csFSS`

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | ```
#
# Find some stationary solutions with \code{Ncoefs=3}.
#
# Note: this is a toy example
#
tsx1 <- rnorm(32) # A x time series
tsy1 <- rnorm(32) # A y time series
#
# Find costationary solutions, but only from 2 random starts
#
# Typically, the length of tsx and tsy would be bigger (eg sret, fret are
# other examples you might use). Also, Nsims would be bigger, you need
# to use many random starts to ensure good coverage of the solution
# space, e.g. Nsims=100
#
# Note: the following examples are not run so as to adhere to CRAN
# requirements for package execution timings
#
## Not run: ans <- findstysols(Nsims=3, tsx=tsx1, tsy=tsy1)
#
# Print out a summary of the results
#
## Not run: ans
#Class 'csFSS' : Stationary Solutions Object from costat:
# ~~~~~ : List with 13 components with names
# startpar endpar convergence minvar pvals tsx tsy tsxname tsyname
# filter.number family spec.filter.number spec.family
#
#
#summary(.):
#----------
#Name of X time series: tsx1
#Name of Y time series: tsy1
#Length of input series: 32
#There are 3 sets of solutions
#Each solution vector is based on 3 coefficients
#Some solutions did not converge, check convergence component for more information.
#Zero indicates successful convergence, other values mean different things and
#you should consult the help page for `optim' to discover what they mean
#For size level: 0.05
# 0 solutions appear NOT to be stationary
# 3 solutions appear to be stationary
#Range of p-values: ( 0.93 , 0.995 )
#
#Wavelet filter for combinations: 1 DaubExPhase
#Wavelet filter for spectrum: 1 DaubExPhase
#
#______________
#
# Ok. The printout above suggests that some solutions did not converge.
# Which ones?
#
## Not run: ans$convergence
# [1] 0 1 0
#
# The second one did not converge, the others did. Good. The printout
# above also indicates that all the resultant solutions were stationary
# (this is not surprising for this example, as the inputs tsx1 and tsy1
# are stationary, and indeed iid).
#
# Let's see how the solutions compare. For example, let's plot the
# hierarchical cluster analysis of the final solutions (those that
# converged and are stationary)
#
## Not run: plot(ans, ALLplotscale=FALSE)
#
# My cluster shows that solution 1 and 3 are similar. Let's
# view solution 3.
#
## Not run: oldpar <- par(mfrow=c(2,2))
## Not run: plot(ans, solno=3)
## Not run: par(oldpar)
``` |