From: <jpi...@us...> - 2012-03-28 13:32:52
|
Revision: 10080 http://octave.svn.sourceforge.net/octave/?rev=10080&view=rev Author: jpicarbajal Date: 2012-03-28 13:32:37 +0000 (Wed, 28 Mar 2012) Log Message: ----------- system-identitifaction: Adding devel TISEAN files Added Paths: ----------- trunk/octave-forge/main/system-identification/devel/tisean/ trunk/octave-forge/main/system-identification/devel/tisean/source_c/ trunk/octave-forge/main/system-identification/devel/tisean/source_c/Makefile.in trunk/octave-forge/main/system-identification/devel/tisean/source_c/ar-model.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/arima-model.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/av-d2.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/boxcount.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/corr.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/d2.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/delay.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/extrema.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/false_nearest.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/fsle.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/ghkss.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/histogram.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lfo-ar.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lfo-run.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lfo-test.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/low121.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lyap_k.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lyap_r.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lyap_spec.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lzo-gm.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lzo-run.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/lzo-test.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/makenoise.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/mem_spec.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/mutual.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/new.tgz trunk/octave-forge/main/system-identification/devel/tisean/source_c/nrlazy.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/nstat_z.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/pca.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/poincare.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/polyback.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/polynom.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/polynomp.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/polypar.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/rbf.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/recurr.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/resample.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/rescale.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/ trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/Makefile.in trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/arima.tgz trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/check_alloc.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/check_option.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/diffc.log trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/diffh.log trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/eigen.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/exclude_interval.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/find_multi_neighbors.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/find_neighbors.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/get_multi_series.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/get_series.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/invert_matrix.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/make_box.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/make_multi_box.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/make_multi_box2.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/make_multi_index.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/myfgets.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/rand.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/rand_arb_dist.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/rescale_data.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/scan_help.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/search_datafile.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/solvele.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/test_outfile.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/tisean_cec.h trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/tsa.h trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/variance.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/routines/what_i_do.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/sav_gol.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/xcor.c trunk/octave-forge/main/system-identification/devel/tisean/source_c/xzero.c trunk/octave-forge/main/system-identification/devel/tisean/source_f/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/Makefile.in trunk/octave-forge/main/system-identification/devel/tisean/source_f/addnoise.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/any_s.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/ar-run.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/arguments.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/autocor.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/c1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/c2d.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/c2g.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/c2naive.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/c2t.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/choose.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/cluster.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/commandline.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/compare.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/d1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/endtoend.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/events.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/gpl.txt trunk/octave-forge/main/system-identification/devel/tisean/source_f/help.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/henon.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/ikeda.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/intervals.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/istdio_temp.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/lazy.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/lorenz.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/neigh.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/nmore.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/normal.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/notch.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/others/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/pc.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/predict.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/project.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/Makefile.in trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cool/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cool/exp.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cost/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cost/auto.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cost/autop.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cost/spikeauto.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cost/spikespec.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/cost/uneven.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/perm/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/perm/event.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/perm/random.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/randomize/randomize.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/rank.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/readfile.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/rms.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/ trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/Makefile.in trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/chkder.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/d1mach.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/dqk15.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/enorm.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/fdjac3.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/fdump.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/i1mach.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/j4save.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/lmpar.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/pythag.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/qrfac.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/qrsolv.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/r1mach.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radb2.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radb3.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radb4.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radb5.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radbg.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radf2.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radf3.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radf4.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radf5.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/radfg.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rand.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rfftb1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rfftf1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rffti1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rgauss.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rs.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/rwupdt.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/snls1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/tql2.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/tqlrat.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/tred1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/tred2.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/xercnt.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/xerhlt.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/xermsg.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/xerprn.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/xersve.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/slatec/xgetua.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/spectrum.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/spikeauto.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/spikespec.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/store_spec.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/stp.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/surrogates.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/timerev.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/tospec.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/totospec.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/upo.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/upoembed.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/verbose.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/wiener1.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/wiener2.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/xc2.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/xreadfile.f trunk/octave-forge/main/system-identification/devel/tisean/source_f/xrecur.f Added: trunk/octave-forge/main/system-identification/devel/tisean/source_c/Makefile.in =================================================================== --- trunk/octave-forge/main/system-identification/devel/tisean/source_c/Makefile.in (rev 0) +++ trunk/octave-forge/main/system-identification/devel/tisean/source_c/Makefile.in 2012-03-28 13:32:37 UTC (rev 10080) @@ -0,0 +1,47 @@ +SHELL = /bin/sh + +prefix = @prefix@ +exec_prefix = @exec_prefix@ +BINDIR = ${exec_prefix}/@bindir@ + +CC = @CC@ +CFLAGS = @CFLAGS@ +AR = @AR@ +ARFLAGS = @ARFLAGS@ +INSTALL = @INSTALL@ + +LOADLIBS = routines/libddtsa.a -lm + +# list of executables we want to produce + ALL = poincare extrema rescale recurr corr mutual false_nearest \ + lyap_r lyap_k lyap_spec d2 av-d2 makenoise nrlazy low121 \ + lzo-test lfo-run lfo-test rbf polynom polyback polynomp polypar \ + ar-model mem_spec pca ghkss lfo-ar xzero xcor boxcount fsle \ + resample histogram nstat_z sav_gol delay lzo-gm arima-model \ + lzo-run + +all: $(ALL) + +routines/libddtsa.a: + (cd routines && $(MAKE)) + +$(ALL): routines/libddtsa.a *.c + -$(CC) $(CFLAGS) $(COPTS) -o $@ $@.c $(LOADLIBS) + +install: all + -for bin in $(ALL); do $(INSTALL) $$bin $(BINDIR); done + +clean: + @rm -f *.o *~ #*# + @rm -f $(ALL) + -(cd routines && $(MAKE) clean) + +missing: + -@for bin in $(ALL); do \ + test -z "`$$bin -h 2>&1 | grep Usage`" \ + && echo $$bin "(Dresden C)" >> ../missing.log; \ + $$bin -h 2>&1 | cat >> ../install.log; \ + done; : + +uninstall: + -@for bin in $(ALL); do rm -f $(BINDIR)/$$bin; done Added: trunk/octave-forge/main/system-identification/devel/tisean/source_c/ar-model.c =================================================================== --- trunk/octave-forge/main/system-identification/devel/tisean/source_c/ar-model.c (rev 0) +++ trunk/octave-forge/main/system-identification/devel/tisean/source_c/ar-model.c 2012-03-28 13:32:37 UTC (rev 10080) @@ -0,0 +1,395 @@ +/* + * This file is part of TISEAN + * + * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber + * + * TISEAN is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * TISEAN is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TISEAN; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +/*Author: Rainer Hegger*/ +/*Changes: + Jun 24, 2005: Output average error for multivariate data + Nov 25, 2005: Handle model order = 0 + Jan 31, 2006: Add verbosity 4 to print data+residuals + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <math.h> +#include "routines/tsa.h" + +#define WID_STR "Fits an multivariate AR model to the data and gives\ + the coefficients\n\tand the residues (or an iterated model)" + +unsigned long length=ULONG_MAX,exclude=0; +unsigned int dim=1,poles=1,ilength; +unsigned int verbosity=1; +char *outfile=NULL,*column=NULL,stdo=1,dimset=0,run_model=0; +char *infile=NULL; +double **series,*my_average; + +void show_options(char *progname) +{ + what_i_do(progname,WID_STR); + fprintf(stderr," Usage: %s [options]\n",progname); + fprintf(stderr," Options:\n"); + fprintf(stderr,"Everything not being a valid option will be interpreted" + " as a possible" + " datafile.\nIf no datafile is given stdin is read. Just - also" + " means stdin\n"); + fprintf(stderr,"\t-l length of file [default is whole file]\n"); + fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n"); + fprintf(stderr,"\t-m dimension [default is 1]\n"); + fprintf(stderr,"\t-c columns to read [default is 1,...,dimension]\n"); + fprintf(stderr,"\t-p #order of AR-Fit [default is 1]\n"); + fprintf(stderr,"\t-s length of iterated model [default no iteration]\n"); + fprintf(stderr,"\t-o output file name [default is 'datafile'.ar]\n"); + fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t" + "0='only panic messages'\n\t\t" + "1='+ input/output messages'\n\t\t" + "2='+ print residuals though iterating a model'\n\t\t" + "4='+ print original data plus residuals'\n"); + fprintf(stderr,"\t-h show these options\n\n"); + exit(0); +} + +void scan_options(int argc,char **argv) +{ + char *out; + + if ((out=check_option(argv,argc,'p','u')) != NULL) { + sscanf(out,"%u",&poles); + if (poles < 1) { + fprintf(stderr,"The order should at least be one!\n"); + exit(127); + } + } + if ((out=check_option(argv,argc,'l','u')) != NULL) + sscanf(out,"%lu",&length); + if ((out=check_option(argv,argc,'x','u')) != NULL) + sscanf(out,"%lu",&exclude); + if ((out=check_option(argv,argc,'m','u')) != NULL) { + sscanf(out,"%u",&dim); + dimset=1; + } + if ((out=check_option(argv,argc,'c','u')) != NULL) + column=out; + if ((out=check_option(argv,argc,'V','u')) != NULL) + sscanf(out,"%u",&verbosity); + if ((out=check_option(argv,argc,'s','u')) != NULL) { + sscanf(out,"%u",&ilength); + run_model=1; + } + if ((out=check_option(argv,argc,'o','o')) != NULL) { + stdo=0; + if (strlen(out) > 0) + outfile=out; + } +} + +void set_averages_to_zero(void) +{ + double var; + long i,j; + + for (i=0;i<dim;i++) { + variance(series[i],length,&my_average[i],&var); + for (j=0;j<length;j++) + series[i][j] -= my_average[i]; + } +} + +double** build_matrix(double **mat) +{ + long n,i1,j1,i2,j2,hi,hj; + double norm; + + norm=1./((double)length-(double)poles); + + for (i1=0;i1<dim;i1++) + for (i2=0;i2<poles;i2++) { + hi=i1*poles+i2; + for (j1=0;j1<dim;j1++) + for (j2=0;j2<poles;j2++) { + hj=j1*poles+j2; + mat[hi][hj]=0.0; + for (n=poles-1;n<length-1;n++) + mat[hi][hj] += series[i1][n-i2]*series[j1][n-j2]; + mat[hi][hj] *= norm; + } + } + + return invert_matrix(mat,(unsigned int)(dim*poles)); +} + +void build_vector(double *vec,long comp) +{ + long i1,i2,hi,n; + double norm; + + norm=1./((double)length-(double)poles); + + for (i1=0;i1<poles*dim;i1++) + vec[i1]=0.0; + + for (i1=0;i1<dim;i1++) + for (i2=0;i2<poles;i2++) { + hi=i1*poles+i2; + for (n=poles-1;n<length-1;n++) + vec[hi] += series[comp][n+1]*series[i1][n-i2]; + vec[hi] *= norm; + } +} + +double* multiply_matrix_vector(double **mat,double *vec) +{ + long i,j; + double *new_vec; + + check_alloc(new_vec=(double*)malloc(sizeof(double)*poles*dim)); + + for (i=0;i<poles*dim;i++) { + new_vec[i]=0.0; + for (j=0;j<poles*dim;j++) + new_vec[i] += mat[i][j]*vec[j]; + } + return new_vec; +} + +double* make_residuals(double **diff,double **coeff) +{ + long n,d,i,j; + double *resi; + + check_alloc(resi=(double*)malloc(sizeof(double)*dim)); + for (i=0;i<dim;i++) + resi[i]=0.0; + + for (n=poles-1;n<length-1;n++) { + for (d=0;d<dim;d++) { + diff[d][n+1]=series[d][n+1]; + for (i=0;i<dim;i++) + for (j=0;j<poles;j++) + diff[d][n+1] -= coeff[d][i*poles+j]*series[i][n-j]; + resi[d] += sqr(diff[d][n+1]); + } + } + for (i=0;i<dim;i++) + resi[i]=sqrt(resi[i]/((double)length-(double)poles)); + + return resi; +} + +void iterate_model(double **coeff,double *sigma,FILE *file) +{ + long i,j,i1,i2,n,d; + double **iterate,*swap; + + check_alloc(iterate=(double**)malloc(sizeof(double*)*(poles+1))); + for (i=0;i<=poles;i++) + check_alloc(iterate[i]=(double*)malloc(sizeof(double)*dim)); + rnd_init(0x44325); + for (i=0;i<1000;i++) + gaussian(1.0); + for (i=0;i<dim;i++) + for (j=0;j<poles;j++) + iterate[j][i]=gaussian(sigma[i]); + + for (n=0;n<ilength;n++) { + for (d=0;d<dim;d++) { + iterate[poles][d]=gaussian(sigma[d]); + for (i1=0;i1<dim;i1++) + for (i2=0;i2<poles;i2++) + iterate[poles][d] += coeff[d][i1*poles+i2]*iterate[poles-1-i2][i1]; + } + if (file != NULL) { + for (d=0;d<dim;d++) + fprintf(file,"%e ",iterate[poles][d]); + fprintf(file,"\n"); + } + else { + for (d=0;d<dim;d++) + printf("%e ",iterate[poles][d]); + printf("\n"); + } + + swap=iterate[0]; + for (i=0;i<poles;i++) + iterate[i]=iterate[i+1]; + iterate[poles]=swap; + } + + for (i=0;i<=poles;i++) + free(iterate[i]); + free(iterate); +} + +int main(int argc,char **argv) +{ + char stdi=0; + double *pm; + long i,j; + FILE *file; + double **mat,**inverse,*vec,**coeff,**diff,avpm; + + if (scan_help(argc,argv)) + show_options(argv[0]); + + scan_options(argc,argv); +#ifndef OMIT_WHAT_I_DO + if (verbosity&VER_INPUT) + what_i_do(argv[0],WID_STR); +#endif + + infile=search_datafile(argc,argv,NULL,verbosity); + if (infile == NULL) + stdi=1; + + if (outfile == NULL) { + if (!stdi) { + check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1)); + strcpy(outfile,infile); + strcat(outfile,".ar"); + } + else { + check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1)); + strcpy(outfile,"stdin.ar"); + } + } + if (!stdo) + test_outfile(outfile); + + if (column == NULL) + series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset, + verbosity); + else + series=(double**)get_multi_series(infile,&length,exclude,&dim,column, + dimset,verbosity); + + check_alloc(my_average=(double*)malloc(sizeof(double)*dim)); + set_averages_to_zero(); + + if (poles >= length) { + fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n"); + exit(AR_MODEL_TOO_MANY_POLES); + } + + + check_alloc(vec=(double*)malloc(sizeof(double)*poles*dim)); + check_alloc(mat=(double**)malloc(sizeof(double*)*poles*dim)); + for (i=0;i<poles*dim;i++) + check_alloc(mat[i]=(double*)malloc(sizeof(double)*poles*dim)); + + check_alloc(coeff=(double**)malloc(sizeof(double*)*dim)); + inverse=build_matrix(mat); + for (i=0;i<dim;i++) { + build_vector(vec,i); + coeff[i]=multiply_matrix_vector(inverse,vec); + } + + check_alloc(diff=(double**)malloc(sizeof(double*)*dim)); + for (i=0;i<dim;i++) + check_alloc(diff[i]=(double*)malloc(sizeof(double)*length)); + + pm=make_residuals(diff,coeff); + + if (stdo) { + avpm=pm[0]*pm[0]; + for (i=1;i<dim;i++) + avpm += pm[i]*pm[i]; + avpm=sqrt(avpm/dim); + printf("#average forcast error= %e\n",avpm); + printf("#individual forecast errors: "); + for (i=0;i<dim;i++) + printf("%e ",pm[i]); + printf("\n"); + for (i=0;i<dim*poles;i++) { + printf("# "); + for (j=0;j<dim;j++) + printf("%e ",coeff[j][i]); + printf("\n"); + } + if (!run_model || (verbosity&VER_USR1)) { + for (i=poles;i<length;i++) { + if (run_model) + printf("#"); + for (j=0;j<dim;j++) + if (verbosity&VER_USR2) + printf("%e %e ",series[j][i]+my_average[j],diff[j][i]); + else + printf("%e ",diff[j][i]); + printf("\n"); + } + } + if (run_model && (ilength > 0)) + iterate_model(coeff,pm,NULL); + } + else { + file=fopen(outfile,"w"); + if (verbosity&VER_INPUT) + fprintf(stderr,"Opened %s for output\n",outfile); + avpm=pm[0]*pm[0]; + for (i=1;i<dim;i++) + avpm += pm[i]*pm[i]; + avpm=sqrt(avpm/dim); + fprintf(file,"#average forcast error= %e\n",avpm); + fprintf(file,"#individual forecast errors: "); + for (i=0;i<dim;i++) + fprintf(file,"%e ",pm[i]); + fprintf(file,"\n"); + for (i=0;i<dim*poles;i++) { + fprintf(file,"# "); + for (j=0;j<dim;j++) + fprintf(file,"%e ",coeff[j][i]); + fprintf(file,"\n"); + } + if (!run_model || (verbosity&VER_USR1)) { + for (i=poles;i<length;i++) { + if (run_model) + fprintf(file,"#"); + for (j=0;j<dim;j++) + if (verbosity&VER_USR2) + fprintf(file,"%e %e ",series[j][i]+my_average[j],diff[j][i]); + else + fprintf(file,"%e ",diff[j][i]); + fprintf(file,"\n"); + } + } + if (run_model && (ilength > 0)) + iterate_model(coeff,pm,file); + fclose(file); + } + + if (outfile != NULL) + free(outfile); + if (infile != NULL) + free(infile); + free(vec); + for (i=0;i<poles*dim;i++) { + free(mat[i]); + free(inverse[i]); + } + free(mat); + free(inverse); + for (i=0;i<dim;i++) { + free(coeff[i]); + free(diff[i]); + } + free(coeff); + free(diff); + free(pm); + + return 0; +} Added: trunk/octave-forge/main/system-identification/devel/tisean/source_c/arima-model.c =================================================================== --- trunk/octave-forge/main/system-identification/devel/tisean/source_c/arima-model.c (rev 0) +++ trunk/octave-forge/main/system-identification/devel/tisean/source_c/arima-model.c 2012-03-28 13:32:37 UTC (rev 10080) @@ -0,0 +1,721 @@ +/* + * This file is part of TISEAN + * + * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber + * + * TISEAN is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * TISEAN is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TISEAN; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +/*Author: Rainer Hegger, Last modified: Feb 6, 2006 */ +/*Changes: + Feb 4, 2006: First version + Feb 6, 2006: Find and remove bugs (1) + Feb 11, 2006: Add rand_arb_dist to iterate_***_model + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <math.h> +#include "routines/tsa.h" + +#define WID_STR "Fits an multivariate ARIMA model to the data and gives\ + the coefficients\n\tand the residues (or an iterated model)" + +unsigned long length=ULONG_MAX,exclude=0; +unsigned int dim=1,poles=10,ilength,ITER=50; +unsigned int arpoles=0,ipoles=0,mapoles=0,offset; +unsigned int verbosity=1; +char *outfile=NULL,*column=NULL,stdo=1,dimset=0,run_model=0,arimaset=0; +char *infile=NULL; +double **series,convergence=1.0e-3; + +double *my_average; +unsigned long ardim,armadim; +unsigned int **aindex; + +void show_options(char *progname) +{ + what_i_do(progname,WID_STR); + fprintf(stderr," Usage: %s [options]\n",progname); + fprintf(stderr," Options:\n"); + fprintf(stderr,"Everything not being a valid option will be interpreted" + " as a possible" + " datafile.\nIf no datafile is given stdin is read. Just - also" + " means stdin\n"); + fprintf(stderr,"\t-l length of file [default is whole file]\n"); + fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n"); + fprintf(stderr,"\t-m dimension [default is 1]\n"); + fprintf(stderr,"\t-c columns to read [default is 1,...,dimension]\n"); + fprintf(stderr,"\t-p order of initial AR-Fit [default is %u]\n",poles); + fprintf(stderr,"\t-P order of AR,I,MA-Fit [default is %u,%u,%u]\n", + arpoles,ipoles,mapoles); + fprintf(stderr,"\t-I # of arima iterations [default is %u]\n",ITER); + fprintf(stderr,"\t-e accuracy of convergence [default is %lf]\n",convergence); + fprintf(stderr,"\t-s length of iterated model [default no iteration]\n"); + fprintf(stderr,"\t-o output file name [default is 'datafile'.ari]\n"); + fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t" + "0='only panic messages'\n\t\t" + "1='+ input/output messages'\n\t\t" + "2='+ print residuals though iterating a model'\n\t\t" + "4='+ print original data plus residuals'\n"); + fprintf(stderr,"\t-h show these options\n\n"); + exit(0); +} + +void scan_options(int argc,char **argv) +{ + char *out; + + if ((out=check_option(argv,argc,'p','u')) != NULL) { + sscanf(out,"%u",&poles); + if (poles < 1) { + fprintf(stderr,"The order should at least be one!\n"); + exit(127); + } + } + if ((out=check_option(argv,argc,'l','u')) != NULL) + sscanf(out,"%lu",&length); + if ((out=check_option(argv,argc,'x','u')) != NULL) + sscanf(out,"%lu",&exclude); + if ((out=check_option(argv,argc,'m','u')) != NULL) { + sscanf(out,"%u",&dim); + dimset=1; + } + if ((out=check_option(argv,argc,'P','3')) != NULL) { + sscanf(out,"%u,%u,%u",&arpoles,&ipoles,&mapoles); + if ((arpoles+ipoles+mapoles)>0) + arimaset=1; + } + if ((out=check_option(argv,argc,'I','u')) != NULL) + sscanf(out,"%u",&ITER); + if ((out=check_option(argv,argc,'e','f')) != NULL) + sscanf(out,"%lf",&convergence); + if ((out=check_option(argv,argc,'c','u')) != NULL) + column=out; + if ((out=check_option(argv,argc,'V','u')) != NULL) + sscanf(out,"%u",&verbosity); + if ((out=check_option(argv,argc,'s','u')) != NULL) { + sscanf(out,"%u",&ilength); + run_model=1; + } + if ((out=check_option(argv,argc,'o','o')) != NULL) { + stdo=0; + if (strlen(out) > 0) + outfile=out; + } +} + +void make_difference(void) +{ + unsigned long i,d; + + for (i=length-1;i>0;i--) + for (d=0;d<dim;d++) + series[d][i]=series[d][i]-series[d][i-1]; +} + +unsigned int** make_ar_index(void) +{ + unsigned int** ar_index; + unsigned long i; + + check_alloc(ar_index=(unsigned int**)malloc(sizeof(unsigned int*)*2)); + for (i=0;i<2;i++) + check_alloc(ar_index[i]=(unsigned int*) + malloc(sizeof(unsigned int)*ardim)); + for (i=0;i<ardim;i++) { + ar_index[0][i]=i/poles; + ar_index[1][i]=i%poles; + } + return ar_index; +} + +unsigned int** make_arima_index(unsigned int ars,unsigned int mas) +{ + unsigned int** arima_index; + unsigned int armad; + unsigned long i,i0; + + armad=(ars+mas)*dim; + check_alloc(arima_index=(unsigned int**)malloc(sizeof(unsigned int*)*2)); + for (i=0;i<2;i++) + check_alloc(arima_index[i]=(unsigned int*) + malloc(sizeof(unsigned int)*armad)); + for (i=0;i<ars*dim;i++) { + arima_index[0][i]=i/ars; + arima_index[1][i]=i%ars; + } + i0=ars*dim; + for (i=0;i<mas*dim;i++) { + arima_index[0][i+i0]=dim+i/mas; + arima_index[1][i+i0]=i%mas; + } + + return arima_index; +} + +void set_averages_to_zero(void) +{ + double var; + long i,j; + + for (i=0;i<dim;i++) { + variance(series[i],length,&my_average[i],&var); + for (j=0;j<length;j++) + series[i][j] -= my_average[i]; + } +} + +double** build_matrix(double **mat,unsigned int size) +{ + long n,i,j,is,id,js,jd; + double norm; + + norm=1./((double)length-1.0-(double)poles-(double)offset); + + for (i=0;i<size;i++) { + id=aindex[0][i]; + is=aindex[1][i]; + for (j=i;j<size;j++) { + jd=aindex[0][j]; + js=aindex[1][j]; + mat[i][j]=0.0; + for (n=offset+poles-1;n<length-1;n++) + mat[i][j] += series[id][n-is]*series[jd][n-js]; + mat[i][j] *= norm; + mat[j][i]=mat[i][j]; + } + } + + return invert_matrix(mat,size); +} + +void build_vector(double *vec,unsigned int size,long comp) +{ + long i,is,id,n; + double norm; + + norm=1./((double)length-1.0-(double)poles-(double)offset); + + for (i=0;i<size;i++) { + id=aindex[0][i]; + is=aindex[1][i]; + vec[i]=0.0; + for (n=offset+poles-1;n<length-1;n++) + vec[i] += series[comp][n+1]*series[id][n-is]; + vec[i] *= norm; + } +} + +double* multiply_matrix_vector(double **mat,double *vec,unsigned int size) +{ + long i,j; + double *new_vec; + + check_alloc(new_vec=(double*)malloc(sizeof(double)*size)); + + for (i=0;i<size;i++) { + new_vec[i]=0.0; + for (j=0;j<size;j++) + new_vec[i] += mat[i][j]*vec[j]; + } + + return new_vec; +} + +double* make_residuals(double **diff,double **coeff,unsigned int size) +{ + long n,n1,d,i,is,id; + double *resi; + + check_alloc(resi=(double*)malloc(sizeof(double)*dim)); + for (i=0;i<dim;i++) + resi[i]=0.0; + + for (n=poles-1;n<length-1;n++) { + n1=n+1; + for (d=0;d<dim;d++) { + diff[d][n1]=series[d][n1]; + for (i=0;i<size;i++) { + id=aindex[0][i]; + is=aindex[1][i]; + diff[d][n1] -= coeff[d][i]*series[id][n-is]; + } + resi[d] += sqr(diff[d][n1]); + } + } + + for (i=0;i<dim;i++) + resi[i]=sqrt(resi[i]/((double)length-(double)poles)); + + return resi; +} + +void iterate_model(double **coeff,double *sigma,double **diff,FILE *file) +{ + long i,j,i1,i2,n,d; + double **iterate,*swap,**myrand; + + check_alloc(iterate=(double**)malloc(sizeof(double*)*(poles+1))); + for (i=0;i<=poles;i++) + check_alloc(iterate[i]=(double*)malloc(sizeof(double)*dim)); + + check_alloc(myrand=(double**)malloc(sizeof(double*)*dim)); + for (i=0;i<dim;i++) + myrand[i]=rand_arb_dist(diff[i],length,ilength+poles,100,0x44325); + + rnd_init(0x44325); + for (i=0;i<1000;i++) + rnd_long(); + for (i=0;i<dim;i++) + for (j=0;j<poles;j++) + iterate[j][i]=myrand[i][j]; + + for (n=0;n<ilength;n++) { + for (d=0;d<dim;d++) { + iterate[poles][d]=myrand[d][n+poles]; + for (i1=0;i1<dim;i1++) + for (i2=0;i2<poles;i2++) + iterate[poles][d] += coeff[d][i1*poles+i2]*iterate[poles-1-i2][i1]; + } + if (file != NULL) { + for (d=0;d<dim;d++) + fprintf(file,"%e ",iterate[poles][d]); + fprintf(file,"\n"); + } + else { + for (d=0;d<dim;d++) + printf("%e ",iterate[poles][d]); + printf("\n"); + } + + swap=iterate[0]; + for (i=0;i<poles;i++) + iterate[i]=iterate[i+1]; + iterate[poles]=swap; + } + + for (i=0;i<=poles;i++) + free(iterate[i]); + free(iterate); + + for (i=0;i<dim;i++) + free(myrand[i]); + free(myrand); +} + +void iterate_arima_model(double **coeff,double *sigma,double **diff,FILE *file) +{ + double **iterate,*swap,**myrand; + unsigned long i,j,n,is,id; + + check_alloc(iterate=(double**)malloc(sizeof(double*)*(poles+1))); + for (i=0;i<=poles;i++) + check_alloc(iterate[i]=(double*)malloc(sizeof(double)*2*dim)); + + check_alloc(myrand=(double**)malloc(sizeof(double*)*dim)); + for (i=0;i<dim;i++) + myrand[i]=rand_arb_dist(diff[i],length,ilength+poles,100,0x44325); + + rnd_init(0x44325); + for (i=0;i<1000;i++) + rnd_long(); + for (i=0;i<dim;i++) + for (j=0;j<poles;j++) + iterate[j][i]=iterate[j][dim+i]=myrand[i][j]; + + for (n=0;n<ilength;n++) { + for (i=0;i<dim;i++) + iterate[poles][i]=iterate[poles][i+dim]=myrand[i][n+poles]; + + for (j=0;j<dim;j++) { + for (i=0;i<armadim;i++) { + id=aindex[0][i]; + is=aindex[1][i]; + iterate[poles][j] += coeff[j][i]*iterate[poles-1-is][id]; + } + } + + if (file != NULL) { + for (i=0;i<dim;i++) + fprintf(file,"%e ",iterate[poles][i]); + fprintf(file,"\n"); + } + else { + for (i=0;i<dim;i++) + printf("%e ",iterate[poles][i]); + printf("\n"); + } + + swap=iterate[0]; + for (i=0;i<poles;i++) + iterate[i]=iterate[i+1]; + iterate[poles]=swap; + } + + for (i=0;i<=poles;i++) + free(iterate[i]); + free(iterate); + for (i=0;i<dim;i++) + free(myrand[i]); + free(myrand); +} + +int main(int argc,char **argv) +{ + char stdi=0; + double *pm; + long i,j,iter,hj,realiter=0; + unsigned int size,is,id; + FILE *file; + double **mat,**inverse,*vec,**coeff,**diff,**hseries; + double **oldcoeff,*diffcoeff=NULL; + double hdiff,**xdiff=NULL,avpm; + double loglikelihood,aic,alldiff; + + if (scan_help(argc,argv)) + show_options(argv[0]); + + scan_options(argc,argv); +#ifndef OMIT_WHAT_I_DO + if (verbosity&VER_INPUT) + what_i_do(argv[0],WID_STR); +#endif + + infile=search_datafile(argc,argv,NULL,verbosity); + if (infile == NULL) + stdi=1; + + if (outfile == NULL) { + if (!stdi) { + check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1)); + strcpy(outfile,infile); + strcat(outfile,".ari"); + } + else { + check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1)); + strcpy(outfile,"stdin.ari"); + } + } + if (!stdo) + test_outfile(outfile); + + if (column == NULL) + series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset, + verbosity); + else + series=(double**)get_multi_series(infile,&length,exclude,&dim,column, + dimset,verbosity); + + check_alloc(my_average=(double*)malloc(sizeof(double)*dim)); + + for (i=0;i<ipoles;i++) + make_difference(); + + for (i=0;i<dim;i++) + series[i] += ipoles; + length -= ipoles; + + set_averages_to_zero(); + + if (poles >= length) { + fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n"); + exit(AR_MODEL_TOO_MANY_POLES); + } + if (arimaset) { + if ((arpoles >= length) || (mapoles >= length)) { + fprintf(stderr,"It makes no sense to have more poles than data! Exiting\n"); + exit(AR_MODEL_TOO_MANY_POLES); + } + } + + ardim=poles*dim; + aindex=make_ar_index(); + + check_alloc(vec=(double*)malloc(sizeof(double)*ardim)); + check_alloc(mat=(double**)malloc(sizeof(double*)*ardim)); + for (i=0;i<ardim;i++) + check_alloc(mat[i]=(double*)malloc(sizeof(double)*ardim)); + + check_alloc(coeff=(double**)malloc(sizeof(double*)*dim)); + inverse=build_matrix(mat,ardim); + for (i=0;i<dim;i++) { + build_vector(vec,ardim,i); + coeff[i]=multiply_matrix_vector(inverse,vec,ardim); + } + + check_alloc(diff=(double**)malloc(sizeof(double*)*dim)); + for (i=0;i<dim;i++) + check_alloc(diff[i]=(double*)malloc(sizeof(double)*length)); + + pm=make_residuals(diff,coeff,ardim); + + free(vec); + for (i=0;i<ardim;i++) { + free(mat[i]); + free(inverse[i]); + } + free(mat); + free(inverse); + size=ardim; + + if (arimaset) { + offset=poles; + for (i=0;i<2;i++) + free(aindex[i]); + free(aindex); + + for (i=0;i<dim;i++) + free(coeff[i]); + free(coeff); + check_alloc(xdiff=(double**)malloc(sizeof(double*)*ITER)); + for (i=0;i<ITER;i++) + check_alloc(xdiff[i]=(double*)malloc(sizeof(double)*dim)); + + armadim=(arpoles+mapoles)*dim; + aindex=make_arima_index(arpoles,mapoles); + size=armadim; + + check_alloc(hseries=(double**)malloc(sizeof(double*)*2*dim)); + for (i=0;i<dim;i++) { + check_alloc(hseries[i]=(double*)malloc(sizeof(double)*length)); + check_alloc(hseries[i+dim]=(double*)malloc(sizeof(double)*length)); + for (j=0;j<length;j++) { + hseries[i][j]=series[i][j]; + hseries[i+dim][j]=diff[i][j]; + } + } + + for (i=0;i<dim;i++) + free(series[i]-ipoles); + free(series); + + series=hseries; + + check_alloc(oldcoeff=(double**)malloc(sizeof(double*)*dim)); + for (i=0;i<dim;i++) { + check_alloc(oldcoeff[i]=(double*)malloc(sizeof(double)*armadim)); + for (j=0;j<armadim;j++) + oldcoeff[i][j]=0.0; + } + check_alloc(diffcoeff=(double*)malloc(sizeof(double)*ITER)); + + for (iter=1;iter<=ITER;iter++) { + check_alloc(vec=(double*)malloc(sizeof(double)*armadim)); + check_alloc(mat=(double**)malloc(sizeof(double*)*armadim)); + for (i=0;i<armadim;i++) + check_alloc(mat[i]=(double*)malloc(sizeof(double)*armadim)); + + check_alloc(coeff=(double**)malloc(sizeof(double*)*dim)); + + poles=(arpoles > mapoles)? arpoles:mapoles; + + offset += poles; + inverse=build_matrix(mat,armadim); + + for (i=0;i<dim;i++) { + build_vector(vec,armadim,i); + coeff[i]=multiply_matrix_vector(inverse,vec,armadim); + } + + pm=make_residuals(diff,coeff,armadim); + + for (j=0;j<dim;j++) { + hdiff=0.0; + hj=j+dim; + for (i=offset;i<length;i++) + hdiff += sqr(series[hj][i]-diff[j][i]); + for (i=0;i<length;i++) { + series[hj][i]=diff[j][i]; + } + xdiff[iter-1][j]=sqrt(hdiff/(double)(length-offset)); + } + + free(vec); + for (i=0;i<armadim;i++) { + free(mat[i]); + free(inverse[i]); + } + free(mat); + free(inverse); + + diffcoeff[iter-1]=0.0; + for (i=0;i<dim;i++) + for (j=0;j<dim;j++) { + diffcoeff[iter-1] += sqr(coeff[i][j]-oldcoeff[i][j]); + oldcoeff[i][j]=coeff[i][j]; + } + diffcoeff[iter-1]=sqrt(diffcoeff[iter-1]/(double)armadim); + alldiff=xdiff[iter-1][0]; + for (i=1;i<dim;i++) + if (xdiff[iter-1][i] > alldiff) + alldiff=xdiff[iter-1][i]; + realiter=iter; + if (alldiff < convergence) + iter=ITER; + + if (iter < ITER) { + for (i=0;i<dim;i++) + free(coeff[i]); + free(coeff); + } + } + } + + if (stdo) { + if (arimaset) { + printf("#convergence of residuals in arima fit\n"); + for (i=0;i<realiter;i++) { + printf("#iteration %ld ",i+1); + for (j=0;j<dim;j++) + printf("%e ",xdiff[i][j]); + printf("%e",diffcoeff[i]); + printf("\n"); + } + } + avpm=pm[0]*pm[0]; + loglikelihood= -log(pm[0]); + for (i=1;i<dim;i++) { + avpm += pm[i]*pm[i]; + loglikelihood -= log(pm[i]); + } + loglikelihood *= ((double)length); + loglikelihood += -((double)length)* + ((1.0+log(2.*M_PI))*dim)/2.0; + avpm=sqrt(avpm/dim); + printf("#average forcast error= %e\n",avpm); + printf("#individual forecast errors: "); + for (i=0;i<dim;i++) + printf("%e ",pm[i]); + printf("\n"); + if (arimaset) + aic=2.0*(arpoles+mapoles)-2.0*loglikelihood; + else + aic=2.0*poles-2.0*loglikelihood; + printf("#Log-Likelihood= %e\t AIC= %e\n",loglikelihood,aic); + for (i=0;i<size;i++) { + id=aindex[0][i]; + is=aindex[1][i]; + if (id < dim) + printf("#x_%u(n-%u) ",id+1,is); + else + printf("#e_%u(n-%u) ",id+1-dim,is); + for (j=0;j<dim;j++) + printf("%e ",coeff[j][i]); + printf("\n"); + } + if (!run_model || (verbosity&VER_USR1)) { + for (i=poles;i<length;i++) { + if (run_model) + printf("#"); + for (j=0;j<dim;j++) + if (verbosity&VER_USR2) + printf("%e %e ",series[j][i]+my_average[j],diff[j][i]); + else + printf("%e ",diff[j][i]); + printf("\n"); + } + } + if (run_model && (ilength > 0)) { + if (!arimaset) + iterate_model(coeff,pm,diff,NULL); + else + iterate_arima_model(coeff,pm,diff,NULL); + } + } + else { + file=fopen(outfile,"w"); + if (verbosity&VER_INPUT) + fprintf(stderr,"Opened %s for output\n",outfile); + if (arimaset) { + fprintf(file,"#convergence of residuals in arima fit\n"); + for (i=0;i<realiter;i++) { + fprintf(file,"#iteration %ld ",i+1); + for (j=0;j<dim;j++) + fprintf(file,"%e ",xdiff[i][j]); + fprintf(file,"%e",diffcoeff[i]); + fprintf(file,"\n"); + } + } + avpm=pm[0]*pm[0]; + loglikelihood= -log(pm[0]); + for (i=1;i<dim;i++) { + avpm += pm[i]*pm[i]; + loglikelihood -= log(pm[i]); + } + loglikelihood *= ((double)length); + loglikelihood += -((double)length)* + ((1.0+log(2.*M_PI))*dim)/2.0; + avpm=sqrt(avpm/dim); + fprintf(file,"#average forcast error= %e\n",avpm); + fprintf(file,"#individual forecast errors: "); + for (i=0;i<dim;i++) + fprintf(file,"%e ",pm[i]); + fprintf(file,"\n"); + if (arimaset) + aic=2.0*(arpoles+mapoles)-2.0*loglikelihood; + else + aic=2.0*poles-2.0*loglikelihood; + fprintf(file,"#Log-Likelihood= %e\t AIC= %e\n",loglikelihood,aic); + for (i=0;i<size;i++) { + id=aindex[0][i]; + is=aindex[1][i]; + if (id < dim) + fprintf(file,"#x_%u(n-%u) ",id+1,is); + else + fprintf(file,"#e_%u(n-%u) ",id+1-dim,is); + for (j=0;j<dim;j++) + fprintf(file,"%e ",coeff[j][i]); + fprintf(file,"\n"); + } + if (!run_model || (verbosity&VER_USR1)) { + for (i=poles;i<length;i++) { + if (run_model) + fprintf(file,"#"); + for (j=0;j<dim;j++) + if (verbosity&VER_USR2) + fprintf(file,"%e %e ",series[j][i]+my_average[j],diff[j][i]); + else + fprintf(file,"%e ",diff[j][i]); + fprintf(file,"\n"); + } + } + if (run_model && (ilength > 0)) { + if (!arimaset) + iterate_model(coeff,pm,diff,file); + else + iterate_arima_model(coeff,pm,diff,file); + } + fclose(file); + } + if (outfile != NULL) + free(outfile); + if (infile != NULL) + free(infile); + for (i=0;i<dim;i++) { + free(coeff[i]); + free(diff[i]); + free(series[i]); + if (arimaset) + free(series[i+dim]); + } + free(coeff); + free(diff); + free(series); + + free(pm); + + return 0; +} Added: trunk/octave-forge/main/system-identification/devel/tisean/source_c/av-d2.c =================================================================== --- trunk/octave-forge/main/system-identification/devel/tisean/source_c/av-d2.c (rev 0) +++ trunk/octave-forge/main/system-identification/devel/tisean/source_c/av-d2.c 2012-03-28 13:32:37 UTC (rev 10080) @@ -0,0 +1,188 @@ +/* + * This file is part of TISEAN + * + * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber + * + * TISEAN is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * TISEAN is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TISEAN; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +/*Author: Rainer Hegger Last modified: Sep 3, 1999 */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <limits.h> +#include "routines/tsa.h" + +#define WID_STR "Smoothes the output of the d2 program" + +#define MAXLENGTH 1000 + +unsigned int maxdim=UINT_MAX,mindim=1; +unsigned int verbosity=0xff; +int aver=1; +char rescaled=0; +char stout=1; +char *outfile=NULL; +char *infile=NULL; + +void show_options(char *progname) +{ + what_i_do(progname,WID_STR); + fprintf(stderr,"Usage: %s [options]\n",progname); + fprintf(stderr,"Options:\n"); + fprintf(stderr,"Everything not being a valid option will be interpreted" + " as a possible datafile.\nStdin does NOT work.\n"); + fprintf(stderr,"\t-m dimension to start with [Default: 1]\n"); + fprintf(stderr,"\t-M dimension to end with [Default: whole file]\n"); + fprintf(stderr,"\t-a n average over (2n+1) values [Default: 1]\n"); + fprintf(stderr,"\t-E use rescaled data for the length scales\n\t\t" + "[Default: use units of data]\n"); + fprintf(stderr,"\t-o name of output file [Default: stdout,\n\t\t" + "-o without value means 'datafile'.av]\n"); + fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t" + "0='only panic messages'\n\t\t" + "1='+ input/output messages'\n"); + fprintf(stderr,"\t-h show these options\n"); + exit(0); +} + +void scan_options(int n,char **in) +{ + char *out; + + if ((out=check_option(in,n,'m','u')) != NULL) + sscanf(out,"%u",&mindim); + if ((out=check_option(in,n,'M','u')) != NULL) + sscanf(out,"%u",&maxdim); + if ((out=check_option(in,n,'a','u')) != NULL) + sscanf(out,"%u",&aver); + if ((out=check_option(in,n,'V','u')) != NULL) + sscanf(out,"%u",&verbosity); + if ((out=check_option(in,n,'E','n')) != NULL) + rescaled=1; + if ((out=check_option(in,n,'o','o')) != NULL) { + stout=0; + if (strlen(out) > 0) + outfile=out; + } +} + +int main(int argc,char **argv) +{ + char instr[1024]; + char *form1="%lf%lf",*form2="%*lf%lf%lf"; + char empty=0; + unsigned int howmany,size=1; + int j,k; + long dim; + double *eps,*y; + double avy,aveps,norm; + FILE *file,*fout=NULL; + + if ((argc < 2) || scan_help(argc,argv)) + show_options(argv[0]); + + scan_options(argc,argv); +#ifndef OMIT_WHAT_I_DO + if (verbosity&VER_INPUT) + what_i_do(argv[0],WID_STR); +#endif + + infile=search_datafile(argc,argv,0L,verbosity); + if (infile == NULL) { + fprintf(stderr,"You have to give a datafile. Exiting!\n"); + exit(127); + } + if (outfile == NULL) { + check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1)); + sprintf(outfile,"%s.av",infile); + } + + check_alloc(eps=(double*)malloc(sizeof(double)*MAXLENGTH)); + check_alloc(y=(double*)malloc(sizeof(double)*MAXLENGTH)); + + file=fopen(infile,"r"); + + if (!stout) { + test_outfile(outfile); + fout=fopen(outfile,"w"); + if (verbosity&VER_INPUT) + fprintf(stderr,"Opened %s for writing\n",outfile); + } + else { + if (verbosity&VER_INPUT) + fprintf(stderr,"Writing to stdout\n"); + } + + if (mindim > maxdim) + mindim=maxdim; + norm=2.0*aver+1.0; + + while (fgets(instr,1024,file) != NULL) { + if (strlen(instr) != 1) { + if (instr[0] == '#') { + if (strstr(instr,"m= ") != NULL) { + sscanf(instr,"%*s %ld",&dim); + if ((dim >= mindim) && (dim <= maxdim)) { + howmany=0; + empty=0; + do { + if (fgets(instr,1024,file) == NULL) + exit(127); + if (strlen(instr) == 1) + empty=1; + if (!empty && (instr[0] != '#')) { + if (!rescaled) + sscanf(instr,form1,&eps[howmany],&y[howmany]); + else + sscanf(instr,form2,&y[howmany],&eps[howmany]); + howmany++; + if (!(howmany%MAXLENGTH)) { + check_alloc(realloc(eps,size*MAXLENGTH*sizeof(double))); + check_alloc(realloc(y,size*MAXLENGTH*sizeof(double))); + size++; + } + } + } while (!empty); + for (k=aver;k<howmany-aver;k++) { + avy=aveps=0.0; + for (j= -aver;j<=aver;j++) { + avy += y[k+j]; + aveps += eps[k+j]; + } + if (!stout) + fprintf(fout,"%e %e\n",aveps/norm,avy/norm); + else + fprintf(stdout,"%e %e\n",aveps/norm,avy/norm); + } + if (!stout) + fprintf(fout,"\n"); + else + fprintf(stdout,"\n"); + } + } + } + } + } + + if (outfile != NULL) + free(outfile); + if (infile != NULL) + free(infile); + free(eps); + free(y); + + return 0; +} Added: trunk/octave-forge/main/system-identification/devel/tisean/source_c/boxcount.c =================================================================== --- trunk/octave-forge/main/system-identification/devel/tisean/source_c/boxcount.c (rev 0) +++ trunk/octave-forge/main/system-identification/devel/tisean/source_c/boxcount.c 2012-03-28 13:32:37 UTC (rev 10080) @@ -0,0 +1,369 @@ +/* + * This file is part of TISEAN + * + * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber + * + * TISEAN is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * TISEAN is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TISEAN; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +/* Author: Rainer Hegger Last modified: Feb 22, 2006 */ +/* Changes: + 02/22/06: Remove this strange else in start_box that + did not compile anyways +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <string.h> +#include <limits.h> +#include "routines/tsa.h" + +#define WID_STR "Estimates the Renyi entropy of Qth order\n\t\ +using a partition instead of a covering." + +typedef struct { + double *hist; + void *ptr; +} hliste; + +unsigned long LENGTH=ULONG_MAX,exclude=0; +unsigned int maxembed=10,dimension=1,DELAY=1,EPSCOUNT=20; +unsigned int verbosity=0xff; +double Q=2.0,EPSMIN=1.e-3,EPSMAX=1.0; +char dimset=0,epsminset=0,epsmaxset=0; +char *outfile=NULL; +char *column=NULL; + +int epsi; +unsigned long length; +double EPSFAKTOR; +unsigned int **which_dims; +double *histo; +double **series; + +void show_options(char *progname) +{ + what_i_do(progname,WID_STR); + fprintf(stderr,"Usage: %s [Options]\n",progname); + fprintf(stderr,"Options:\n"); + fprintf(stderr,"\t-l # of datapoints [Default: whole file]\n"); + fprintf(stderr,"\t-x # of lines to ignore [Default: %lu]\n",exclude); + fprintf(stderr,"\t-M # of columns,maximal embedding dimension " + "[Default: %u,%u]\n",dimension,maxembed); + fprintf(stderr,"\t-c columns to read [Default: 1,...,#of compon.]\n"); + fprintf(stderr,"\t-d delay [Default: %u]\n",DELAY); + fprintf(stderr,"\t-Q order of the Renyi entropy [Default: %.1f]\n",Q); + fprintf(stderr,"\t-r minimal epsilon [Default: (data interval)/1000]\n"); + fprintf(stderr,"\t-R maximal epsilon [Default: data interval]\n"); + fprintf(stderr,"\t-# # of epsilons to use [Default: %u]\n",EPSCOUNT); + fprintf(stderr,"\t-o output file name [Default: 'datafile'.box]\n"); + fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t" + "0='only panic messages'\n\t\t" + "1='+ input/output messages'\n"); + fprintf(stderr,"\t-h show these options\n\n"); + exit(0); +} + +void scan_options(int n,char **in) +{ + char *out; + + if ((out=check_option(in,n,'l','u')) != NULL) + sscanf(out,"%lu",&LENGTH); + if ((out=check_option(in,n,'c','s')) != NULL) + column=out; + if ((out=check_option(in,n,'x','u')) != NULL) + sscanf(out,"%lu",&exclude); + if ((out=check_option(in,n,'M','2')) != NULL) { + sscanf(out,"%u,%u",&dimension,&maxembed); + dimset=1; + } + if ((out=check_option(in,n,'d','u')) != NULL) + sscanf(out,"%u",&DELAY); + if ((out=check_option(in,n,'Q','f')) != NULL) + sscanf(out,"%lf",&Q); + if ((out=check_option(in,n,'r','f')) != NULL) { + sscanf(out,"%lf",&EPSMIN); + epsminset=1; + } + if ((out=check_option(in,n,'R','f')) != NULL) { + sscanf(out,"%lf",&EPSMAX); + epsmaxset=1; + } + if ((out=check_option(in,n,'#','u')) != NULL) + sscanf(out,"%u",&EPSCOUNT); + if ((out=check_option(in,n,'V','u')) != NULL) + sscanf(out,"%u",&verbosity); + if ((out=check_option(in,n,'o','s')) != NULL) + outfile=out; +} + +hliste *make_histo(void) +{ + int i; + hliste *element; + + check_alloc(element=(hliste*)malloc(sizeof(hliste))); + element->ptr=NULL; + check_alloc(element->hist=(double*)malloc(sizeof(double)*maxembed*dimension)); + for (i=0;i<maxembed*dimension;i++) + element->hist[i]=0.0; + + return element; +} + +void next_dim(int wd,int n,unsigned int *first) +{ + int i,which,d1,comp; + double epsinv,norm,p; + unsigned int **act; + int *found,hf; + + comp=which_dims[wd][0]; + d1=which_dims[wd][1]*DELAY; + + epsinv=(double)epsi; + norm=(double)length; + + check_alloc(act=(unsigned int**)malloc(epsi*sizeof(int*))); + check_alloc(found=(int*)malloc(epsi*sizeof(int))); + + for (i=0;i<epsi;i++) { + found[i]=0; + act[i]=NULL; + } + + for (i=0;i<n;i++) { + which=(int)(series[comp][first[i]+d1]*epsinv); + hf= ++found[which]; + check_alloc(act[which]= + realloc((unsigned int*)act[which],hf*sizeof(unsigned int))); + act[which][hf-1]=first[i]; + } + + for (i=0;i<epsi;i++) + if (found[i]) { + p=(double)(found[i])/(norm); + if (Q == 1.0) + histo[wd] -= p*log(p); + else + histo[wd] += pow(p,Q); + } + + if (wd<(maxembed*dimension-1)) + for (i=0;i<epsi;i++) + if (found[i]) + next_dim(wd+1,found[i],act[i]); + + for (i=0;i<epsi;i++) + if (found[i]) + free(act[i]); + + free(act); + free(found); +} + +void start_box(void) +{ + int i,which; + double epsinv,norm,p; + unsigned int **act; + int *found,hf; + void next_dim(); + + epsinv=(double)epsi; + norm=(double)length; + + check_alloc(act=(unsigned int**)malloc(epsi*sizeof(int*))); + check_alloc(found=(int*)malloc(epsi*sizeof(int))); + + for (i=0;i<epsi;i++) { + found[i]=0; + act[i]=NULL; + } + + for (i=0;i<length;i++) { + which=(int)(series[0][i]*epsinv); + hf= ++found[w... [truncated message content] |