AnovaTable RegSuf::anova()const{ AnovaTable ans; double nobs = n(); double p = size(); // p+1 really ans.SSE = SSE(); ans.SST = SST(); ans.SSM = ans.SST - ans.SSE; ans.df_total = nobs-1; ans.df_error = nobs - p; ans.df_model = p-1; ans.MSE = ans.SSE/ ans.df_error; ans.MSM = ans.SSM/ans.df_model; ans.F = ans.MSM/ans.MSE; ans.p_value = pf(ans.F, ans.df_model, ans.df_error, false, false); return ans; }
int main(int argc, char *argv[]){ /* Itteration Variables */ int i = 1; int gen = 0; double bestSSE = DBL_MAX; /* Creating performance variables */ double genDiv = 0; double genSSE[3] = {0,0,0}; /* worst, avg, best */ double sseError[MAXPOP]; double val[NUMPOINTS][2]; /* Genetic Paramenters */ int populationSize = 500; /* Population size (constant) */ int treeDepth = 15; /* Maximum Tree Depth */ int maxGenerations = 50; /* Maximum Number of generations */ double sseGoal = 0.5; /* SSE error goal (for spartan) */ double pruneFactor = 0.0; /* Probability that a branch will be prunned */ double constProb = 0.60; /* Probability that a leaf will be a randomly choose (0,1) constant */ double mutationRate = 0.20; /* Mutation Rate (probability that a node will be mutated */ double swapRate = 0.7; /* Probability that crossover will occur */ double tournamentFraction = 0.40; /* fraction of individuals selected by tournament */ double rankFraction = 0.50; /* fraction of individual selected by rank */ double spartanFraction = 0.1; /* fraction of individuals selected by rank, copied as is */ struct geneticParam genParam; /* compat representation of generation */ node *forest[MAXPOP]; /* Forest of trees */ node *bestTree; /* Output Variables */ int train = 1; int performance = 1; double evalPoint = 0; FILE *out = stdout; char bestTreeName[128]; char response[128] = "Y"; strcpy(bestTreeName,"bestTree"); /* Processing Command Line Inputs */ for (i = 1; i < argc; i++){ if (strcmp(argv[i],"--maxDepth")==0){ sscanf(argv[++i],"%d",&treeDepth); } else if (strcmp(argv[i],"--test")==0){ test(); return EXIT_SUCCESS; } else if (strcmp(argv[i],"--popSize")==0){ populationSize = atoi(argv[++i]); } else if (strcmp(argv[i],"--pruneFactor")==0){ pruneFactor = (double)atof(argv[++i]); } else if (strcmp(argv[i],"--constProb")==0){ constProb = (double)atof(argv[++i]); } else if (strcmp(argv[i],"--sseGoal")==0){ sseGoal = (double)atof(argv[++i]); } else if (strcmp(argv[i],"--mutationRate")==0){ mutationRate = (double)atof(argv[++i]); } else if (strcmp(argv[i],"--swapRate")==0){ swapRate = (double)atof(argv[++i]); } else if (strcmp(argv[i],"--tournamentFraction")==0){ tournamentFraction = (double) atof(argv[++i]); rankFraction = 1.0 - tournamentFraction; } else if (strcmp(argv[i],"--rankFraction")==0){ rankFraction = (double) atof(argv[++i]); } else if (strcmp(argv[i],"--spartanFraction")==0){ spartanFraction = (double) atof(argv[++i]); } else if (strcmp(argv[i],"--maxGen")==0){ maxGenerations = atoi(argv[++i]); } else if(strcmp(argv[i],"--bestTreeName")==0){ strcpy(bestTreeName,argv[++i]); } else if (strcmp(argv[i],"--help")==0){ usage(stdout,argv[0]); exit(EXIT_SUCCESS); } else{ fprintf(stderr,"Argument %s is not reconized\n",argv[i]); usage(stderr,argv[0]); exit(EXIT_FAILURE); } } /* Checking Input Arguments */ if (populationSize > MAXPOP){ fprintf(stderr,"population size is set to the max at %d. Change define for larger population\n",MAXPOP); populationSize = MAXPOP; } if (mutationRate > 1) mutationRate = 0.1; if (swapRate > 1) swapRate = 0.1; if (tournamentFraction + rankFraction > 1.0){ fprintf(stderr,"Tournament Fraction %5.3f and rank fraction %5.3f are greater than 1.0\n", tournamentFraction,rankFraction); fprintf(stderr,"Both are set to default values\n"); tournamentFraction = 0.9; rankFraction = 1.0 - tournamentFraction; } genParam.mutationRate = mutationRate; genParam.swapRate = swapRate; genParam.touramentFraction = tournamentFraction; genParam.rankFraction = rankFraction; genParam.spartanFraction = spartanFraction; genParam.constProb = constProb; genParam.maxDepth = treeDepth; genParam.pruneFraction = pruneFactor; splash(out); /* Train or Run? */ if (argc <= 1){ fprintf(stdout,"Preform symbolic regression with genetic programming [y/n]:\n"); fscanf(stdin,"%s",response); if (strcmp(response,"y")==0 || strcmp(response,"Y")==0) train = 1; else train = 0; fprintf(stdout,"Do you want to print the performance of the best tree [y/n]: \n"); fscanf(stdin,"%s",response); if (strcmp(response,"y")==0 || strcmp(response,"Y")==0) performance = 1; else performance = 0; } /* Run Information */ fprintf(out,"Parameters:\n"); fprintf(out,"\tPopulation Size: %d\n\tMax Tree Depth: %d\n",populationSize,treeDepth); fprintf(out,"\tPrune factor: %3.2f\n\tConstant Probability: %3.2f\n",pruneFactor,constProb); fprintf(out,"\tSSE Goal: %3.2f\n\tMax Generations: %d\n",sseGoal,maxGenerations); fprintf(out,"\tSpartan Fraction: %3.2f\n",spartanFraction); fprintf(out,"\tTournament Fraction: %3.2f\n\tRank Fraction: %5.2f\n",tournamentFraction,rankFraction); fprintf(out,"\tMutation Rate: %3.2f\n\tSwap Rate: %3.2f\n",mutationRate,swapRate); printSet(); /* Reading in the data file */ importData("proj2-data.dat",val); /* Creating the intitial population */ if (train){ srand( time(NULL)); fprintf(stdout,"Creating Intial Population\n"); rampedHalfHalf(forest,populationSize,&genParam); /* Running Generations */ fprintf(out,"Generation\tDiversity\tMean SSE\tBest SSE\n"); while(gen < maxGenerations && bestSSE > sseGoal){ /* Diversity and SSE */ genDiv = diversity(forest,populationSize); bestSSE = SSE(forest,populationSize,val,genSSE,sseError,bestTreeName); fprintf(out,"\t%d\t%3.2f\t\t%3.2e\t%3.2e\n",gen,genDiv,genSSE[1],genSSE[2]); /* Genetic Operations */ breedGeneration(forest,populationSize,sseError,&genParam); gen++; } /* Diversity and SSE */ genDiv = diversity(forest,populationSize); bestSSE = SSE(forest,populationSize,val,genSSE,sseError,bestTreeName); fprintf(out,"\t%d\t%3.2f\t\t%3.2e\t%3.2e\n",gen,genDiv,genSSE[1],genSSE[2]); /* Clean up, clean up, everybody do your share */ deleteForest(forest,populationSize); } /* Looking at the performance of the best tree */ if (performance){ sprintf(response,"%s.postfix",bestTreeName); bestTree = bestTreeSummary(out,response,val); if (argc <= 1){ fprintf(stdout,"Enter value on which to test the tree [n to escape]: \n"); while(fscanf(stdin,"%lf",&evalPoint)==1){ fprintf(stdout,"Tree(%5.3f) = %5.3f\n",evalPoint,evalTree(bestTree,evalPoint)); } deleteTree(bestTree); } } return EXIT_SUCCESS; }
double exp1Var(unsigned n, double *x, double *grad, void *my_func_data) { HWModel *model=(HWModel*)my_func_data; int fup,fdown; double *level=model->super.level; double *trend=model->super.trend; double *season=model->super.season; double dotrend=(model->super.dotrend); double doseasonal=(model->super.doseasonal); int period=(model->super.period); int seasonal=(model->super.seasonType); //Season additive oder multiplikative int xl=(model->obsCount); double a=(model->super.a); double b=(model->super.b); double alpha=x[0]; double beta=model->super.beta; double gamma=model->super.gamma; int start_time=(model->super.start_time); double smape=0; double ga=1e-5; double xhat = 0, stmp = 0; int i, i0, s0; model->expectedValueXSquared=0; model->expectedValueX=0; /* copy start values to the beginning of the vectors */ level[0] = a; if (dotrend == 1) trend[0] = b; for (i = start_time; i < xl; i++) { /* indices for period i */ i0 = i - start_time + 1; s0 = i0 + period - 1; /* forecast *for* period i */ xhat = level[i0 - 1] + (dotrend == 1 ? trend[i0 - 1] : 0); stmp = doseasonal == 1 ? season[s0 - period] : (seasonal != 1); if (seasonal == 1) xhat += stmp; else xhat *= stmp; switch(model->super.optflag[3]){ case 0: smape += SMAPE(xhat,model->data[i]); break; case 1: smape+= SSE(xhat,model->data[i]); break; case 2: smape+= ABS(xhat,model->data[i]); break; } model->expectedValueXSquared+=pow((xhat-model->data[i]),2); model->expectedValueX+=(abs(xhat-model->data[i])); /* estimate of level *in* period i */ if (seasonal == 1) level[i0] = alpha * (model->data[i] - stmp) + (1 - alpha) * (level[i0 - 1] + trend[i0 - 1]); else level[i0] = alpha * (model->data[i] / stmp) + (1 - alpha) * (level[i0 - 1] + trend[i0 - 1]); /* estimate of trend *in* period i */ if (dotrend == 1) trend[i0] = beta * (level[i0] - level[i0 - 1]) + (1 - beta) * trend[i0 - 1]; /* estimate of seasonal component *in* period i */ if (doseasonal == 1) { if (seasonal == 1) season[s0] = gamma * (model->data[i] - level[i0]) + (1 - gamma) * stmp; else season[s0] = gamma * (model->data[i] / level[i0]) + (1 - gamma) * stmp; } } if(grad && model->gradflag) { model->gradflag=0; for(i=0;i<n;i++) { if(model->super.optflag[i]==0) { grad[i]=0; continue; } x[i]+=ga; fup=exp1Var(n, x,grad,my_func_data); x[i]-=2*ga; fdown=exp1Var(n, x,grad,my_func_data); x[i]+=ga; grad[i]=(fup-fdown)/(2*ga); } model->gradflag=1; } //model->oneStepAheadForecastVariance=(1.0/(model->obsCount-1))*(expectedValueXSquared-((1.0/model->obsCount)*pow(expectedValueX,2))); return smape/(model->obsCount-(model->super.start_time-1)); }