Created
October 2, 2012 07:04
-
-
Save ryuichimatsumoto-single/3816956 to your computer and use it in GitHub Desktop.
平均、分散、標準偏差の求め方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
double Average(double *data,int n); | |
double Verb(double *data,int n); | |
double Deviation(double *data,int n); | |
//平均を計算 | |
double Average(double *data,int n) | |
{ | |
int i; | |
double average=0; | |
for(i=0;i<n;i++) | |
{ | |
average += data[i]; | |
} | |
return (average/n); | |
} | |
//分散を計算 | |
double Verb(double *data,int n) | |
{ | |
int i; | |
double average=0; | |
double verb=0; | |
average = Average(data,n); | |
for(i=0;i<n;i++) | |
{ | |
verb += data[i]*data[i]; | |
} | |
verb /=n; | |
verb -= average*average; | |
return verb; | |
} | |
//標準偏差を計算 | |
double Deviation(double *data,int n) | |
{ | |
return(Verb(data,n)); | |
} | |
int main(void) | |
{ | |
double data[3]={1.0,1.0,1.0};//テスト用の配列 | |
const int n = sizeof data /sizeof data[0];//配列の要素数 | |
printf("標本数:%d\n",n); | |
printf("平均:%f\n",Average(data,n)); | |
printf("分散:%f\n",Verb(data,n)); | |
printf("標準偏差:%f\n",Deviation(data,n)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
*実行結果は:http://ideone.com/bltVV | |
*/ | |
//平均の算出 | |
function Average($array){ | |
$result=0; | |
foreach($array as $value) | |
{ | |
$result += $value; | |
} | |
$result =($result/count($array)); | |
return $result; | |
} | |
//分散の算出 | |
function Verb($array) | |
{ | |
$result=0; | |
$E_x = Average($array); | |
foreach($array as $value) | |
{ | |
$result += $value*$value; | |
} | |
//V(X)=E(X^2)-E(X)^2 より、分散を算出 | |
$result /= count($array); //E(X^2)を算出 | |
$result -= $E_x * $E_x;//E(X)^2を引く | |
return $result; | |
} | |
function Deviation($array) | |
{ | |
return sqrt(Verb($array)); | |
} | |
//テスト用の入力値 | |
$array_1= Array(1,1,1,1,1); | |
$array_2= Array(1,2,3); | |
//結果1 | |
printf("平均:%f,分散:%f,標準偏差:%f",Average($array_1),Verb($array_1),Deviation($array_1)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment