时间序列之 季节系数法
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();
}
//实现季节系数法,对时间序列进行预测
//作者:郭运凯
//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();
}
相关文章推荐
- Excel在统计分析中的应用—第十三章—时间序列分析-季节变动的测定
- [时间序列分析][6]--季节差分
- 互相关系数cross correlation-自相关系数-时间序列,图像
- [时间序列分析][2]--趋势和(季节)因子
- Excel在统计分析中的应用—第十三章—时间序列分析-季节变动的测定(季节虚拟变量回归法)
- 互相关系数cross correlation-自相关系数-时间序列,图像
- 经典贪心法:时间序列问题及其全局最优性证明
- 时间序列完全教程(R)
- 时间序列的概念、种类及编制原则
- [转]时间序列用户生命周期的聚类方法
- pandas 时间序列、绘图、存储文件 date_range()
- pandas小记:pandas时间序列分析和处理Timeseries
- 时间序列分解算法:STL
- 最大子序列和问题的求解(时间复杂度为O(N))
- 基于时间序列的股票趋势预测模型
- R语言 时间序列分析之ARIMA模型预测
- HBase在时间序列数据库中的应用
- 最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现
- 非平稳时间序列确定性因素分解
- 时间序列数据库的秘密(2)——索引