⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datamanager.cpp

📁 阿伦方差的计算
💻 CPP
字号:
// datamanager.cpp: implementation of the datamanager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "arlunSigma.h"
#include "datamanager.h"
#include <math.h>
#define PI 3.1415926
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

datamanager::datamanager(double miu,double sigma)
{
	this->sampleNum=20000;
	
	double min=miu-3*sigma;
	double max=miu+3*sigma;
	
	srand((unsigned)time(NULL));
	this->sampleValues=new double[sampleNum];
	
	for (int i=0;i<sampleNum;i++)
	{
		double x=0;
		double dScope;
		double y;
		do
		{
			x = AverageRandom(min,max); 
			y = Normal(x, miu, sigma);
			dScope = AverageRandom(0, Normal(miu,miu,sigma));
		}while( dScope > y);
		
		sampleValues[i]=x;
	}
	
	i=0;
	int k=0;
	this->xNum=10000;
	this->timeInterval=new double[xNum];
	for(int j=0;j<xNum;j++)
	{
		timeInterval[j]=0.01*(j+1);
	}
	this->sigmaValues=new double[this->xNum];
	for (i=0;i<this->xNum;i++)
	{
		double t0=timeInterval[i];
		int interval=(int)(t0/0.01);
		int n=(int)((double)sampleNum/interval);
		double *avg=new double[n];
		for (j=0;j<n;j++)
		{
			double tt=0;
			for (k=interval*j;k<interval*(j+1);k++)
			{
				if (k>=sampleNum){
					break;
				}
				tt+=sampleValues[k];
			}
			if (k>=sampleNum){
				avg[j]=tt/(sampleNum-(n-1)*interval);
			}else
				avg[j]=tt/interval;
		}
		double op1=0,op2=0;
		for (j=0;j<n;j++)
		{
			op1+=(avg[j]*avg[j]);
		}
		for (j=0;j<n;j++)
		{
			op2+=avg[j];
		}
		op2=op2*op2;
		sigmaValues[i]=sqrt((n*op1-op2)/(n*(n-1)));
		delete []avg;
	}

}

datamanager::~datamanager()
{

}

double datamanager::AverageRandom(double min, double max)
{
	int minInteger = (int)(min*10000);
	int maxInteger = (int)(max*10000);
	int randInteger = rand()*rand();
	int diffInteger = maxInteger - minInteger;
	int resultInteger = randInteger % diffInteger + minInteger;
	return resultInteger/10000.0;
}

double datamanager::Normal(double x, double miu, double sigma)
{
	return 1.0/sqrt(2*PI*sigma) *
		exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -