Example #1
0
int main(int argc,char **argv)
{
  long i;
  char stdi=0;
  double min,max,maxmax;

  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,".rec");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.rec");
    }
  }
  if (!stdo)
    test_outfile(outfile);

  if (columns == NULL)
    series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
                                      verbosity);
  else
    series=(double**)get_multi_series(infile,&length,exclude,&dim,columns,
                                      dimset,verbosity);

  maxmax=0.0;
  for (i=0;i<dim;i++) {
    rescale_data(series[i],length,&min,&max);
    if (max > maxmax)
      maxmax=max;
  }

  if (epsset)
    eps /= maxmax;

  check_alloc(list=(long*)malloc(sizeof(long)*length));
  check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
  for (i=0;i<BOX;i++)
    check_alloc(box[i]=(long*)malloc(sizeof(long)*BOX));

  make_multi_box(series,box,list,length,BOX,dim,embed,delay,eps);
  lfind_neighbors();

  return 0;
}
Example #2
0
int main(int argc,char **argv)
{
  char stdi=0;
  unsigned int i,j;
  double rms,*av;

  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,".pca");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.pca");
    }
  }
  if (!stout)
    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);
  dimemb=DIM*EMB;
  if (!projection_set)
    LDIM=dimemb;
  else {
    if (LDIM < 1) LDIM=1;
    if (LDIM > dimemb) LDIM=dimemb;
  }

  check_alloc(av=(double*)malloc(sizeof(double)*DIM));
  for (j=0;j<DIM;j++) {
    av[j]=rms=0.0;
    variance(series[j],LENGTH,&av[j],&rms);
    for (i=0;i<LENGTH;i++)
      series[j][i] -= av[j];
  }
  make_pca(av);

  return 0;
}
Example #3
0
int main(int argc,char **argv)
{
  char stin=0;
  unsigned long i;
  int j,k;
  unsigned int alldim,maxemb,emb,rundel,delsum,runmdel;
  unsigned int *inddelay;
  FILE *fout;

  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)
    stin=1;

  if (outfile == NULL) {
    if (!stin) {
      check_alloc(outfile=(char*)calloc(strlen(infile)+5,1));
      strcpy(outfile,infile);
      strcat(outfile,".del");
    }
    else {
      check_alloc(outfile=(char*)calloc(10,1));
      strcpy(outfile,"stdin.del");
    }
  }
  if (!stdo)
    test_outfile(outfile);

  if (delayset && mdelayset) {
    fprintf(stderr,"-d and -D can't be used simultaneously. Exiting!\n");
    exit(DELAY_INCONS_d_D);
  }

  if (delay < 1) {
    fprintf(stderr,"Delay has to be larger than 0. Exiting!\n");
    exit(DELAY_SMALL_ZERO);
  }

  if (!formatset && (embdim%indim)) {
    fprintf(stderr,"Inconsistent -m and -M. Please set -F\n");
    exit(DELAY_INCONS_m_M);
  }
  if (formatset) {
    create_format_list();
  }
  else {
    check_alloc(formatlist=(unsigned int*)malloc(sizeof(int)*indim));
    for (i=0;i<indim;i++) {
      formatlist[i]=embdim/indim;
    }
  }

  alldim=0;
  for (i=0;i<indim;i++)
    alldim += formatlist[i];

  if (mdelayset) {
    create_delay_list();
  }

  check_alloc(inddelay=(unsigned int*)malloc(sizeof(int)*alldim));

  rundel=0;
  if (!mdelayset) {
    for (i=0;i<indim;i++) {
      delsum=0;
      inddelay[rundel++]=delsum;
      for (j=1;j<formatlist[i];j++) {
	delsum += delay;
	inddelay[rundel++]=delsum;
      }
    }
  }
  else {
    runmdel=0;
    for (i=0;i<indim;i++) {
      delsum=0;
      inddelay[rundel++]=delsum;
      for (j=1;j<formatlist[i];j++) {
	delsum += delaylist[runmdel++];
	inddelay[rundel++]=delsum;
      }
    }
  }

  maxemb=0;
  for (i=0;i<alldim;i++)
    maxemb=(maxemb<inddelay[i])?inddelay[i]:maxemb;

  if (column == NULL) {
    series=get_multi_series(infile,&length,exclude,&indim,"",dimset,verbosity);
  }
  else {
    series=get_multi_series(infile,&length,exclude,&indim,column,dimset,
			    verbosity);
  }

  if (stdo) {
    if (verbosity)
      fprintf(stderr,"Writing to stdout\n");
    for (i=maxemb;i<length;i++) {
      rundel=0;
      for (j=0;j<indim;j++) {
	emb=formatlist[j];
	for (k=0;k<emb;k++)
	  fprintf(stdout,"%e ",series[j][i-inddelay[rundel++]]);
      }
      fprintf(stdout,"\n");
    }
  }
  else {
    fout=fopen(outfile,"w");
    if (verbosity)
      fprintf(stderr,"Opened %s for writing\n",outfile);
    for (i=maxemb;i<length;i++) {
      for (j=0;j<indim;j++) {
	rundel=0;
	emb=formatlist[j];
	for (k=0;k<emb;k++)
	  fprintf(fout,"%e ",series[j][i-inddelay[rundel++]]);
      }
      fprintf(fout,"\n");
    }
    fclose(fout);
  }

  if (formatlist != NULL)
    free(formatlist);
  if (delaylist != NULL)
    free(delaylist);
  free(inddelay);

  return 0;
}
Example #4
0
int main(int argc,char** argv)
{
  char stdi=0;
  unsigned long i,j;
  double av=0.0,*sigmax;
  FILE *fout;

  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

  if (!justcreate) {
    infile=search_datafile(argc,argv,NULL,verbosity);
    if (infile == NULL)
      stdi=1;
  }
  else
    stdi=1;

  if (outfile == NULL) {
    if (!stdi) {
      check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
      strcpy(outfile,infile);
      strcat(outfile,".noi");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.noi");
    }
  }
  if (!stout)
    test_outfile(outfile);

  if (!justcreate) {
    if (column == NULL)
      array=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
				       verbosity);
    else
      array=(double**)get_multi_series(infile,&length,exclude,&dim,column,
				       dimset,verbosity);
  }
  else {
    check_alloc(array=(double**)malloc(sizeof(double*)*dim));
    for (i=0;i<dim;i++) {
      check_alloc(array[i]=(double*)malloc(sizeof(double)*length));
      for (j=0;j<length;j++)
	array[i][j]=0.0;
    }
  }

  check_alloc(sigmax=(double*)malloc(sizeof(double)*dim));

  if (!absolute) {
    for (j=0;j<dim;j++)
      variance(array[j],length,&av,&sigmax[j]);
  }

  rnd_init(iseed);

  for (i=0;i<10000;i++) rnd_1279();

  for (j=0;j<dim;j++) {
    if (!cgaussian)
      equidistri(sigmax[j],j);
    else
      gauss(sigmax[j],j);
  }

  if (!stout) {
    fout=fopen(outfile,"w");
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Opened %s for writing\n",outfile);
    for (i=0;i<length;i++) {
      for (j=0;j<dim-1;j++)
	fprintf(fout,"%e ",array[j][i]);
      fprintf(fout,"%e\n",array[dim-1][i]);
    }
    fclose(fout);
  }
  else {
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Writing to stdout\n");
    for (i=0;i<length;i++) {
      for (j=0;j<dim-1;j++)
	fprintf(stdout,"%e ",array[j][i]);
      fprintf(stdout,"%e\n",array[dim-1][i]);
    }
  }

  for (i=0;i<dim;i++)
    free(array[i]);
  free(array);
  free(sigmax);
  if (outfile != NULL)
    free(outfile);
  if (infile != NULL)
    free(infile);
  if (column != NULL)
    free(column);

  return 0;
}
Example #5
0
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;
}
Example #6
0
int main(int argc,char **argv)
{
  char stdi=0,*done,alldone;
  int i;
  long n;
  long maxlength;
  double min,max;
  FILE *file;
  
  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,&column,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,".ros");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.ros");
    }
  }
  test_outfile(outfile);

  series=(double*)get_series(infile,&length,exclude,column,verbosity);
  rescale_data(series,length,&min,&max);

  if (epsset)
    eps0 /= max;

  check_alloc(list=(long*)malloc(length*sizeof(long)));
  check_alloc(lyap=(double*)malloc((steps+1)*sizeof(double)));
  check_alloc(found=(long*)malloc((steps+1)*sizeof(long)));
  check_alloc(done=(char*)malloc(length));

  for (i=0;i<=steps;i++) {
    lyap[i]=0.0;
    found[i]=0;
  }
  for (i=0;i<length;i++)
    done[i]=0;
  
  maxlength=length-delay*(dim-1)-steps-1-mindist;
  alldone=0;
  file=fopen(outfile,"w");
  if (verbosity&VER_INPUT)
    fprintf(stderr,"Opened %s for writing\n",outfile);
  for (eps=eps0;!alldone;eps*=1.1) {
    epsinv=1.0/eps;
    put_in_boxes();
    alldone=1;
    for (n=0;n<=maxlength;n++) {
      if (!done[n])
	done[n]=make_iterate(n);
      alldone &= done[n];
    }
    if (verbosity&VER_USR1)
      fprintf(stderr,"epsilon: %e already found: %ld\n",eps*max,found[0]);
  } 
  for (i=0;i<=steps;i++)
    if (found[i])
      fprintf(file,"%d %e\n",i,lyap[i]/found[i]/2.0);
  fclose(file);

  return 0;
}
Example #7
0
int main(int argc,char **argv)
{
  char stdi=0;
  FILE *file;
  double min,max;
  double av,varianz;
  long i,n;
    
  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,".res");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.res");
    }
  }
  if (!stdo)
    test_outfile(outfile);

  if (xmin >= xmax) {
    fprintf(stderr,"Choosing the minimum larger or equal the maximum\n"
	    "makes no sense. Exiting!\n");
    exit(RESCALE__WRONG_INTERVAL);
  }

  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);

  for (n=0;n<dim;n++) {
    variance(series[n],length,&av,&varianz);
    if (ddof!=0)
      varianz *= sqrt((double) length/(length-ddof));
    
    if (set_av)
      for (i=0;i<length;i++)
	series[n][i] -= av;

    if (set_var)
      for (i=0;i<length;i++)
	series[n][i] /= varianz;
  
    if (!set_var && !set_av) {
      rescale_data(series[n],length,&min,&max);
      for (i=0;i<length;i++)
	series[n][i]=series[n][i]*(xmax-xmin)+xmin;
    }
  }

  if (stdo) {
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Writing to stdout\n");
    for (i=0;i<length;i++) {
      fprintf(stdout,"%e",series[0][i]);
      for (n=1;n<dim;n++)
	fprintf(stdout," %e",series[n][i]);
      fprintf(stdout,"\n");
    }
  }
  else {
    file=fopen(outfile,"w");
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Opened %s for writing\n",outfile);
    for (i=0;i<length;i++) {
      fprintf(file,"%e",series[0][i]);
      for (n=1;n<dim;n++)
	fprintf(file," %e",series[n][i]);
      fprintf(file,"\n");
    }
    fclose(file);
  }

  return 0;
}
Example #8
0
int main(int argc,char **argv)
{
  char stdi=0,done;
  long i,j,hdim,actfound;
  unsigned long count=1;
  double *swap,*newcast,maxinterval,*min,*interval,dummy,epsilon0;
  FILE *file=NULL;
  
  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,".lzr");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.lzr");
    }
  }
  if (!onscreen)
    test_outfile(outfile);
  
  hdim=(embed-1)*DELAY+1;
  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);

  dim1=dim-1;

  check_alloc(min=(double*)malloc(sizeof(double)*dim));
  check_alloc(interval=(double*)malloc(sizeof(double)*dim));
  check_alloc(var=(double*)malloc(sizeof(double)*dim));

  maxinterval=0.0;

  for (i=0;i<dim;i++) {
    rescale_data(series[i],LENGTH,&min[i],&interval[i]);
    variance(series[i],LENGTH,&dummy,&var[i]);
    if (interval[i] > maxinterval)
      maxinterval=interval[i];
  }

  if (epsset)
    EPS0 /= maxinterval;
    
  check_alloc(cast=(double**)malloc(sizeof(double*)*hdim));
  for (i=0;i<hdim;i++)
    check_alloc(cast[i]=(double*)malloc(sizeof(double)*dim));
  check_alloc(newcast=(double*)malloc(sizeof(double)*dim));
  check_alloc(newav=(double*)malloc(sizeof(double)*dim));
    
  check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
  check_alloc(found=(long*)malloc(sizeof(long)*LENGTH));
  check_alloc(abstand=(double*)malloc(sizeof(double)*LENGTH));
  check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
  for (i=0;i<NMAX;i++)
    check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
  
  check_alloc(vec=(double*)malloc(sizeof(double)*dim));
  check_alloc(hsum=(double*)malloc(sizeof(double)*dim));
  check_alloc(mat=(double**)malloc(sizeof(double*)*dim));
  for (i=0;i<dim;i++) {
    check_alloc(mat[i]=(double*)malloc(sizeof(double)*dim));
  }

  for (j=0;j<dim;j++)
    for (i=0;i<hdim;i++)
      cast[i][j]=series[j][LENGTH-hdim+i];

  indexes=make_multi_index(dim,embed,DELAY);
  
  if (!onscreen) {
    file=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");
  }

  rnd_init(seed);

  epsilon0=EPS0/EPSF;

  if (setnoise) 
    Q /= 100.0;

  for (i=0;i<FLENGTH;i++) {
    done=0;
    if (setsort)
      epsilon= epsilon0/((double)count*EPSF);
    else
      epsilon=epsilon0;
    while (!done) {
      epsilon*=EPSF;
      put_in_boxes();
      actfound=hfind_neighbors();
      if (actfound >= MINN) {
	if (setsort) {
	  epsilon0 += epsilon;
	  count++;
	  sort(actfound);
	  actfound=MINN;
	}
	make_zeroth(actfound,newcast);
	if (onscreen) {
	  for (j=0;j<dim-1;j++)
	    printf("%e ",newcast[j]*interval[j]+min[j]);
	  printf("%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
	  fflush(stdout);
	}
	else {
	  for (j=0;j<dim-1;j++)
	    fprintf(file,"%e ",newcast[j]*interval[j]+min[j]);
	  fprintf(file,"%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
	  fflush(file);
	}
	done=1;
	swap=cast[0];
	for (j=0;j<hdim-1;j++)
	  cast[j]=cast[j+1];
	cast[hdim-1]=swap;
	for (j=0;j<dim;j++)
	  cast[hdim-1][j]=newcast[j];
      }
    }
  }
  if (!onscreen)
    fclose(file);
  
  if (outfile != NULL)
    free(outfile);
  for (i=0;i<dim;i++)
    free(mat[i]);
  free(mat);
  for (i=0;i<hdim;i++)
    free(cast[i]);
  free(cast);
  free(newcast);
  free(found);
  free(list);
  for (i=0;i<NMAX;i++)
    free(box[i]);
  free(box);
  free(vec);
  free(newav);
  for (i=0;i<dim;i++)
    free(series[i]);
  free(series);

  return 0;
}
Example #9
0
int main(int argc,char **argv)
{
  char stdi=0;
  FILE *file=NULL;
  double min,inter=0.0,ind_inter,epsilon,av,ind_var;
  char *nearest,alldone;
  long i;
  unsigned int dim,emb;
  unsigned long donesofar;

  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,".fnn");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.fnn");
    }
  }
  if (!stdo)
    test_outfile(outfile);

  if (column == NULL)
    series=(double**)get_multi_series(infile,&length,exclude,&comp,"",dimset,
				      verbosity);
  else
    series=(double**)get_multi_series(infile,&length,exclude,&comp,column,
				      dimset,verbosity);

  for (i=0;i<comp;i++) {
    rescale_data(series[i],length,&min,&ind_inter);
    variance(series[i],length,&av,&ind_var);
    if (i == 0) {
      varianz=ind_var;
      inter=ind_inter;
    }
    else {
      varianz=(varianz>ind_var)?ind_var:varianz;
      inter=(inter<ind_inter)?ind_inter:inter;
    }
  }

  check_alloc(list=(long*)malloc(sizeof(long)*length));
  check_alloc(nearest=(char*)malloc(length));
  check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
  for (i=0;i<BOX;i++)
    check_alloc(box[i]=(long*)malloc(sizeof(long)*BOX));

  if (!stdo) {
    file=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");
  }
  check_alloc(vcomp=(unsigned int*)malloc(sizeof(int)*(maxdim)));
  check_alloc(vemb=(unsigned int*)malloc(sizeof(int)*(maxdim)));
  for (i=0;i<maxdim;i++) {
    if (comp == 1) {
      vcomp[i]=0;
      vemb[i]=i;
    }
    else {
      vcomp[i]=i%comp;
      vemb[i]=(i/comp)*delay;
    }
  }
  for (emb=minemb;emb<=maxemb;emb++) {
    dim=emb*comp-1;
    epsilon=eps0;
    toolarge=0;
    alldone=0;
    donesofar=0;
    aveps=0.0;
    vareps=0.0;
    for (i=0;i<length;i++)
      nearest[i]=0;
    if (verbosity&VER_USR1)
      fprintf(stderr,"Start for dimension=%u\n",dim+1);
    while (!alldone && (epsilon < 2.*varianz/rt)) {
      alldone=1;
      mmb(vcomp[dim],vemb[dim],epsilon);
      for (i=0;i<length-maxemb*delay;i++)
	if (!nearest[i]) {
	  nearest[i]=find_nearest(i,dim,epsilon);
	  alldone &= nearest[i];
	  donesofar += (unsigned long)nearest[i];
	}
      if (verbosity&VER_USR1)
	fprintf(stderr,"Found %lu up to epsilon=%e\n",donesofar,epsilon*inter);
      epsilon*=sqrt(2.0);
      if (!donesofar)
	eps0=epsilon;
    }
    if (donesofar == 0) {
      fprintf(stderr,"Not enough points found!\n");
      exit(FALSE_NEAREST_NOT_ENOUGH_POINTS);
    }
    aveps *= (1./(double)donesofar);
    vareps *= (1./(double)donesofar);
    if (stdo) {
      fprintf(stdout,"%u %e %e %e\n",dim+1,(double)toolarge/(double)donesofar,
	      aveps*inter,sqrt(vareps)*inter);
      fflush(stdout);
    }
    else {
      fprintf(file,"%u %e %e %e\n",dim+1,(double)toolarge/(double)donesofar,
	      aveps*inter,sqrt(vareps)*inter);
      fflush(file);
    }
  }
  if (!stdo)
    fclose(file);

  if (infile != NULL)
    free(infile);
  if (outfile != NULL)
    free(outfile);
  free(series);
  free(list);
  free(nearest);
  for (i=0;i<BOX;i++)
    free(box[i]);
  free(box);

  return 0;
}
Example #10
0
int main(int argc,char **argv)
{
  unsigned int outstrlen=0;
  int i,j,k,n;
  unsigned int offset,wdim=1,alldim;
  unsigned int has_future,*future,**useries,h;
  unsigned long count;
  long *segment;
  char *outstring,*infile,*wcol,*use_array;
  double **series,*sermin,*serinter,**dfuture;
  ptree *root;
  FILE *fout;

  if (scan_help(argc,argv))
    show_options(argv[0]);

  for (i=0;i<argc;i++) {
    outstrlen += strlen(argv[i]);
    outstrlen++;
  }
  check_alloc(outstring=(char*)malloc(sizeof(char)*(outstrlen+8)));
  sprintf(outstring,"#Prog: ");
  offset=7;
  for (i=0;i<argc;i++) {
    sprintf(outstring+offset,"%s ",argv[i]);
    offset += (strlen(argv[i])+1);
  }

  scan_options(argc,argv);
 
  if (!compdimset) 
    compdim=pars.DIM;
  else
    if (compdim > pars.DIM) {
      fprintf(stderr,"-C value larger -m Value.\n");
      exit(LANGEVIN_MAIN_C_TOO_LARGE);
    }

  infile=search_datafile(argc,argv,NULL,VERBOSITY);
  if (infile == NULL) {
    fprintf(stderr,"No input datafile found.\n");
    exit(LANGEVIN_MAIN_NO_INPUTFILE);
  }
  
  if (OUTFILE == NULL) {
    check_alloc(OUTFILE=(char*)calloc(strlen(infile)+5,(size_t)1));
    sprintf(OUTFILE,"%s.pru",infile);
  }
  OUTFILE=test_outfile(OUTFILE);

  if (COLUMN == NULL)
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),"",dimset,VERBOSITY);
  else
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),COLUMN,dimset,VERBOSITY);

  check_alloc(sermin=malloc(sizeof(double)*pars.DIM));
  check_alloc(serinter=malloc(sizeof(double)*pars.DIM));
  rescale_data(series,pars,sermin,serinter);

  alldim=pars.DIM;
  pars.DIM=compdim;

  check_alloc(useries=(unsigned int**)malloc(sizeof(int*)*pars.DIM));
  check_alloc(PART=(unsigned int*)malloc(sizeof(int)*pars.DIM));
  if (SPART == NULL)
    for (i=0;i<pars.DIM;i++)
      PART[i]=2;
  else
    set_part(PART,SPART,pars);

  for (i=0;i<pars.DIM;i++) {
    check_alloc(useries[i]=(unsigned int*)malloc(sizeof(int)*pars.LENGTH));
    for (j=0;j<pars.LENGTH;j++) {
      h=(unsigned int)((series[i][j]*(double)PART[i])/serinter[i]);
      useries[i][j]=((h<PART[i])?h:(PART[i]-1));
    }
  }

  check_alloc(future=(unsigned int*)malloc(sizeof(int)*pars.LENGTH));
  if (WHICHFUTURE>0) {
    check_alloc(wcol=(char*)calloc((size_t)10,(size_t)1));
    sprintf(wcol,"%u",WHICHFUTURE);
    dfuture=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				       &wdim,wcol,1,0L);
    for (i=0;i<pars.LENGTH-1;i++) {
      future[i] = (dfuture[0][i]>0.0);
    }
    future[pars.LENGTH-1]=1;
    free(dfuture);
  }
  else {
    for (i=0;i<pars.LENGTH;i++)
      future[i]=1;
  }
  root=make_ptree(PART[0]);

  count=0;
  check_alloc(segment=(long*)malloc(sizeof(long)*pars.LENGTH));
  for (i=0;i<pars.LENGTH;i += ODEPTH) {
    has_future=1;
    for (j=0;j<ODEPTH;j++)
      has_future &= future[i+j];
    if (has_future) {
      fill_tree(root,useries,pars,i,0,DEPTH,PART);
    }
    segment[count++]=i;
  }

  if (scrambleset)
    lscramble(segment,count);

  check_alloc(use_array=(char*)malloc(sizeof(char)*pars.LENGTH));
  for (i=0;i<count;i++) {
    n=segment[i];
    has_future=1;
    for (j=0;j<ODEPTH;j++)
      has_future &= future[n+j];
    if (has_future) {
      h=read_tree(root,useries,pars,n,0,DEPTH);
      if (h <= MAXOUT)
	use_array[i]=1;
      else 
	use_array[i]=0;
    }
    else 
      use_array[i]=2;
  }
  pars.DIM=alldim;

  fout=fopen(OUTFILE,"w");
  fprintf(fout,"%s\n",outstring);
  fprintf(fout,"#Content: ");
  for (i=0;i<pars.DIM;i++)
    fprintf(fout,"x%d ",i+1);
  fprintf(fout,"future time_index\n");
  fflush(fout);

  for (i=0;i<count;i++) {
    n=segment[i];
    if (use_array[i] == 1) {
      for (j=0;j<(ODEPTH-1);j++) {
	for (k=0;k<pars.DIM;k++)
	  fprintf(fout,"%f ",series[k][n+j]+sermin[k]);
	fprintf(fout,"1 %u\n",n+j);
      }
      for (k=0;k<pars.DIM;k++)
	fprintf(fout,"%f ",series[k][n+ODEPTH-1]+sermin[k]);
      if (i<(count-1)) {
	if (segment[i+1] == (n+ODEPTH) && use_array[i+1])
	  fprintf(fout,"1 %u\n",n+ODEPTH-1);
	else 
	  fprintf(fout,"0 %u\n",n+ODEPTH-1);
      }
      else 
	fprintf(fout,"0 %u\n",n+ODEPTH-1);
    }
  }

  fclose(fout);
}
Example #11
0
int main(int argc,char **argv)
{
  char stdi=0,done;
  long i,j,hdim,actfound;
  double maxinterval,*swap,*newcast;
  FILE *file=NULL;
  
  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)+6,(size_t)1));
      strcpy(outfile,infile);
      strcat(outfile,".cast");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)11,(size_t)1));
      strcpy(outfile,"stdin.cast");
    }
  }
  if (!onscreen)
    test_outfile(outfile);
  
  hdim=(embed-1)*DELAY+1;
  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(min=(double*)malloc(sizeof(double)*dim));
  check_alloc(interval=(double*)malloc(sizeof(double)*dim));
  dim1=dim-1;
  maxinterval=0.0;
  for (i=0;i<dim;i++) {
    rescale_data(series[i],LENGTH,&min[i],&interval[i]);
    if (interval[i] > maxinterval)
      maxinterval=interval[i];
  }
  
  check_alloc(cast=(double**)malloc(sizeof(double*)*hdim));
  for (i=0;i<hdim;i++)
    check_alloc(cast[i]=(double*)malloc(sizeof(double)*dim));
  check_alloc(newcast=(double*)malloc(sizeof(double)*dim));
    
  check_alloc(list=(long*)malloc(sizeof(long)*LENGTH));
  check_alloc(found=(long*)malloc(sizeof(long)*LENGTH));
  check_alloc(box=(long**)malloc(sizeof(long*)*NMAX));
  for (i=0;i<NMAX;i++)
    check_alloc(box[i]=(long*)malloc(sizeof(long)*NMAX));
  
  check_alloc(localav=(double*)malloc(sizeof(double)*dim*embed));
  check_alloc(foreav=(double*)malloc(sizeof(double)*dim));
  check_alloc(vec=(double*)malloc(sizeof(double)*dim*embed));
  check_alloc(mat=(double**)malloc(sizeof(double*)*dim*embed));
  for (i=0;i<dim*embed;i++)
    check_alloc(mat[i]=(double*)malloc(sizeof(double)*dim*embed));

  if (epsset)
    EPS0 /= maxinterval;

  for (j=0;j<dim;j++)
    for (i=0;i<hdim;i++)
      cast[i][j]=series[j][LENGTH-hdim+i];
  
  if (!onscreen) {
    file=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");
  }

  for (i=0;i<FLENGTH;i++) {
    done=0;
    epsilon=EPS0/EPSF;
    while (!done) {
      epsilon*=EPSF;
      put_in_boxes();
      actfound=hfind_neighbors();
      if (actfound >= MINN) {
	if (!do_zeroth)
	  make_fit(actfound,newcast);
	else
	  make_zeroth(actfound,newcast);
	if (onscreen) {
	  for (j=0;j<dim-1;j++)
	    printf("%e ",newcast[j]*interval[j]+min[j]);
	  printf("%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
	  fflush(stdout);
	}
	else {
	  for (j=0;j<dim-1;j++)
	    fprintf(file,"%e ",newcast[j]*interval[j]+min[j]);
	  fprintf(file,"%e\n",newcast[dim-1]*interval[dim-1]+min[dim-1]);
	  fflush(file);
	}
	done=1;
	for (j=0;j<dim;j++) {
	  if ((newcast[j] > 2.0) || (newcast[j] < -1.0)) {
	    fprintf(stderr,"Forecast failed. Escaping data region!\n");
	    exit(NSTEP__ESCAPE_REGION);
	  }
	}

	swap=cast[0];
	for (j=0;j<hdim-1;j++)
	  cast[j]=cast[j+1];
	cast[hdim-1]=swap;
	for (j=0;j<dim;j++)
	  cast[hdim-1][j]=newcast[j];
      }
    }
  }
  if (!onscreen)
    fclose(file);
  
  if (outfile != NULL)
    free(outfile);
  for (i=0;i<embed*dim;i++)
    free(mat[i]);
  free(mat);
  for (i=0;i<hdim;i++)
    free(cast[i]);
  free(cast);
  free(newcast);
  free(found);
  free(list);
  for (i=0;i<NMAX;i++)
    free(box[i]);
  free(box);
  free(vec);
  free(localav);
  free(foreav);
  free(min);
  free(interval);
  for (i=0;i<dim;i++)
    free(series[i]);
  free(series);

  return 0;
}
Example #12
0
int main(int argc,char **argv)
{
  char stdi=0;
  unsigned long i,j;
  double x,norm,size=1.0,size2=1.0;
  double min,max;
  double *series;
  double average,var;
  long *box;
  FILE *fout;

  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,&column,verbosity);
  if (infile == NULL)
    stdi=1;

  if (outfile == NULL) {
    if (!stdi) {
      check_alloc(outfile=(char*)calloc(strlen(infile)+5,1));
      strcpy(outfile,infile);
      strcat(outfile,".his");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,1));
      strcpy(outfile,"stdin.his");
    }
  }
  if (!my_stdout)
    test_outfile(outfile);

  series=(double*)get_series(infile,&length,exclude,column,verbosity);
  variance(series,length,&average,&var);

  min=max=series[0];
  for (i=1;i<length;i++) {
    if (series[i] < min) min=series[i];
    else if (series[i] > max) max=series[i];
  }
  max -= min;

  for (i=0;i<length;i++)
    series[i]=(series[i]-min);

  if (base > 0) {
    check_alloc(box=(long*)malloc(sizeof(long)*base));
    for (i=0;i<base;i++)
      box[i]=0;
    size=1./base;
    size2=(1.0-size/2.0)*max;
    for (i=0;i<length;i++) {
      if (series[i] > size2)
	series[i]=size2;
      j=(long)(series[i]*base/max);
      box[j]++;
    }
  }

  if (!density)
    norm=1.0/(double)length;
  else
    norm=1.0/(double)length*(double)base/max;

  if (!my_stdout) {
    fout=fopen(outfile,"w");
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Opened %s for writing\n",outfile);
    fprintf(fout,"#interval of data: [%e:%e]\n",min,max+min);
    fprintf(fout,"#average= %e\n",average);
    fprintf(fout,"#standard deviation= %e\n",var);
    for (i=0;i<base;i++) {
      x=(double)(i*size);
      fprintf(fout,"%e %e\n",(x+size/2.0)*max+min,(double)box[i]*norm);
    }
    fclose(fout);
  }
  else {
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Writing to stdout\n");
    fprintf(stdout,"#interval of data: [%e:%e]\n",min,max+min);
    fprintf(stdout,"#average= %e\n",average);
    fprintf(stdout,"#standard deviation= %e\n",var);
    for (i=0;i<base;i++) {
      x=(double)(i*size);
      fprintf(stdout,"%e %e\n",(x+size/2.0)*max+min,(double)box[i]*norm);
      fflush(stdout);
    }
  }
  return 0;
}
Example #13
0
int main(int argc,char** argv)
{
  char stdi=0;
  long i;
  FILE *fout=NULL;
  
  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,&column,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,".cor");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.cor");
    }
  }
  if (!stout)
    test_outfile(outfile);

  array=(double*)get_series(infile,&length,exclude,column,verbosity);

  if (tau >= length)
    tau=length-1;

  variance(array,length,&av,&var);

  if (normalize) {
    for (i=0;i<length;i++)
      array[i] -= av;
  }

  if (!stout) {
    fout=fopen(outfile,"w");
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Opened %s for writing\n",outfile);
    fprintf(fout,"# average=%e\n",av);
    fprintf(fout,"# standard deviation=%e\n",var);
  }
  else {
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Writing to stdout\n");
    fprintf(stdout,"# average=%e\n",av);
    fprintf(stdout,"# standard deviation=%e\n",var);
  }
  if (normalize)
    var *= var;
  else
    var=1.0;

  for (i=0;i<=tau;i++)
    if (!stout) {
      fprintf(fout,"%ld %e\n",i,corr(i)/var);
      fflush(fout);
    }
    else {
      fprintf(stdout,"%ld %e\n",i,corr(i)/var);
      fflush(stdout);
    }
  if (!stout)
    fclose(fout);

  if (outfile != NULL)
    free(outfile);
  if (infile != NULL)
    free(infile);
  free(array);

  return 0;
}
Example #14
0
int main(int argc,char **argv)
{
  unsigned int outstrlen=0,offset,hdim,wdim=1;
  int i,j,minnadd;
  unsigned int *future,has_future;
  long li,lj,n,refn;
  char *outstring,*infile,*wcol;
  char ntoolarge;
  double **series,**rseries,*sermin,*serinter,**dfuture,max,**cast,sweights;
  struct sfound sf;
  FILE *fout;

  if (scan_help(argc,argv))
    show_options(argv[0]);

  for (i=0;i<argc;i++) {
    outstrlen += strlen(argv[i]);
    outstrlen++;
  }
  check_alloc(outstring=(char*)malloc(sizeof(char)*(outstrlen+8)));
  sprintf(outstring,"#Prog: ");
  offset=7;
  for (i=0;i<argc;i++) {
    sprintf(outstring+offset,"%s ",argv[i]);
    offset += (strlen(argv[i])+1);
  }

  scan_options(argc,argv);

  infile=search_datafile(argc,argv,NULL,VERBOSITY);
  if (infile == NULL) {
    fprintf(stderr,"No input datafile found.\n");
    exit(LANGEVIN_MAIN_NO_INPUTFILE);
  }
  if (reference) {
    fout=fopen(refname,"r");
    if (fout == NULL) {
      fprintf(stderr,"No test point datafile found.\n");
      exit(LANGEVIN_MAIN_NO_INPUTFILE);
    }
    else
      fclose(fout);
  }

  if (OUTFILE == NULL) {
    check_alloc(OUTFILE=(char*)calloc(strlen(infile)+6,(size_t)1));
    sprintf(OUTFILE,"%s.osn",infile);
  }
  OUTFILE=test_outfile(OUTFILE);

  if (!reference) {
    refname=infile;
    ignore += EXCLUDE;
  }

  if (COLUMN == NULL)
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),"",dimset,VERBOSITY);
  else
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),COLUMN,dimset,VERBOSITY);

  switch (neighborkind) {
  case 0: {hdim=0;minnadd=1;}break;
  case 1: {hdim=0;minnadd=2;}break;
  case 2: {hdim=1;minnadd=3;}
  }
  if (!reference && (ignore < hdim) && (neighborkind == 2)) {
    ignore=hdim;
  }
  pars.hdim=hdim;

  if (COLUMN == NULL)
    rseries=(double**)get_multi_series(refname,&FLENGTH,ignore,
				       &(pars.DIM),"",dimset,VERBOSITY);
  else
    rseries=(double**)get_multi_series(refname,&FLENGTH,ignore,
				       &(pars.DIM),COLUMN,dimset,VERBOSITY);

  if (VERBOSITY)
    fprintf(stderr,"%lf\n",rseries[0][0]);

  check_alloc(sermin=malloc(sizeof(double)*pars.DIM));
  check_alloc(serinter=malloc(sizeof(double)*pars.DIM));
  rescale_data(series,pars,sermin,serinter);

  max=serinter[0];
  for (i=1;i<pars.DIM;i++)
    if (serinter[i] > max)
      max=serinter[i];

  for (i=0;i<pars.DIM;i++) {
    for (j=0;j<pars.LENGTH;j++)
      series[i][j] /= max;
    for (j=0;j<FLENGTH;j++)
      rseries[i][j]=(rseries[i][j]-sermin[i])/max;
  }
  if (pars.maxr > 0.0)
    pars.maxr /= max;

  check_alloc(future=(unsigned int*)malloc(sizeof(int)*pars.LENGTH));
  if (neighborkind == 0) {
    for (li=0;li<pars.LENGTH;li++)
      future[li]=1;
  }
  else {
    if (WHICHFUTURE>0) {
      check_alloc(wcol=(char*)calloc((size_t)10,(size_t)1));
      sprintf(wcol,"%u",WHICHFUTURE);
      dfuture=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
					 &wdim,wcol,1,0L);
      if (neighborkind == 2) {
	for (li=0;li<hdim;li++)
	  future[li]=0;
      }
      for (li=hdim;li<pars.LENGTH;li++) {
	has_future= (dfuture[0][li]>0.0);
	for (lj=1;lj<=hdim;lj++)
	  has_future &= (dfuture[0][li-lj]>0.0);
	future[li]=has_future;
      }
      free(dfuture);
    }
    else {
    for (li=0;li<pars.LENGTH-1;li++)
      future[li]=1;
    future[pars.LENGTH-1]=0;
      if (neighborkind == 2) {
    for (li=0;li<hdim;li++)
      future[li]=0;
      }
  }
    }

  check_alloc(sf.found=(unsigned long*)
	      malloc(sizeof(unsigned long)*pars.LENGTH));
  check_alloc(sf.distance=(double*)malloc(sizeof(double)*pars.LENGTH));
  check_alloc(sf.weight=(double*)malloc(sizeof(double)*pars.LENGTH));
  check_alloc(sf.count=(unsigned long*)malloc(sizeof(unsigned long)));
  check_alloc(sf.aveps=(double*)malloc(sizeof(double)));
  sf.count[0]=0;
  sf.aveps[0]=0.0;
  check_alloc(cast=(double**)malloc(sizeof(double*)*pars.DIM));
  for (i=0;i<pars.DIM;i++)
    check_alloc(cast[i]=(double*)malloc(sizeof(double)*(hdim+1)));

  pars.minminn=1;
  init_neighbor_search(series,pars,future);

  /* print program call and column labels */
  fout=fopen(OUTFILE,"w");
  fprintf(fout,"%s\n",outstring);
#if defined(MAXNORM)
  fprintf(fout,"#Norm: Maxnorm\n");
#else
  fprintf(fout,"#Norm: L2 Norm\n");
#endif
#if defined(WEIGHTS)
  if (pars.maxr > 0.0)
    fprintf(fout,"#Weights: on WFACT = %lf maxr = %lf\n",WFACT,pars.maxr*max);
  else
    fprintf(fout,"#Weights: on WFACT = %lf maxr not set\n",WFACT);
#else
  fprintf(fout,"#Weights: off\n");
#endif
  fprintf(fout,"#Content: ");
  for (i=0;i<pars.DIM;i++) {
    fprintf(fout,"x%d ",i+1);
  }
  for (i=0;i<pars.MINN;i++)
    fprintf(fout,"n%d ",i+1);
  fprintf(fout,"\n");
  fflush(fout);

  n=0;
  while (n < FLENGTH) {

    refn=n+ignore-EXCLUDE;
    if (!reference) {
      while (!future[refn]) {
        n++;
        refn++;
        if (n>=(pars.LENGTH-1))
          goto ntoolarge;
      }
    }
    for (j=0;j<pars.DIM;j++)
      cast[j][hdim]=rseries[j][n];

    /* search neighbors */
    if (!reference)
      pars.MINN += minnadd;

    search_neighbors(series,cast,pars,sf);

    if (!reference) {
      j=0;
      switch (neighborkind) {
      case 0: {
	for (i=0;i<pars.MINN;i++) {
	  if (sf.found[i] == refn) {
	    j++;
	  }
	  sf.found[i]=sf.found[j];
	  sf.distance[i]=sf.distance[j];
	  j++;
	}
      } break;
      case 1: {
	for (i=0;i<pars.MINN;i++) {
	  if ((sf.found[i] == refn) || (sf.found[i] == (refn-1))) {
	    j++;
	  }
	  sf.found[i]=sf.found[j];
	  sf.distance[i]=sf.distance[j];
	  j++;
	}
      } break;
      case 2: {
	for (i=0;i<pars.MINN;i++) {
	  if ((sf.found[i] == refn) || (sf.found[i] == (refn-1)) ||
	      (sf.found[i] == (refn+1))) {
	    j++;
	  }
	  sf.found[i]=sf.found[j];
	  sf.distance[i]=sf.distance[j];
	  j++;
	}
      } break;
      }
      pars.MINN -= minnadd;
    }

    /* print coordinate */
    for (i=0;i<pars.DIM;i++)
      fprintf(fout,"%e ",cast[i][hdim]*max+sermin[i]);

    /* print neighbors */
    for (i=0;i<pars.MINN;i++)
      fprintf(fout,"%lu ",sf.found[i]+1);
    fprintf(fout,"\n");
    fflush(fout);
    n++;
  }
 ntoolarge: fclose(fout);

  return 0;
}
Example #15
0
int main(int argc,char **argv)
{
  unsigned int outstrlen=0,offset,hdim,wdim=1;
  int i,j;
  unsigned int *future,has_future;
  unsigned long count;
  long li,lj;
  char *outstring,*infile,*fieldfile,*wcol;
  double **series,*sermin,*serinter,**dfuture,max,**cast,sweights;
  double *new,*drift,**diffusion,**gamma;
  struct sfound sf;
  FILE *fout,*ffield;

  if (scan_help(argc,argv))
    show_options(argv[0]);

  for (i=0;i<argc;i++) {
    outstrlen += strlen(argv[i]);
    outstrlen++;
  }
  check_alloc(outstring=(char*)malloc(sizeof(char)*(outstrlen+8)));
  sprintf(outstring,"#Prog: ");
  offset=7;
  for (i=0;i<argc;i++) {
    sprintf(outstring+offset,"%s ",argv[i]);
    offset += (strlen(argv[i])+1);
  }

  scan_options(argc,argv);

  if (pars.MINN < (pars.DIM+1)) {
    fprintf(stderr,"Too few neighbors. System is underdetermined.\n");
    exit(LANGEVIN_MAIN_TOO_FEW_MINN);
  }

  infile=search_datafile(argc,argv,NULL,VERBOSITY);
  if (infile == NULL) {
    fprintf(stderr,"No input datafile found.\n");
    exit(LANGEVIN_MAIN_NO_INPUTFILE);
  }
  
  if (OUTFILE == NULL) {
    check_alloc(OUTFILE=(char*)calloc(strlen(infile)+6,(size_t)1));
    sprintf(OUTFILE,"%s.lang",infile);
    check_alloc(fieldfile=(char*)calloc(strlen(infile)+12,(size_t)1));
    sprintf(fieldfile,"%s.lang.field",infile);
  }
  else {
    check_alloc(fieldfile=(char*)calloc(strlen(OUTFILE)+7,(size_t)1));
    sprintf(fieldfile,"%s.field",OUTFILE);
  }
  OUTFILE=test_outfile(OUTFILE);
  if (!nofields)
    fieldfile=test_outfile(fieldfile);

  if (COLUMN == NULL)
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),"",dimset,VERBOSITY);
  else
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),COLUMN,dimset,VERBOSITY);

  init_noise(pars,seed);

  if (INNOISE > 0.0)
    for (li=0;li<pars.LENGTH;li++)
      for (i=0;i<pars.DIM;i++)
	series[i][li] += 2.0*INNOISE*
	  ((double)rnd_long()/(double)ULONG_MAX-0.5);

  check_alloc(sermin=malloc(sizeof(double)*pars.DIM));
  check_alloc(serinter=malloc(sizeof(double)*pars.DIM));
  //  rescale_data(series,pars,sermin,serinter);

  for (i=0;i<pars.DIM;i++) {
    sermin[i]=0.0;
    serinter[i]=1.0;
  }
  max=serinter[0];
  for (i=1;i<pars.DIM;i++)
    if (serinter[i] > max)
      max=serinter[i];

  for (i=0;i<pars.DIM;i++) {
    for (j=0;j<pars.LENGTH;j++)
      series[i][j] /= max;
  }
  if (pars.maxr > 0.0)
    pars.maxr /= max;

  hdim=1;
  pars.hdim=hdim;

  check_alloc(future=(unsigned int*)malloc(sizeof(int)*pars.LENGTH));
  if (WHICHFUTURE>0) {
    check_alloc(wcol=(char*)calloc((size_t)10,(size_t)1));
    sprintf(wcol,"%u",WHICHFUTURE);
    dfuture=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				       &wdim,wcol,1,0L);
    for (li=0;li<hdim;li++)
      future[li]=0;
    for (li=hdim;li<pars.LENGTH-1;li++) {
      has_future= (dfuture[0][li]>0.0);
      for (lj=1;lj<=hdim;lj++)
	has_future &= (dfuture[0][li-lj]>0.0);
      future[li]=has_future;
    }
    future[pars.LENGTH-1]=0;
    free(dfuture);
  }
  else {
    for (li=0;li<pars.LENGTH-1;li++)
      future[li]=1;
    future[pars.LENGTH-1]=0;
    for (li=0;li<hdim;li++)
      future[li]=0;
  }

  check_alloc(sf.found=(unsigned long*)
	      malloc(sizeof(unsigned long)*pars.LENGTH));
  check_alloc(sf.distance=(double*)malloc(sizeof(double)*pars.LENGTH));
  check_alloc(sf.weight=(double*)malloc(sizeof(double)*pars.LENGTH));
  check_alloc(sf.count=(unsigned long*)malloc(sizeof(unsigned long)));
  check_alloc(sf.aveps=(double*)malloc(sizeof(double)));
  sf.count[0]=0;
  sf.aveps[0]=0.0;
  check_alloc(cast=(double**)malloc(sizeof(double*)*pars.DIM));
  for (i=0;i<pars.DIM;i++)
    check_alloc(cast[i]=(double*)malloc(sizeof(double)*(hdim+1)));
  check_alloc(new=(double*)malloc(sizeof(double)*pars.DIM));

  check_alloc(drift=(double*)malloc(sizeof(double)*pars.DIM));
  check_alloc(diffusion=(double**)malloc(sizeof(double*)*pars.DIM));
  check_alloc(gamma=(double**)malloc(sizeof(double*)*pars.DIM));
  for (i=0;i<pars.DIM;i++) {
    check_alloc(diffusion[i]=(double*)malloc(sizeof(double)*pars.DIM));
    check_alloc(gamma[i]=(double*)malloc(sizeof(double)*pars.DIM));
  }

  for (i=0;i<=hdim;i++)
    for (j=0;j<pars.DIM;j++)
      cast[j][hdim-i]=series[j][pars.LENGTH-1-i];

  pars.minminn=(unsigned long)((3.*pars.DIM+3.)/2.0+0.5);
  init_neighbor_search_circ(series,pars,future);

  /* print program call and column labels */
  fout=fopen(OUTFILE,"w");
  fprintf(fout,"%s\n",outstring);
#if defined(MAXNORM)
  fprintf(fout,"#Norm: Maxnorm\n");
#else
  fprintf(fout,"#Norm: L2 Norm\n");
#endif
#if defined(WEIGHTS)
  if (pars.maxr > 0.0)
    fprintf(fout,"#Weights: on WFACT = %lf maxr = %lf\n",WFACT,pars.maxr*max);
  else
    fprintf(fout,"#Weights: on WFACT = %lf maxr not set\n",WFACT);
#else
  fprintf(fout,"#Weights: off\n");
#endif
  fprintf(fout,"#Content: ");
  for (i=0;i<pars.DIM;i++)
    fprintf(fout,"x%d ",i+1);
  fprintf(fout,"\n");
  fflush(fout);

  if (!nofields) {
    ffield=fopen(fieldfile,"w");
    fprintf(ffield,"%s\n",outstring);
#if defined(MAXNORM)
    fprintf(ffield,"#Norm: Maxnorm\n");
#else
    fprintf(ffield,"#Norm: L2 Norm\n");
#endif
#if defined(WEIGHTS)
  if (pars.maxr > 0.0)
    fprintf(ffield,"#Weights: on WFACT = %lf maxr = %lf\n",WFACT,pars.maxr*max);
  else
    fprintf(ffield,"#Weights: on WFACT = %lf maxr not set\n",WFACT);
#else
    fprintf(ffield,"#Weights: off\n");
#endif
    fprintf(ffield,"#Content: ");
    for (i=0;i<pars.DIM;i++)
      for (j=0;j<pars.EMB;j++)
	if (pars.EMB>1) {
	  fprintf(ffield,"x%d_%d ",i+1,j+1);
	}
	else {
	  fprintf(ffield,"x%d ",i+1);
	}
    for (i=0;i<pars.DIM;i++)
      fprintf(ffield,"f%d ",i+1);
    for (i=0;i<pars.DIM;i++)
      for (j=0;j<pars.DIM;j++)
	fprintf(ffield,"g_%d_%d ",i+1,j+1);
    for (i=0;i<pars.DIM;i++)
      for (j=0;j<=i;j++)
	fprintf(ffield,"K_%d_%d ",i+1,j+1);
#if defined(WEIGHTS)
    fprintf(ffield,"distance sweights\n");
#else
    fprintf(ffield,"distance\n");
#endif
    fflush(ffield);
  }

  count=0;
  while (count <FLENGTH) {
    search_neighbors_circ(series,cast,pars,sf);
    get_fields_no_circ(series,pars,sf,drift,gamma,diffusion,cast);
    make_cast_no_circ(cast,new,pars,drift,gamma,diffusion);

    for (i=0;i<pars.DIM;i++)
      fprintf(fout,"%lf ",new[i]*max+sermin[i]);
    fprintf(fout,"\n");
    fflush(fout);

    if (!nofields) {
      for (i=0;i<pars.DIM;i++)
	for (j=0;j<pars.EMB;j++)
	  fprintf(ffield,"%e ",cast[i][hdim-j*pars.DELAY]*max+sermin[i]);
      for (i=0;i<pars.DIM;i++)
	fprintf(ffield,"%e ",drift[i]*max);
      for (i=0;i<pars.DIM;i++)
	for (j=0;j<pars.DIM;j++)
	  fprintf(ffield,"%e ",gamma[i][j]);
      for (i=0;i<pars.DIM;i++)
	for (j=0;j<=i;j++)
	  fprintf(ffield,"%e ",diffusion[i][j]*max);
#if defined(WEIGHTS)
      sweights=sf.weight[0];
      for (i=1;i<pars.MINN;i++)
	sweights += sf.weight[i];
      fprintf(ffield,"%e %lf\n",sf.distance[pars.MINN-1]*max,sweights);
#else
      fprintf(ffield,"%e\n",sf.distance[pars.MINN-1]*max);
#endif
      fflush(ffield);
    }
    for (i=1;i<=hdim;i++)
      for (j=0;j<pars.DIM;j++)
	cast[j][i-1]=cast[j][i];
    for (i=0;i<pars.DIM;i++)
      cast[i][hdim]=new[i];
    count++;
  }
  fclose(fout);
  if (!nofields)
    fclose(ffield);

  return 0;
}
Example #16
0
int main(int argc,char** argv)
{
  char stdi=0;
  long i;
  unsigned int dummy=2;
  FILE *fout=NULL;
  double **both;
  double av1,var1,av2,var2;

  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,0L,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,".ccr");
    }
    else {
      check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
      strcpy(outfile,"stdin.ccr");
    }
  }
  if (!stout)
    test_outfile(outfile);

  if (columns == NULL)
    both=(double**)get_multi_series(infile,&length,exclude,&dummy,"",(char)1,
				    verbosity);
  else
    both=(double**)get_multi_series(infile,&length,exclude,&dummy,columns,
				    (char)1,verbosity);
    
  array1=both[0];
  array2=both[1];

  if (tau >= length)
    tau=length-1;

  variance(array1,length,&av1,&var1);
  variance(array2,length,&av2,&var2);
  
  for (i=0;i<length;i++) {
    array1[i] -= av1;
    array2[i] -= av2;
  }

  if (!stout) {
    fout=fopen(outfile,"w");
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Opened %s for writing\n",outfile);
    fprintf(fout,"# average of first comp.=%e\n",av1);
    fprintf(fout,"# standard deviation of first comp.=%e\n",var1);
    fprintf(fout,"# average of sec. comp.=%e\n",av2);
    fprintf(fout,"# standard deviation of sec. comp.=%e\n",var2);
  }
  else {
    if (verbosity&VER_INPUT)
      fprintf(stderr,"Writing to stdout\n");
    fprintf(stdout,"# average of first comp.=%e\n",av1);
    fprintf(stdout,"# standard deviation of first comp.=%e\n",var1);
    fprintf(stdout,"# average of sec. comp.=%e\n",av2);
    fprintf(stdout,"# standard deviation of sec. comp.=%e\n",var2);
  }

  for (i= -tau;i<=tau;i++)
    if (!stout) {
      fprintf(fout,"%ld %e\n",i,corr(i)/var1/var2);
      fflush(fout);
    }
    else {
      fprintf(stdout,"%ld %e\n",i,corr(i)/var1/var2);
      fflush(stdout);
    }
  if (!stout)
    fclose(fout);
  
  return 0;
}