<commit_before>//fit data to a line of the form
//f(x) = a1*x + a2
void fitLin(const parameters * p, const data * d, fit_results * fr)
{
  //construct equations
  int i,j;
  lin_eq_type linEq;
  linEq.dim=2;
  
  linEq.matrix[0][0]=d->xpowsum[0][2];
  linEq.matrix[0][1]=d->xpowsum[0][1]; 
  linEq.matrix[1][0]=d->xpowsum[0][1]; 
  linEq.matrix[1][1]=d->xpowsum[0][0];//bottom right entry
  
  linEq.vector[0]=d->mxpowsum[0][1];
  linEq.vector[1]=d->mxpowsum[0][0];
    
  //solve system of equations and assign values
  if(!(solve_lin_eq(&linEq)==1))
    {
      printf("ERROR: Could not determine fit parameters.\n");
      exit(-1);
    }
  
  //save fit parameters  
  for(i=0;i<linEq.dim;i++)
    fr->a[i]=linEq.solution[i];
  long double f;
  fr->chisq=0;
  fr->ndf=d->lines-3;
  for(i=0;i<d->lines;i++)//loop over data points for chisq
    {
      f=fr->a[0]*d->x[0][i] + fr->a[1];
      fr->chisq+=(d->x[1][i] - f)*(d->x[1][i] - f)
                  /(d->x[1+1][i]*d->x[1+1][i]);
    }
  
  //Calculate covariances and uncertainties, see <NAME> 
  //'Data Analysis Using the Method of Least Squares' sec 2.5
  for(i=0;i<linEq.dim;i++)
    for(j=0;j<linEq.dim;j++)
      fr->covar[i][j]=linEq.inv_matrix[i][j]*(fr->chisq/fr->ndf);
  for(i=0;i<linEq.dim;i++)
    fr->aerr[i]=(long double)sqrt((double)(fr->covar[i][i]));
    
  //now that the fit is performed, use the fit parameters to find the intercept(s)
  fr->fitVert[0]=-1.0*fr->a[1]/fr->a[0];//x-intercept
  fr->fitVert[1]=fr->a[1];//y-intercept
  
  //set up quantities to compute confidence interval
  long double xb,yb,sxx,syx,tval;
  tval=t_stat(d->lines-2,0.025);//t-statistic t(df,alpha/2)
  xb=d->xpowsum[0][1]/d->lines;
  yb=d->msum/d->lines;
  sxx=0.;
  syx=0.;
  for(i=0;i<d->lines;i++)
  	{
  		sxx+=(d->x[0][i] - xb)*(d->x[0][i] - xb);
  		syx+=(d->x[p->numVar][i] - yb)*(d->x[p->numVar][i] - yb);
  	}
  
  /*if(p->verbose>=2)
  	{
		 	printf("Confidence interval t-statistic: %LF\n",tval);
		 	printf("xb: %LF\n",xb);
		 	printf("yb: %LF\n",yb);
		 	printf("sxx: %LF\n",sxx);
		 	printf("syx: %LF\n",syx);
		 	printf("1/n: %lF\n",1.0/d->lines);
 		}*/
  
  //set up equation forms for plotting
  if(strcmp(p->plotMode,"1d")==0)
    {
      sprintf(fr->fitForm[0], "%Lf*x + %Lf",fr->a[0],fr->a[1]);
      sprintf(fr->ciUForm[0], "%Lf*x + %Lf + (%LF * sqrt(%LF/%i) * ((1.0/%i) + sqrt(((x - %LF)**2)/%LF)))",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
      sprintf(fr->ciLForm[0], "%Lf*x + %Lf - (%LF * sqrt(%LF/%i) * ((1.0/%i) + sqrt(((x - %LF)**2)/%LF)))",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
      sprintf(fr->piUForm[0], "%Lf*x + %Lf + (%LF * sqrt(%LF/%i) * (1.0 + (1.0/%i) + sqrt(((x - %LF)**2)/%LF)))",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
      sprintf(fr->piLForm[0], "%Lf*x + %Lf - (%LF * sqrt(%LF/%i) * (1.0 + (1.0/%i) + sqrt(((x - %LF)**2)/%LF)))",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);<commit_msg>Fixed linear regression confidence interval formula again, hopefully for the last time.<commit_after>//fit data to a line of the form
//f(x) = a1*x + a2
void fitLin(const parameters * p, const data * d, fit_results * fr)
{
  //construct equations
  int i,j;
  lin_eq_type linEq;
  linEq.dim=2;
  
  linEq.matrix[0][0]=d->xpowsum[0][2];
  linEq.matrix[0][1]=d->xpowsum[0][1]; 
  linEq.matrix[1][0]=d->xpowsum[0][1]; 
  linEq.matrix[1][1]=d->xpowsum[0][0];//bottom right entry
  
  linEq.vector[0]=d->mxpowsum[0][1];
  linEq.vector[1]=d->mxpowsum[0][0];
    
  //solve system of equations and assign values
  if(!(solve_lin_eq(&linEq)==1))
    {
      printf("ERROR: Could not determine fit parameters.\n");
      exit(-1);
    }
  
  //save fit parameters  
  for(i=0;i<linEq.dim;i++)
    fr->a[i]=linEq.solution[i];
  long double f;
  fr->chisq=0;
  fr->ndf=d->lines-3;
  for(i=0;i<d->lines;i++)//loop over data points for chisq
    {
      f=fr->a[0]*d->x[0][i] + fr->a[1];
      fr->chisq+=(d->x[1][i] - f)*(d->x[1][i] - f)
                  /(d->x[1+1][i]*d->x[1+1][i]);
    }
  
  //Calculate covariances and uncertainties, see <NAME> 
  //'Data Analysis Using the Method of Least Squares' sec 2.5
  for(i=0;i<linEq.dim;i++)
    for(j=0;j<linEq.dim;j++)
      fr->covar[i][j]=linEq.inv_matrix[i][j]*(fr->chisq/fr->ndf);
  for(i=0;i<linEq.dim;i++)
    fr->aerr[i]=(long double)sqrt((double)(fr->covar[i][i]));
    
  //now that the fit is performed, use the fit parameters to find the intercept(s)
  fr->fitVert[0]=-1.0*fr->a[1]/fr->a[0];//x-intercept
  fr->fitVert[1]=fr->a[1];//y-intercept
  
  //set up quantities to compute confidence interval
  long double xb,yb,sxx,syx,tval;
  tval=t_stat(d->lines-2,0.025);//t-statistic t(df,alpha/2)
  xb=d->xpowsum[0][1]/d->lines;
  yb=d->msum/d->lines;
  sxx=0.;
  syx=0.;
  for(i=0;i<d->lines;i++)
  	{
  		sxx+=(d->x[0][i] - xb)*(d->x[0][i] - xb);
  		syx+=(d->x[p->numVar][i] - yb)*(d->x[p->numVar][i] - yb);
  	}
  
  /*if(p->verbose>=2)
  	{
		 	printf("Confidence interval t-statistic: %LF\n",tval);
		 	printf("xb: %LF\n",xb);
		 	printf("yb: %LF\n",yb);
		 	printf("sxx: %LF\n",sxx);
		 	printf("syx: %LF\n",syx);
		 	printf("1/n: %lF\n",1.0/d->lines);
 		}*/
  
  //set up equation forms for plotting
  if(strcmp(p->plotMode,"1d")==0)
    {
      sprintf(fr->fitForm[0], "%Lf*x + %Lf",fr->a[0],fr->a[1]);
      sprintf(fr->ciUForm[0], "%Lf*x + %Lf + (%LF * sqrt(%LF/%i) * sqrt((1.0/%i) + ((x - %LF)**2)/%LF) )",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
      sprintf(fr->ciLForm[0], "%Lf*x + %Lf - (%LF * sqrt(%LF/%i) * sqrt((1.0/%i) + ((x - %LF)**2)/%LF) )",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
      sprintf(fr->piUForm[0], "%Lf*x + %Lf + (%LF * sqrt(%LF/%i) * sqrt(1.0 + (1.0/%i) + ((x - %LF)**2)/%LF) )",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
      sprintf(fr->piLForm[0], "%Lf*x + %Lf - (%LF * sqrt(%LF/%i) * sqrt(1.0 + (1.0/%i) + ((x - %LF)**2)/%LF) )",fr->a[0],fr->a[1],tval,syx,d->lines-2,d->lines,xb,sxx);
{'fraction_non_alphanumeric': 0.2797319932998325, 'fraction_numerical': 0.035510887772194306, 'mean_word_length': 3.3488710852148578, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3921327', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2858, 'n_words': 398}