アットウィキロゴ

統計 > 標準偏差とか > ソース

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <sys/time.h>
using namespace std;
#define li        long long int
#define rep(i,to) for(li i=0;i<((li)(to));++i)
#define pb        push_back
#define sz(v)     ((li)(v).size())
#define bit(n)    (1ll<<(li)(n))
#define all(vec)  (vec).begin(),(vec).end()
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define MP        make_pair
#define F         first
#define S         second
 
 
const int MAX=160;
double a[MAX];
 
double get(li maxi){ 
	return (double)rand()*maxi/(RAND_MAX-1.0);
}
 
void init(){
	rep(i,MAX){
		int times=100;
		double sum=0,R=10000000;
		rep(j,times){
			double x=get(R);
			double y=get(R);
			if(x*x+y*y<=R*R) sum++;
		}
		a[i]=4.0*sum/times;
	}
}
 
double mean(){
	double res=0;
	rep(i,MAX) res+=a[i];
	return res/MAX;
}
 
double bunsan(){
	double res=0,m=mean();
	rep(i,MAX) res+=(m-a[i])*(m-a[i])/MAX;
	return res;
}
 
double huhenbunsan(){
	double res=0,m=mean();
	rep(i,MAX) res+=(m-a[i])*(m-a[i])/(MAX-1);
	return res;
}
 
double hyoujunhensa(){
	return sqrt(huhenbunsan());
}
 
double hyoujungosa(){
	return hyoujunhensa()/sqrt(MAX);
}
 
bool print_95(bool flag){
	double low =mean()-hyoujungosa()*1.980;
	double high=mean()+hyoujungosa()*1.980;
	double sum=0;
	rep(i,MAX)if(low<=a[i] && a[i]<high) sum++;
	if(flag) printf("95%% : %0.10lf < pi < %0.10lf\n",low,high);
	return low<=acos(-1) && acos(-1)<high;
}
 
int main(){
	double sum=0;
	int times=1000;
	rep(i,times){
		init();
		if(i==0){
			cout<<"平均   :"<<mean()<<endl;
			cout<<"分散   :"<<bunsan()<<endl;
			cout<<"不変分散 :"<<huhenbunsan()<<endl;
			cout<<"標準偏差 :"<<hyoujunhensa()<<endl;
			cout<<"標準誤差 :"<<hyoujungosa()<<endl;
		}
		if(print_95(i==0)) sum++;
	}
	printf("95%%信頼区間: %0.2lf%%\n",sum*100.0/times);
}
 
最終更新:2012年02月17日 07:51