Skip to content

Instantly share code, notes, and snippets.

@fxborg
Last active December 7, 2016 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxborg/cc9541f150fcfd74e2825a490dc113ba to your computer and use it in GitHub Desktop.
Save fxborg/cc9541f150fcfd74e2825a490dc113ba to your computer and use it in GitHub Desktop.
liner regression
<?php
$data;
$arr=array(
0,0.3987527,-0.8932547,0.1768298,-1.675824,-2.837438,-2.355524,-1.252398,-2.899442,-2.419981,-2.091308,-2.10021,-2.714076,-3.625284,-3.592246,-2.441939,-3.843519,-4.990552,-3.515711,-4.657077,-7.364385,-7.658229,-7.499073,-7.562957,-8.481097,-8.252456,-8.464059,-7.580894,-7.210767,-6.445776,-6.008003,-5.500024,-4.63942,-6.790132,-6.419145,-7.802707,-7.596067,-6.33891,-6.690298,-7.485143,-7.644236,-6.111133,-5.882926,-5.647768,-6.127489,-6.66978,-6.871189,-8.511509,-7.466892,-7.792628,-9.821345,-10.05553,-10.42828,-12.32344,-10.51485,-11.57175,-13.00084,-12.73021,-11.76026,-11.89079,-11.64385,-12.71155,-11.84579,-11.49114,-11.33376,-10.54172,-9.44159,-10.01351,-9.509508,-10.437,-8.269219,-7.62659,-7.505197,-7.904896,-8.12006,-9.240085,-9.298025,-8.780998,-9.275904,-10.12872,-11.86345,-11.52073,-11.26576,-11.491,-12.07157,-11.88601,-13.27906,-14.90004,-16.58147,-17.229,-16.64617,-15.97717,-16.49825,-15.11031,-16.1323,-16.66544,-17.47197,-17.43013,-19.23583,-18.25882,-17.05801,-15.96338,-15.87742,-14.62859,-13.97062,-12.86039,-12.49589,-12.07383,-14.08006,-14.76518,-13.8932,-13.77575,-14.97463,-16.04297,-16.76059,-14.79139,-14.85039,-15.44074,-17.56516,-19.69539,-18.80035
);
$XYsum=0.0;
$Xsum=0.0;
$Ysum=0.0;
$X2sum=0.0;
$b;
for($x=0;$x<120;$x++)
{
$y = $arr[$x];
$XYsum+=$x*$y;
$Xsum+=$x;
$Ysum+=$y;
$X2sum+=$x*$x;
}
$n=$x;
$div=($n * $X2sum - $Xsum * $Xsum) ;
if($div!=0)
{
$b= ($n * $XYsum - $Xsum * $Ysum) / $div;
$a= ($X2sum * $Ysum - $XYsum * $Xsum) / $div;
echo "傾き:{$b} , 切片:{$a}\n";
echo "--->予測値:". ($x*$b+$a) . "\n";
}
$avg=$Xsum/120;
$y0=$a;
$v=0;
$vv=0;
$a=0;
for($x=0;$x<120;$x++)
{
$ym=$y0+$b*$x;
$df= $arr[$x]-$ym;
$vv+= $df*$df;
$a+= pow(($x-$avg),2);
}
$ve=$vv/118;
echo "V:{$vv}\n";
echo "Ve:{$ve}\n";
echo "XV:{$a}\n";
$sa= sqrt(($ve/$a));
echo "Sa:{$sa}\n";
$t = $b/$sa;
echo "T-value:{$t}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment