public static MovAvgModel minimize(MovAvgModel model,EvictingQueue<Double> train,double[] test){
  double temp=1;
  double minTemp=0.0001;
  int iterations=100;
  double alpha=0.9;
  MovAvgModel bestModel=model;
  MovAvgModel oldModel=model;
  double oldCost=cost(model,train,test);
  double bestCost=oldCost;
  while (temp > minTemp) {
    for (int i=0; i < iterations; i++) {
      MovAvgModel newModel=oldModel.neighboringModel();
      double newCost=cost(newModel,train,test);
      double ap=acceptanceProbability(oldCost,newCost,temp);
      if (ap > Math.random()) {
        oldModel=newModel;
        oldCost=newCost;
        if (newCost < bestCost) {
          bestCost=newCost;
          bestModel=newModel;
        }
      }
    }
    temp*=alpha;
  }
  return bestModel;
}

public static MovAvgModel minimize ( MovAvgModel model , EvictingQueue < Double > train , double [ ] test ) { double temp = 1 ; double minTemp = 0.0001 ; int iterations = 100 ; double alpha = 0.9 ; MovAvgModel bestModel = model ; MovAvgModel oldModel = model ; double oldCost = cost ( model , train , test ) ; double bestCost = oldCost ; while ( temp > minTemp ) { for ( int i = 0 ; i < iterations ; i ++ ) { MovAvgModel newModel = oldModel . neighboringModel ( ) ; double newCost = cost ( newModel , train , test ) ; double ap = acceptanceProbability ( oldCost , newCost , temp ) ; if ( ap > Math . random ( ) ) { oldModel = newModel ; oldCost = newCost ; if ( newCost < bestCost ) { bestCost = newCost ; bestModel = newModel ; } } } temp *= alpha ; } return bestModel ; }
runs the simulated annealing algorithm and produces a model with new coefficients that , theoretically fit the data better and generalizes to future forecasts without overfitting .
