The importance of the Linux script development math library in PHP

Source: Internet
Author: User
Tags constructor contains count functions variables php class linux

  Brief Introduction

Compared to other Open-source languages such as Perl and Python, the PHP community lacks a strong job to develop a math library. One reason for this is that there are already a large number of sophisticated mathematical tools that may be preventing the community from developing its own PHP tools. For example, I have studied a powerful tool S System, which has an impressive set of statistical databases, designed to analyze datasets, and was awarded the ACM Award in 1998 for its language design. If S or its open source category R is just a exec_shell call, why bother using PHP to achieve the same statistical computing function? For more information on S System, its ACM awards, or R, see Resources.

Isn't this a waste of developer energy? If the motivation for developing a PHP math library is to save the developer's energy and use the best tools to do the job, then PHP's current topic is meaningful.

On the other hand, motivation for teaching may encourage the development of a PHP math library. For about 10% of the people, mathematics is an interesting subject to explore. For those who are also skilled at PHP, the development of the PHP Math library can enhance the math learning process, in other words, not just reading chapters on T-tests, but also implementing a class that calculates the intermediate values and displays them in a standard format.

Through coaching and training, I want to prove that developing a PHP math library is not a difficult task, it may represent an interesting technology and learning problem. In this article, I'll provide a sample PHP math library, called Simplelinearregression, which demonstrates a common way to develop a PHP math library. Let's start by discussing some common principles that guide me in developing this simplelinearregression class.

  Guiding Principles

I used six general principles to guide the development of simplelinearregression classes.

Each analysis model builds a class.

Use reverse links to develop classes.

Expected to have a large number of getter.

Store intermediate results.

Develop preferences for detailed APIs.

Perfection is not a goal.

Let's look at these guidelines in more detail.

  Each analysis model builds a class

Each major analytical test or procedure should have a PHP class with the same name as the test or procedure name, which contains the input functions, functions and output functions that calculate the intermediate value and the total value (the median and total values are displayed on the screen in text or graphic format).

  use reverse link to develop class

In mathematical programming, the goal of encoding is usually the standard output value that an analysis process (such as multipleregression, TimeSeries, or chisquared) expects to generate. From a problem-solving perspective, this means that you can use reverse linking to develop a method of mathematical classes.

For example, the summary Output screen displays one or more summary statistics. These summary statistical results depend on the calculation of the intermediate statistic results, which may involve a deeper level of intermediate statistics, and so on. This development method based on the reverse link derives the next principle.

  expected to have a large number of getter

Most of the math class development work involves calculating intermediate values and summary values. In fact, this means that you should not be surprised if your class contains many getter methods for calculating intermediate and aggregate values.

  Store Intermediate Results

The intermediate results are stored in the result object so that you can use the intermediate result as input for subsequent computations. This principle is implemented in S language design. In the current environment, this principle is implemented by selecting the instance variable to represent the calculated intermediate value and the aggregated result.

  develop preferences for detailed APIs

When making a naming scheme for member functions and instance variables in the Simplelinearregression class, I found that if I used a longer name (a name like Getsumsquarederror rather than a getYY2) to describe a member function and an instance variable, It is much easier to understand what the operation of the function is and what the variables mean.

I did not give up the abbreviated name altogether; however, when I use the abbreviated name, I have to try to provide a comment to fully explain the meaning of the name. My view is that highly abbreviated naming schemes are common in mathematical programming, but they make it more difficult to understand and prove whether a mathematical routine is more or less in step, without having to create such a difficulty.

  Perfection is not a goal

The goal of this coding exercise is not to be sure to develop a highly optimized and rigorous math engine for PHP. In the early stages, emphasis should be placed on learning to achieve significant analytical testing, as well as addressing the challenges in this area.

Instance variables: When modeling a statistical test or procedure, you need to indicate which instance variables to declare.

The selection of an instance variable can be determined by stating the intermediate value and the total value generated by the analysis process. Each intermediate value and summary value can have a corresponding instance variable that takes the value of the variable as an object property.

I use this analysis to determine which variables to declare for the Simplelinearregression class in Listing 1. A similar analysis can be performed on multipleregression, ANOVA, or timeseries processes.

  

       
        
<?php//Copyright 2003, Paul Meagher//Distributed under GPL class Simplelinearregression {var $n;   var $X = array ();   var $Y = array ();   var $ConfInt;   var $Alpha;   var $XMean;   var $YMean;   var $SumXX;   var $SumXY;   var $SumYY;   var $Slope;   var $YInt;   var $PredictedY = array ();   var $Error = array ();   var $SquaredError = array ();   var $TotalError;   var $SumError;   var $SumSquaredError;   var $ErrorVariance;   var $StdErr;   var $SlopeStdErr;  var $SlopeVal;   T value of Slope var $YIntStdErr;  var $YIntTVal;   T value for Y Intercept var $R;   var $RSquared;     var $DF; Degrees of Freedom var $SlopeProb;  Probability of Slope estimate var $YIntProb; Probability of Y Intercept estimate Var $AlphaTVal;      T Value for given Alpha setting Var $ConfIntOfSlope; var $RPath = "/USR/LOCAL/BIN/R"; Your path here var $format = "%01.2f";    Used for formatting output}?>
       

  

Listing 1. Instance variables of the Simplelinearregression class

  Constructors

The constructor method of the Simplelinearregression class accepts an X and a Y vector, each of which has the same number of values. You can also set a confidence interval that defaults to 95% for your estimated Y value (confidence interval).

The constructor method starts with validating that the data form is appropriate for processing. Once the input vector passes the "equal size" and "value greater than 1" test, the kernel part of the algorithm is executed.

Performing this task involves calculating the intermediate and summary values of the statistical process through a series of getter methods. Assigns the return value of each method call to an instance variable of the class. Storing the results in this way ensures that the call routines in the back-and-forth calculation can use intermediate and summary values. You can also display these results by calling the output method of the class, as described in Listing 2.

  

       
        
<?php//Copyright 2003, Paul Meagher//Distributed under GPL function Simplelinearregression ($X, $Y, $Co   "Nfidenceinterval=") {$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 is 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-&Gt;getconfintofslope ();   return true; }?>
       

  

Listing 2. Calling class Output method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.