为风速时间序列添加季节性变化

时间:2017-07-24 08:32:17

标签: r hidden-markov-models probability-density markov weibull

R blog跟进,这对于使用Weibull参数模拟未知区域的时间序列非常有用且非常有用。

虽然这种方法给出了一个相当好的时间序列估计,但是当我们寻找季节性变化时,它会受到很大的影响。为了解释季节性变化,我想采用季节性最大风速并进行时间序列合成,使年度分布保持不变,即。形状和尺度参数(年度值)。

我希望通过使用12种不同的最大风速(每个月每个风速)对下面的代码使用季节性最大风速。这样可以在某个月允许更大的风速,在其他月份允许更低的风速,甚至可以使得到的时间序列更均匀。

代码如下:

MeanSpeed<-7.29 ## Mean Yearly Wind Speed at the site.

Shape=2; ## Input Shape parameter (yearly).
Scale=8 ##Calculated Scale Parameter ( yearly).

MaxSpeed<-17 (##yearly)
## $$$ 12 values of these wind speed one for each month to be used. The resultant time series should satisfy shape and scale parameters $$ ###
nStates<-16 

nRows<-nStates;
nColumns<-nStates;


LCateg<-MaxSpeed/nStates; 


WindSpeed=seq(LCateg/2,MaxSpeed-LCateg/2,by=LCateg) ## Fine the velocity vector-centered on the average value of each category.

##Determine Weibull Probability Distribution.
wpdWind<-dweibull(WindSpeed,shape=Shape, scale=Scale); # Freqency distribution.

plot(wpdWind,type = "b", ylab= "frequency", xlab = "Wind Speed")  ##Plot weibull probability distribution.

norm_wpdWind<-wpdWind/sum(wpdWind); ## Convert weibull/Gaussian distribution to normal distribution.

## Correlation between states (Matrix G)
g<-function(x){2^(-abs(x))} ## decreasing correlation function between states.
G<-matrix(nrow=nRows,ncol=nColumns)
G <- row(G)-col(G)
G <- g(G)

##--------------------------------------------------------


## iterative process to calculate the matrix P (initial probability)
P0<-diag(norm_wpdWind);   ## Initial value of the MATRIX P.
P1<-norm_wpdWind;  ## Initial value of the VECTOR p.


## This iterative calculation must be done until a certain error is exceeded
## Now, as something tentative, I set the number of iterations

steps=1000;  
P=P0; 
p=P1; 

for (i in 1:steps){
    r<-P%*%G%*%p;
    r<-as.vector(r/sum(r)); ## The above result is in matrix form. I change it to vector
    p=p+0.5*(P1-r)
    P=diag(p)}

   ## $$ ----Markov Transition Matrix --- $$ ##

N=diag(1/as.vector(p%*%G));## normalization matrix

MTM=N%*%G%*%P ## Markov Transition Matrix

MTMcum<-t(apply(MTM,1,cumsum));## From the MTM generated the accumulated

##-------------------------------------------
## Calculating the series from the MTMcum

##Insert number of data sets. 
LSerie<-52560; Wind Speed every 10 minutes for a year. 

RandNum1<-runif(LSerie);## Random number to choose between states
State<-InitialState<-1;## assumes that the initial state is 1 (this must be changed when concatenating days)
StatesSeries=InitialState;

## Initallise----

## The next state is selected to the one in which the random number exceeds the accumulated probability value
##The next iterative procedure chooses the next state whose random number is greater than the cumulated probability defined by the MTM
for (i in 2:LSerie) {
  ## i has to start on 2 !!
    State=min(which(RandNum1[i]<=MTMcum[State,]));

    ## if (is.infinite (State)) {State = 1}; ## when the above condition is not met max -Inf
    StatesSeries=c(StatesSeries,State)}

RandNum2<-runif(LSerie); ## Random number to choose between speeds within a state

SpeedSeries=WindSpeed[StatesSeries]-0.5+RandNum2*LCateg;
##where the 0.5 correction is needed since the the WindSpeed vector is centered around the mean value of each category.


print(fitdistr(SpeedSeries, 'weibull')) ##MLE fitting of SpeedSeries

有人可以建议我需要对代码进行哪些更改以及哪些更改?

1 个答案:

答案 0 :(得分:0)

我对生成风速时间序列知之甚少,但这些指南可能会帮助您提高代码可读性/可重用性:

#1 您可能希望拥有一个可以产生风速时间的功能 系列给出了一些观测值和季节性最大风速。因此,首先尝试在块中定义代码,如下所示:

wind_time_serie <- function(nobs, max_speed){
    #some code here
}

#2 这样做,如果您的代码的某些部分似乎对生成风速时间序列有用,但与风速时间序列无关,请尝试将它们放入函数中(例如您计算的部分norm_wpdWind,您计算的部分MTMcum,...)。

#3 然后,当您的define全局变量应该消失并成为函数中的默认参数时,代码中的部分开头。

#4 当您的行已经很长时,请避免使用结束注释,并删除结束的分号。

#This
  State<-InitialState<-1;## assumes that the initial state is 1 (this must be changed when concatenating days)

#Would become this:
  #Assumes that the initial state is 1 (this must be changed when concatenating days)
  State<-InitialState<-1

然后您的代码应该更容易被其他人重用/读取。下面是一个应用于rnorm部分的指南的示例:

  norm_distrib<-function(maxSpeed, states = 16, shape = 2, scale = 8){

    #Fine the velocity vector-centered on the average value of each category.
    LCateg<-maxSpeed/states
    WindSpeed=seq(LCateg/2,maxSpeed-LCateg/2,by=LCateg) 

    #Determine Weibull Probability Distribution.
    wpdWind<-dweibull(WindSpeed,shape=shape, scale=scale)

    #Convert weibull/Gaussian distribution to normal distribution.
    return(wpdWind/sum(wpdWind))

  }

  #Plot normal distribution with the max speed you want (e.g. 17)
  plot(norm_distrib(17),type = "b", ylab= "frequency", xlab = "Wind Speed")