您的位置:首页 > 其它

时间序列之 季节系数法

2012-04-14 21:31 211 查看
// jijie.cpp

//实现季节系数法,对时间序列进行预测

//作者:郭运凯

//email:38436655@qq.com

#include <stdio.h>

#include <vector>

#include <IOSTREAM>

#include <string.h>

using namespace std;

const int NSIZE = 100;

const int YAER_NUM = 12;

typedef struct DATA

{

int year;

int month;

int cheliangs;

}DATA;

typedef struct NODE

{

int duanmianbh;

vector<DATA> jtl;

double avg; //所有月份的平均值

double month_avg[12]; //每个月的平均值

double xishu[12]; //月份的系数

double pre_year; //预测年份的值

double pre_month[12]; //预测每月的值

double year[50];

int num_year; //整年数

}NODE;

vector <NODE> DB;

void initnode(NODE & t)

{

if (t.jtl.size() != 0)

{

t.jtl.erase(t.jtl.begin(),t.jtl.end());

}

t.avg = 0;

t.pre_year = 0;

t.num_year = 0;

for (int i = 0; i< 12;i++)

{

t.month_avg[i] = 0;

t.xishu[i] = 0;

t.pre_month[i] = 0;

}

for (i = 0;i < 50;i++)

{

t.year[i] = 0;

}

}

void readdata()

{

FILE * fp;

fp = fopen("data.txt","r");

int row_num = 0;

char chs[NSIZE];

while (fgets(chs,NSIZE,fp) != NULL)

{

row_num ++;

}

fclose(fp);

fp = fopen("data.txt","r");

//如下是data.txt数据文件内容,代码无法运行时,新建一个data.txt将注释部分复制到其中

/*

1000 2007 01 12843

1000 2007 02 17031

1000 2007 03 14923

1000 2007 04 16423

1000 2007 05 17574

1000 2007 06 14553

1000 2007 07 15124

1000 2007 08 15614

1000 2007 09 17270

1000 2007 10 17835

1000 2007 11 15176

1000 2007 12 14327

*/

int duanmianbh;

NODE t;

DATA tempdata;

initnode(t);

fscanf(fp,"%d\t%d\t%d\t%d\n",&duanmianbh,&tempdata.year,&tempdata.month,&tempdata.cheliangs);

t.duanmianbh = duanmianbh;

t.jtl.push_back(tempdata);

for (int i = 1;i < row_num;i++)

{

fscanf(fp,"%d\t%d\t%d\t%d\n",&duanmianbh,&tempdata.year,&tempdata.month,&tempdata.cheliangs);

if(t.duanmianbh != duanmianbh)

{

//新的节点

DB.push_back(t);

initnode(t);

t.duanmianbh = duanmianbh;

t.jtl.push_back(tempdata);

}

else

{

t.jtl.push_back(tempdata);

}

}

DB.push_back(t);

for (i = 0;i <DB.size();i++)

{

printf("[%d]\n",i);

for (int j = 0;j < DB[i].jtl.size();j++)

{

printf("%d\t%d\t%d\n",DB[i].jtl[j].year,DB[i].jtl[j].month,DB[i].jtl[j].cheliangs);

}

printf("\n");

}

fclose(fp);

}

void month_predict()

{

//1、求完整的年份

//2、计算前面完整年份的各月的车辆数的总和,然后求出平均数

//3、计算前面完整年份的各月的车辆数的平均数

//4、计算前面完整年份的各月的系数

//5、

printf("begin to call month_predict\n");

for (int i = 0;i <DB.size();i++)

{

DB[i].num_year = DB[i].jtl.size()/YAER_NUM; //1、求完整的年份

printf("%d\n",DB[i].num_year);

printf("[%d][%d]\n",i,DB[i].num_year);

/*t.month_avg[i] = 0;

t.xishu[i] = 0;

t.pre_month[i] = 0;

*/

//2、计算前面完整年份的各月的车辆数的总和,然后求出平均数

for (int j = 0;j < DB[i].num_year ;j++)

{

for (int k = 0;k < YAER_NUM;k++)

{

int cheliangs = DB[i].jtl[j*YAER_NUM + k ].cheliangs;

DB[i].avg += cheliangs; //计算总和

DB[i].month_avg[ k ] += cheliangs; //计算每月的总和

DB[i].year[j] += cheliangs; //计算每年的总和

printf("%d\t%d\t%d\n",DB[i].jtl[j*YAER_NUM + k ].year,DB[i].jtl[j*YAER_NUM + k ].month,DB[i].jtl[j*YAER_NUM + k ].cheliangs);

}

printf("\n");

}

//2、开始求平均数

DB[i].avg /= DB[i].num_year * YAER_NUM; //计算总的平均数

for (j = 0;j < YAER_NUM;j++)

{

DB[i].month_avg[j] /= DB[i].num_year; //每月的平均数

DB[i].xishu[j] = DB[i].month_avg[j] / DB[i].avg; //每月的月度系数

printf("%lf\t",DB[i].xishu[j]);

}

int sn = 0;

for (j = 0;j < DB[i].num_year;j++)

{

DB[i].pre_year += (DB[i].year[j] * (j + 1) ) ; //加权

sn += (j + 1) ; //加权值的和

}

DB[i].pre_year /= (sn*YAER_NUM); //预测年份的加权值

printf("%lf\n",DB[i].pre_year);

int start = DB[i].num_year * YAER_NUM -1;

for (j = 0;j < YAER_NUM;j++)

{

DB[i].pre_month[j] = DB[i].pre_year * DB[i].xishu[j];

printf("%5.f\t%d\t%5.f\t%5.2f\n",DB[i].pre_month[j],DB[i].jtl[start + j].cheliangs,DB[i].pre_month[j]-DB[i].jtl[start + j].cheliangs,100*(DB[i].pre_month[j]-DB[i].jtl[start + j].cheliangs)/DB[i].jtl[start + j].cheliangs);

}

printf("\n");

}

}

void main()

{

readdata();

month_predict();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: