
Linux脚本开发数学库在PHP中的重要性
发布时间:2006-10-19 12:32:15 来源:天极博客 网友评论 0 条SimpleLinearRegression 类的构造函数方法接受一个 X 和一个 Y 向量,每个向量都有相同数量的值。您还可以为您预计的 Y 值设置一个缺省为 95% 的置信区间(confidence interval)。
构造函数方法从验证数据形式是否适合于处理开始。一旦输入向量通过了“大小相等”和“值大于 1”测试,就执行算法的核心部分。
执行这项任务涉及到通过一系列 getter 方法计算统计过程的中间值和汇总值。将每个方法调用的返回值赋给该类的一个实例变量。用这种方法存储计算结果确保了前后链接的计算中的调用例程可以使用中间值和汇总值。还可以通过调用该类的输出方法来显示这些结果,如清单 2 所描述的那样。
<?php // Copyright 2003, Paul Meagher // Distributed under GPL function SimpleLinearRegression($X, $Y, $ConfidenceInterval="95") { $numX = count($X); $numY = count($Y); if ($numX != $numY) { die("Error: Size of X and Y vectors must be the same."); } if ($numX <= 1) { die("Error: Size of input array must be at least 2."); } $this->n = $numX; $this->X = $X; $this->Y = $Y; $this->ConfInt = $ConfidenceInterval; $this->Alpha = (1 + ($this->ConfInt / 100) ) / 2; $this->XMean = $this->getMean($this->X); $this->YMean = $this->getMean($this->Y); $this->SumXX = $this->getSumXX(); $this->SumYY = $this->getSumYY(); $this->SumXY = $this->getSumXY(); $this->Slope = $this->getSlope(); $this->YInt = $this->getYInt(); $this->PredictedY = $this->getPredictedY(); $this->Error = $this->getError(); $this->SquaredError = $this->getSquaredError(); $this->SumError = $this->getSumError(); $this->TotalError = $this->getTotalError(); $this->SumSquaredError = $this->getSumSquaredError(); $this->ErrorVariance = $this->getErrorVariance(); $this->StdErr = $this->getStdErr(); $this->SlopeStdErr = $this->getSlopeStdErr(); $this->YIntStdErr = $this->getYIntStdErr(); $this->SlopeTVal = $this->getSlopeTVal(); $this->YIntTVal = $this->getYIntTVal(); $this->R = $this->getR(); $this->RSquared = $this->getRSquared(); $this->DF = $this->getDF(); $this->SlopeProb = $this->getStudentProb($this->SlopeTVal, $this->DF); $this->YIntProb = $this->getStudentProb($this->YIntTVal, $this->DF); $this->AlphaTVal = $this->getInverseStudentProb($this->Alpha, $this->DF); $this->ConfIntOfSlope = $this->getConfIntOfSlope(); return true; } ?>
<?php
// Copyright 2003, Paul Meagher
// Distributed under GPL
function SimpleLinearRegression($X, $Y, $ConfidenceInterval="95") {
$numX = count($X);
$numY = count($Y);
if ($numX != $numY) {
die("Error: Size of X and Y vectors must be the same.");
}
if ($numX <= 1) {
die("Error: Size of input array must be at least 2.");
}
$this->n = $numX;
$this->X = $X;
$this->Y = $Y;
$this->ConfInt = $ConfidenceInterval;
$this->Alpha = (1 + ($this->ConfInt / 100) ) / 2;
$this->XMean = $this->getMean($this->X);
$this->YMean = $this->getMean($this->Y);
$this->SumXX = $this->getSumXX();
$this->SumYY = $this->getSumYY();
$this->SumXY = $this->getSumXY();
$this->Slope = $this->getSlope();
$this->YInt = $this->getYInt()
$this->PredictedY = $this->getPredictedY();
$this->Error = $this->getError();
$this->SquaredError = $this->getSquaredError();
$this->SumError = $this->getSumError();
$this->TotalError = $this->getTotalError();
$this->SumSquaredError = $this->getSumSquaredError();
$this->ErrorVariance = $this->getErrorVariance();
$this->StdErr = $this->getStdErr();
$this->SlopeStdErr = $this->getSlopeStdErr();
$this->YIntStdErr = $this->getYIntStdErr();
$this->SlopeTVal = $this->getSlopeTVal();
$this->YIntTVal = $this->getYIntTVal();
$this->R = $this->getR();
$this->RSquared = $this->getRSquared();
$this->DF = $this->getDF();
$this->SlopeProb = $this->getStudentProb($this->SlopeTVal, $this->DF);
$this->YIntProb = $this->getStudentProb($this->YIntTVal, $this->DF);
$this->AlphaTVal = $this->getInverseStudentProb($this->Alpha, $this->DF);
$this->ConfIntOfSlope = $this->getConfIntOfSlope();
return true;
}
?>
方法名及其序列是通过结合逆向链接和参考大学本科学生使用的统计学教科书推导得出的,该教科书一步一步地说明了如何计算中间值。我需要计算的中间值的名称带有“get”前缀,从而推导出方法名。
使模型与数据相吻合
SimpleLinearRegression 过程用于产生与数据相吻合的直线,其中直线具有以下标准方程:
y = b + mx
该方程的 PHP 格式看起来类似于清单 3:
清单 3. 使模型与数据相吻合的 PHP 方程
$PredictedY[$i] = $YIntercept + $Slope * $X[$i]
SimpleLinearRegression 类使用最小二乘法准则推导出 Y 轴截距(Y Intercept)和斜率(Slope)参数的估计值。这些估计的参数用来构造线性方程(请参阅清单 3),该方程对 X 和 Y 值之间的关系进行建模。
使用推导出的线性方程,您就可以得到每个 X 值对应的预测 Y 值。如果线性方程与数据非常吻合,那么 Y 的观测值与预测值趋近于一致。
如何确定是否非常吻合
SimpleLinearRegression 类生成了相当多的汇总值。一个重要的汇总值是 T 统计值,它可以用来衡量一个线性方程与数据的吻合程度。如果非常吻合,那么 T 统计值往往很大。如果 T 统计值很小,那么应当用一个模型替换该线性方程,该模型假设 Y 值的均值是最佳预测值(也就是说,一组值的均值通常是下一个观测值有用的预测值,使之成为缺省模型)。
要测试 T 统计值是否大得足以不把 Y 值的均值作为最佳预测值,您需要计算获取 T 统计值的随机概率。如果获取 T 统计值的概率很低,那么您可以否定均值是最佳预测值这个无效假设,与此相对应,也就确信简单线性模型与数据非常吻合。
那么,如何计算 T 统计值的概率呢?
计算 T 统计值概率
由于 PHP 缺少计算 T 统计值概率的数学例程,因此我决定将此任务交给统计计算包 R(请参阅参考资料中的 www.r-project.org)来获得必要的值。我还想提醒大家注意该包,因为:
R 提供了许多想法,PHP 开发人员可能会在 PHP 数学库中模拟这些想法。
有了 R,可以确定从 PHP 数学库获得的值与那些从成熟的免费可用的开放源码统计包中获得的值是否一致。
清单 4 中的代码演示了交给 R 来处理以获取一个值是多么容易。
清单 4. 交给 R 统计计算包来处理以获取一个值
<?php
// Copyright 2003, Paul Meagher
// Distributed under GPL
class SimpleLinearRegression {
var $RPath = "/usr/local/bin/R"; // Your path here
function getStudentProb($T, $df) {
$Probability = 0.0;
$cmd = "echo 'dt($T, $df)' | $this->RPath --slave";
$result = shell_exec($cmd);
list($LineNumber, $Probability) = explode(" ", trim($result));
return $Probability;
}
function getInverseStudentProb($alpha, $df) {
$InverseProbability = 0.0;
$cmd = "echo 'qt($alpha, $df)' | $this->RPath --slave";
$result = shell_exec($cmd);
list($LineNumber, $InverseProbability) = explode(" ", trim($result));
return $InverseProbability;
}
}
?>
请注意,这里已经设置了到 R 可执行文件的路径,并在两个函数中使用了该路径。第一个函数根据学生的 T 分布返回了与 T 统计值相关的概率值,而第二个反函数计算了与给定的 alpha 设置相对应的 T 统计值。getStudentProb 方法用来评估线性模型的吻合程度;getInverseStudentProb 方法返回一个中间值,它用来计算每个预测的 Y 值的置信区间。
由于篇幅有限,我不可能逐个详细说明这个类中的所有函数,因此如果您想搞清楚简单线性回归分析中所涉及的术语和步骤,我鼓励您参考大学本科学生使用的统计学教科书。
燃耗研究
要演示如何使用该类,我可以使用来自公共事业中燃耗(burnout)研究中的数据。Michael Leiter 和 Kimberly Ann Meechan 研究了称为消耗指数(Exhaustion Index)的燃耗度量单位和称之为集中度(Concentration)的独立变量之间的关系。集中度是指人们的社交接触中来自其工作环境的那部分比例。
要研究他们样本中个人的消耗指数值与集中度值之间的关系,请将这些值装入适当命名的数组中,并用这些数组值对该类进行实例化。对类进行实例化后,显示该类所生成的某些汇总值以评估线性模型与数据的吻合程度。
清单 5 显示了装入数据和显示汇总值的脚本:
清单 5. 用于装入数据并显示汇总值的脚本
<?php
// BurnoutStudy.php
// Copyright 2003, Paul Meagher
// Distributed under GPL
include "SimpleLinearRegression.php";
// Load data from burnout study
$Concentration = array(20,60,38,88,79,87,
68,12,35,70,80,92,
77,86,83,79,75,81,
75,77,77,77,17,85,96);
$ExhaustionIndex = array(100,525,300,980,310,900,
410,296,120,501,920,810,
506,493,892,527,600,855,
709,791,718,684,141,400,970);
$slr = new SimpleLinearRegression($Concentration, $ExhaustionIndex);
$YInt = sprintf($slr->format, $slr->YInt);
$Slope = sprintf($slr->format, $slr->Slope);
$SlopeTVal = sprintf($slr->format, $slr->SlopeTVal);
$SlopeProb = sprintf("%01.6f", $slr->SlopeProb);
?>
<table border='1' cellpadding='5'
- 微软官方入门教程19:轻松掌握Vista系统的快
- 微软2008大冲击,预借Vista SP1力促Vista市
- 在收件箱中获得 Windows Vista 的最新更新
- 微软官方Vista入门教程全集19篇(Vista学院
- Windows Vista 的成功将势不可挡
- 快快抛弃Vista,拥抱XP SP3!你觉得呢?
- 浅谈Vista系统关闭虚拟内存与使用内存盘加速
- 嘿嘿,按下键盘上面的三个键,马上让你的Vi
- Windows Vista的盗版率只有Windows XP的一半
- 3DMark和PCMark Vantage新版将只支持Vista系
- Windows外衣Linux心 红旗桌面版详测
- 扮酷你的桌面 Linux超靓壁纸下载(多图)
- Linux安装流程
- 浅谈Linux的内核
- RedHat Linux9.0安装实例(1)
- ARM的嵌入式Linux移植体验之基本概念
- Linux安装要点
- 红旗Linux桌面版5.0BETA版OS
- 如何在大硬盘上安装Linux
- RealPlayer流媒体播放器Linux版
- Linux步入Unix的后尘-铁甲Linux出现
- Linux内核中的同步和互斥分析报告
- Linux操作系统文件系统的桌面应用
- Linux设备驱动编程之定时器
- 嵌入式Linux操作系统启动信息完全注释
- 在Linux操作系统中实现内部进程通信
- Linux大腕警告称开源软件存在安全问题
- Linux下双网卡绑定技术实现负载均衡
- 深入浅出Linux操作系统的优化和微调
- Linux下的中文显示和支持常见问题解答
- 大话G游 专题:手机病毒揭密
- ARP攻击防范与解决方案 路由故障处理手册
- Picasa中文版_Picasa教程 专题:清除流氓软件
- Firefox专题 seo搜索引擎优化专区
- 重装Windows必知的事情 装机之必备软件大行动
