[#26488] Add Standard Deviation Function to Math Module — Daniel Cohen <danielc2017@...>

This patch adds a Standard Deviation function to the Math Module. It takes

25 messages 2009/11/02
[#26489] Re: Add Standard Deviation Function to Math Module — Yukihiro Matsumoto <matz@...> 2009/11/03

Hi,

[#26490] Re: Add Standard Deviation Function to Math Module — Daniel Cohen <danielc2017@...> 2009/11/03

OK,

[#26493] Re: Add Standard Deviation Function to Math Module — Yukihiro Matsumoto <matz@...> 2009/11/03

Hi,

[#26511] Re: Add Standard Deviation Function to Math Module — Yusuke ENDOH <mame@...> 2009/11/03

Hi,

[#26492] HashWithIndifferentAccess to core — Urabe Shyouhei <shyouhei@...>

Hello,

35 messages 2009/11/03
[#26496] Re: HashWithIndifferentAccess to core — Yukihiro Matsumoto <matz@...> 2009/11/03

Hi,

[#26507] Re: HashWithIndifferentAccess to core — Jeremy Kemper <jeremy@...> 2009/11/03

On Tue, Nov 3, 2009 at 6:48 AM, Yukihiro Matsumoto <matz@ruby-lang.org> wro=

[#26514] Re: HashWithIndifferentAccess to core — "Martin J. Dst" <duerst@...> 2009/11/04

Just a thought: What about implementing this with an option on Hash:new,

[#26522] Re: HashWithIndifferentAccess to core — Yusuke ENDOH <mame@...> 2009/11/04

Hi,

[#26555] Re: HashWithIndifferentAccess to core — Yukihiro Matsumoto <matz@...> 2009/11/05

Hi,

[#26584] Re: HashWithIndifferentAccess to core — Yugui <yugui@...> 2009/11/07

2009/11/6 Yukihiro Matsumoto <matz@ruby-lang.org>:

[#26589] Re: HashWithIndifferentAccess to core — Yukihiro Matsumoto <matz@...> 2009/11/07

Hi,

[#26593] Re: HashWithIndifferentAccess to core — Lourens Naud<lourens@...> 2009/11/07

Hi,

[#26523] [Bug #2330] Non systematic segmentation fault with autoload rubyspec — Marc-Andre Lafortune <redmine@...>

Bug #2330: Non systematic segmentation fault with autoload rubyspec

12 messages 2009/11/04

[#26560] [Feature #2340] Removing YAML/Syck — Yui NARUSE <redmine@...>

Feature #2340: Removing YAML/Syck

38 messages 2009/11/06
[#26562] [Feature #2340] Removing YAML/Syck — Yui NARUSE <redmine@...> 2009/11/06

Issue #2340 has been updated by Yui NARUSE.

[#26567] Re: [Feature #2340] Removing YAML/Syck — James Edward Gray II <james@...> 2009/11/06

On Nov 6, 2009, at 4:02 AM, Yui NARUSE wrote:

[#26568] Re: [Feature #2340] Removing YAML/Syck — Jon <jon.forums@...> 2009/11/06

> > Issue #2340 has been updated by Yui NARUSE.

[#26571] Re: [Feature #2340] Removing YAML/Syck — "NARUSE, Yui" <naruse@...> 2009/11/06

Jon wrote:

[#26574] Re: [Feature #2340] Removing YAML/Syck — Aaron Patterson <aaron@...> 2009/11/06

On Sat, Nov 07, 2009 at 12:59:25AM +0900, NARUSE, Yui wrote:

[#26635] [Feature #2348] RBTree Should be Added to the Standard Library — James Gray <redmine@...>

Feature #2348: RBTree Should be Added to the Standard Library

20 messages 2009/11/08
[#28842] [Feature #2348] RBTree Should be Added to the Standard Library — James Gray <redmine@...> 2010/03/21

Issue #2348 has been updated by James Gray.

[#26650] [Feature #2350] Unicode specific functionality on String in 1.9 — Manfred Stienstra <redmine@...>

Feature #2350: Unicode specific functionality on String in 1.9

12 messages 2009/11/09
[#28985] [Feature #2350](Rejected) Unicode specific functionality on String in 1.9 — Yusuke Endoh <redmine@...> 2010/03/25

Issue #2350 has been updated by Yusuke Endoh.

[#28993] Re: [Feature #2350](Rejected) Unicode specific functionality on String in 1.9 — Nikolai Weibull <now@...> 2010/03/25

On Thu, Mar 25, 2010 at 14:45, Yusuke Endoh <redmine@ruby-lang.org> wrote:

[#26704] Maintainer confirmation process done. — "Yugui (Yuki Sonoda)" <yugui@...>

I'm sorry for my closing the maintainer confirmation process so late.

13 messages 2009/11/12

[#26736] [Bug #2365] Matrix: poor handling of coercion errors [patch] — Marc-Andre Lafortune <redmine@...>

Bug #2365: Matrix: poor handling of coercion errors [patch]

12 messages 2009/11/14

[#26772] [Bug #2378] Regression in ParseDate.parsedate('nn-nn') — Vladimir Sizikov <redmine@...>

Bug #2378: Regression in ParseDate.parsedate('nn-nn')

10 messages 2009/11/16

[#26774] Ruby constant lookup — Yehuda Katz <wycats@...>

Over the past six months or so, I have been working with the new Ruby 1.9

22 messages 2009/11/16
[#26775] Re: Ruby constant lookup — Shugo Maeda <shugo@...> 2009/11/17

Hi,

[#26777] Re: Ruby constant lookup — Yehuda Katz <wycats@...> 2009/11/17

Shugo,

[#26778] Re: Ruby constant lookup — Shugo Maeda <shugo@...> 2009/11/17

Hi,

[#26869] Caching #to_s for immutables (and a possible future for constant-folding) — Kurt Stephens <ks@...>

I have a proof-of-concept patch to MRI that caches #to_s values for

16 messages 2009/11/23
[#26936] Re: Caching #to_s for immutables (and a possible future for constant-folding) — Roger Pack <rogerdpack@...> 2009/11/29

> =A0It reduces the number of #to_s Strings created during the MRI test sui=

[#26958] Re: Caching #to_s for immutables (and a possible future for constant-folding) [with patch] — Kurt Stephens <ks@...> 2009/11/30

The attached patch add caching of #to_s results to the main immutable

[#26960] Re: Caching #to_s for immutables (and a possible future for constant-folding) [with patch] — Roger Pack <rogerdpack@...> 2009/11/30

> Yes. =A0The MRI test suite runs at 45 sec with these changes and at 53 se=

[#26963] Re: Caching #to_s for immutables (and a possible future for constant-folding) [with patch] — Kurt Stephens <ks@...> 2009/11/30

I just ran rubyspec against it; ~ 5% time improvement.

[ruby-core:26510] Re: Add Standard Deviation Function to Math Module

From: Daniel Cohen <danielc2017@...>
Date: 2009-11-03 21:30:41 UTC
List: ruby-core #26510
Hi,

I am attaching a preliminary version of the statistics module. It implements
both kinds of standard deviation, as well as generating random numbers and
finding values for a normal distribution among other things.

I am currently working on a feature to perform linear curve fitting or
regression on a set of points, however, I am running into trouble on how to
sum the products of the x and y. For example, I have two enumerations xlist
and ylist. Both are the same length. I want to multiply each pair and then
add them to the running total. For example, (x1 * y1) + (x2 * y2) + ... for
however many pairs I have. Any suggestions on how to do this cleanly would
be much appreciated.

The odd include for ruby.h is specific to my development environment.

Sincerely,
Daniel

On Tue, Nov 3, 2009 at 7:39 AM, Daniel Cohen <danielc2017@gmail.com> wrote:

> Matz,
> Thanks for your support, I'll start working on a Statistics Module based on
> [ruby-core:26491]
>
> Thanks,
> Daniel
>
>
> On Tue, Nov 3, 2009 at 6:54 AM, Yukihiro Matsumoto <matz@ruby-lang.org>wrote:
>
>> Hi,
>>
>> In message "Re: [ruby-core:26490] Re: Add Standard Deviation Function to
>> Math   Module"
>>     on Tue, 3 Nov 2009 11:05:36 +0900, Daniel Cohen <
>> danielc2017@gmail.com> writes:
>>
>> |I was thinking that a Statistics module would be a good addition to Ruby.
>> If
>> |you decide that it would be worth creating one, I would be happy to
>> |contribute.
>>
>> Although I have no knowledge and experience about statistics, I would
>> love to support technically.  [ruby-core:26491] would be a good
>> starting point.
>>
>>                                                        matz.
>>
>>
>

Attachments (1)

statistics.c (4.18 KB, text/x-c++src)
/**********************************************************************

  statistics.c -

  $Authors: nobu, dcohen $
  created at: Tue Nov 03 09:42:52 JST 2009

  Copyright (C) 2009 Nobuyoshi Nakada and Daniel Cohen

**********************************************************************/

#include <math.h>
#include <time.h>
#include <stdlib.h>
#include "../include/ruby/ruby.h"

#define SHIFT_FACTOR 100000

struct stat_stdev_args {
   double sum, sum2;
   long num;
};

static VALUE
stdev_i(VALUE elem, VALUE arg, int argc, VALUE *argv)
{
   struct stat_stdev_args *a = (void *)arg;
   double x = NUM2DBL(elem);
   a->sum += x;
   a->sum2 += pow(x, 2);
   a->num++;
   return Qnil;
}

/*
 *  call-seq:
 *     Statistics.stdev(enum)   =>   double
 *
 *  Computes the standard deviation of the elements in an <i>enum</i>
 *  If they are all numbers.
 */

static VALUE
stat_stddev(VALUE obj, VALUE list)
{
   long i;
   struct stat_stdev_args args;
   double mean, stddev;

   args.sum = args.sum2 = 0.0;
   args.num = 0;
   rb_iterate(rb_each, list, stdev_i, (VALUE)&args);
   mean = args.sum / args.num;
   stddev = sqrt(args.sum2 / args.num - mean*mean);
   return DBL2NUM(stddev);
}

/*
 *  call-seq:
 *     Statistics.sum(enum)   =>   double
 *
 *  Computes the sum of the elements in an <i>enum</i>
 *  If they are all numbers.
 */


static VALUE
stat_sum(VALUE obj, VALUE list)
{
   struct stat_stdev_args args;
   
   args.sum = args.sum2 = 0.0;
   args.num = 0;
   rb_iterate(rb_each, list, stdev_i, (VALUE)&args);
   return DBL2NUM(args.sum);
}


/*
 *  call-seq:
 *     Statistics.sum2(enum)   =>   double
 *
 *  Computes the sum of the square of each element in an <i>enum</i>
 *  If they are all numbers.
 */

static VALUE
stat_sum2(VALUE obj, VALUE list)
{
   struct stat_stdev_args args;

   args.sum = args.sum2 = 0.0;
   args.num = 0;
   rb_iterate(rb_each, list, stdev_i, (VALUE)&args);
   return DBL2NUM(args.sum2);
}

/*
 *  call-seq:
 *     Statistics.stdevs(enum)   =>   double
 *
 *  Computes the n-1 or sample standard deviation of the elements in an <i>enum</i>
 *  If they are all numbers.
 */

static VALUE
stat_stddev_s(VALUE obj, VALUE list)
{
	struct stat_stdev_args args;
	double mean, stddevs;
	
	args.sum = args.sum2 = 0.0;
	args.num = 0;
	rb_iterate(rb_each, list, stdev_i, (VALUE)&args);
	mean = args.sum / args.num;
	stddevs = sqrt((args.sum2 - (pow(args.sum,2) / args.num)) / (args.num - 1)); 
	return DBL2NUM(stddevs);
}

/*
 *  call-seq:
 *     Statistics.mean(enum)   =>   double
 *
 *  Computes the mean of the elements in an <i>enum</i>
 *  If they are all numbers.
 */

static VALUE
stat_mean(VALUE obj, VALUE list)
{
	struct stat_stdev_args args;
	double mean, stddevs;
	
	args.sum = args.sum2 = 0.0;
	args.num = 0;
	rb_iterate(rb_each, list, stdev_i, (VALUE)&args);
	mean = args.sum / args.num;
	return DBL2NUM(mean);
}

/*
 *  call-seq:
 *     Statistics.cdf(stdev, mean, x)   =>   double
 *
 *  Computes the Area under a Cumulative Distribution or Normal Distribution
 *  given the <i>standard deviation</i>, <i>mean</i> and <i>value</i> to compute 
 *  the area to the left of
 *  If they are all numbers.
 */

static VALUE
stat_cdf(VALUE obj, VALUE sig, VALUE mu, VALUE x)
{
	double numer, denom;
	numer = x - mu;
	denom = sig * sqrt(2);
	return DBL2NUM(0.5+0.5*erf(numer/denom));
}

/*
 *  call-seq:
 *     Statistics.rand()   =>   double
 *
 *  Computes a random number between 0 and 1
 *  If they are all numbers.
 */

static VALUE
stat_rand(VALUE obj)
{
	double r;
	unsigned int iseed = (unsigned int)time(NULL);
	srand(iseed);
	
	r = (double)rand() / ((double)(RAND_MAX)+(double)(1));
	
	return DBL2NUM(r * (double)(SHIFT_FACTOR) - floor(r * (double)(SHIFT_FACTOR)));
	
}


void
Init_statistics(void)
{
   VALUE mStatistics = rb_define_module("Statistics");
   rb_define_module_function(mStatistics, "stddev", stat_stddev, 1);
   rb_define_module_function(mStatistics, "sum", stat_sum, 1);
   rb_define_module_function(mStatistics, "sum2", stat_sum2, 1);
   rb_define_module_function(mStatistics, "stddevs", stat_stddev_s, 1);
   rb_define_module_function(mStatistics, "mean", stat_mean, 1);
   rb_define_module_function(mStatistics, "cdf", stat_cdf, 3);
   rb_define_module_function(mStatistics, "rand", stat_rand, 0);
}

In This Thread