public void trainModel()
  {
    costHistory = new double[numIter];
    
    double sum[] = null;
    int k = 0;
    
  //  for(int k = 0; k < numIter; k++)
    while(true)
    {
      sum = new double[theta.length];
      
      for(int i = 0; i < numSamples; i++)
      {
        for(int j = 0; j < theta.length; j++)
        {
          sum[j] = sum[j] + (predictionHelper(x[i]) - y[i]) * x[i][j];
        }
      }
      
      for(int j = 0; j < theta.length; j++)
      {
        theta[j] = theta[j] - learningRate * 1.0 / numSamples * sum[j];
      }
      sum = null;
      
      if(k == costHistory.length)
      {
        costHistory = Arrays.copyOf(costHistory, k + 1000);
      }
      costHistory[k] = calculateCost();
      
      if(k > 0 && (costHistory[k -1] - costHistory[k] < 0.0001))
    	  break;
      k++;
    }
    System.out.println("Num. Steps = " + k);
    double iterationValues[] = new double[k];
    for(int i = 0; i < k; i++)
      iterationValues[i] = i + 1;
    
    try
    {
      Plot p = new Plot("CostFunctionTrend.png", "Cost Function Trend", "Iterations", "Cost", "", iterationValues, costHistory);
      p.setPlotStyle(PlotStyle.LINE);
      p.plotGraph();
	} 
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }