statistical functions with php?

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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


    Craih Matthews Guest

  2. Similar Questions and Discussions

    1. Help with cf functions
      Here is my problem: I am having component file and inside 3 functions. First function is returning set of results, and then according to...
    2. Request: Statistical analysis - Event detection
      Hello, I'm currently thinking about developing an "event detection" module and I guess I might use Perl for this. Basically what I want to do is...
    3. Using ZIP functions..
      Okay, here's the scenario.. I have uploaded file_blah.zip .. It has a file compressed in it called asdf.mgx .. The thing I need to do is rename the...
    4. #10743 [Com]: class functions & PHP core functions inconsistently clash ;)
      ID: 10743 Comment by: destes at ix dot netcom dot com Reported By: jjones at net-conex dot com Status: Open...
    5. Modules for statistical analysis
      Does anybody know any good modules for computing max- and min-values for data? It's stochastic, lots of little spike and stuff, but the overall...
  3. #2

    Default Re: statistical functions with php?

    "Craih Matthews" <craig1972@nonononospamspamspam.x-mail.net> wrote
    in message news:<bqpro2$qjj$1@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($data) / 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($data));
    }

    function stat_simple_regression ($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
    Nikolai Chuvakhin Guest

  4. #3

    Default Re: statistical functions with php?

    These function were quite useful. Thanks . KANA
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139