473,607 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

statistical functions with php?

hello, does anyone know how to do statistical functions within php? i mean
things like standard deviation, regression etc? any code samples, examples
etc would be great!

thanks!

cm
Jul 17 '05 #1
1 7175
"Craih Matthews" <cr*******@nono nonospamspamspa m.x-mail.net> wrote
in message news:<bq******* ***@news01dd.so-net.ne.jp>...

does anyone know how to do statistical functions within php?
i mean things like standard deviation, regression etc?


Here's what I use for simple statistics (all arguments are arrays):

function stat_mean ($data) {
// calculates mean
return (array_sum($dat a) / count($data));
}

function stat_median ($data) {
// calculates median
sort ($data);
$elements = count ($data);
if (($elements % 2) == 0) {
$i = $elements / 2;
return (($data[$i - 1] + $data[$i]) / 2);
} else {
$i = ($elements - 1) / 2;
return $data[$i];
}
}

function stat_range ($data) {
// calculates range
return (max($data) - min($data));
}

function stat_var ($data) {
// calculates sample variance
$n = count ($data);
$mean = stat_mean ($data);
$sum = 0;
foreach ($data as $element) {
$sum += pow (($element - $mean), 2);
}
return ($sum / ($n - 1));
}

function stat_varp ($data) {
// calculates population variance
$n = count ($data);
$mean = stat_mean ($data);
$sum = 0;
foreach ($data as $element) {
$sum += pow (($element - $mean), 2);
}
return ($sum / $n);
}

function stat_stdev ($data) {
// calculates sample standard deviation
return sqrt (stat_var($data ));
}

function stat_stdevp ($data) {
// calculates population standard deviation
return sqrt (stat_varp($dat a));
}

function stat_simple_reg ression ($x, $y) {
// runs a simple linear regression on $x and $y
// returns an associative array containing the following fields:
// a - intercept
// b - slope
// s - standard error of estimate
// r - correlation coefficient
// r2 - coefficient of determination (r-squared)
// cov - covariation
// t - t-statistic
$output = array();
$output['a'] = 0;
$n = min (count($x), count($y));
$mean_x = stat_mean ($x);
$mean_y = stat_mean ($y);
$SS_x = 0;
foreach ($x as $element) {
$SS_x += pow (($element - $mean_x), 2);
}
$SS_y = 0;
foreach ($y as $element) {
$SS_y += pow (($element - $mean_y), 2);
}
$SS_xy = 0;
for ($i = 0; $i < $n; $i++) {
$SS_xy += ($x[$i] - $mean_x) * ($y[$i] - $mean_y);
}
$output['b'] = $SS_xy / $SS_x;
$output['a'] = $mean_y - $output['b'] * $mean_x;
$output['s'] = sqrt (($SS_y - $output['b'] * $SS_xy)/ ($n - 2));
$output['r'] = $SS_xy / sqrt ($SS_x * $SS_y);
$output['r2'] = pow ($output['r'], 2);
$output['cov'] = $SS_xy / ($n - 1);
$output['t'] = $output['r'] / sqrt ((1 - $output['r2']) / ($n - 2));

return $output;
}

More complicated things (such as multiple regressions) are yet
to be implemented... When I'm 64, perhaps... :)

Cheers,
NC
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2316
by: Jme | last post by:
Can anyone recommend a counter script to track unique visitors? I want to pu tit in osCommerce, since their counter is pretty useless, counting each and every refresh. I want to also check the referring url and any other statistical data that's useful, like time spent per page. I also want to write this data to a database, if possible, so script advice there is greatly appreciated. No need to post scripts if there's a place online I...
2
1721
by: dont bother | last post by:
Hey, I was just wondering if python has any support modules for statistical techniques as Bayesian, Markovian Style Models etc. Any hints? Thanks Dont __________________________________
0
344
by: Verde | last post by:
Excel is packed with a bunch of statistical and financial functions. Is there a comprehensive set of similar functions in then .NET Framework base classes? Or do I need to somehow call Excel via com interop, or ??? Thanks!
3
3563
by: To Forum | last post by:
hi, i am looking for a library in C++ which enables me to fit a function to a set of data...ideally in pure STL C++... Please share your insight on the subject. my thanks TF
0
2082
by: Jurgen | last post by:
Hello, I'm transfering an Excel-VBA app to C#, but i have some problems with tha excel stat functions. I found C# implementations for the NORMDIST function, but I haven't found anything for NORMINV or NORMSINV. Can someone help me with this? -- Jurgen
3
3690
by: Thomas Nelson | last post by:
Sorry if this is a FAQ, but I couldn't find a good summary through google. What kinds of statistical analysis tools exist in python? I really just need t-tests, chi-squared test, and other such tests of statistical significance. A few things point to numpy and scipy, but I was surprised to find the documentation for numpy is not freely available, and I thought it would be wise to ask here before I download it and start hunting through...
0
1066
by: John Henry | last post by:
I am looking for a simple Python function for handling a set of time series data. For instance, I might have the following raw data (year's worth): 1/1/2005 12:00 AM 11.24 1/1/2005 12:10 AM 12.31 1/1/2005 12:20 AM 12.06 1/1/2005 12:30 AM 11.61 1/1/2005 12:40 AM 11.12 1/1/2005 12:50 AM 11.74
4
3067
by: Talbot Katz | last post by:
Greetings Pythoners! I hope you'll indulge an ignorant outsider. I work at a financial software firm, and the tool I currently use for my research is R, a software environment for statistical computing and graphics. R is designed with matrix manipulation in mind, and it's very easy to do regression and time series modeling, and to plot the results and test hypotheses. The kinds of functionality we rely on the most are standard and...
2
2677
Lokean
by: Lokean | last post by:
I'm working on a Common language runtime integration project. We do HEAVY number crunching and would like to integrate some of the aggregate functions for statistical processing, like those which are found in MS Excel. SQL server will not allow for the reference of microsoft.office.interop. So, my question is, are there any code libraries anwywhere that might save me some time re-inventing the wheel? any help would be GREATLY...
0
8050
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7987
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8130
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8324
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2464
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1318
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.