R中的风速时间序列模拟

时间:2017-07-21 11:37:17

标签: r timestamp time-series markov-chains probability-distribution

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

虽然这种方法给出了一个相当好的时间序列估计,但是当我们寻找季节性变化时,它会受到很大的影响。 让我们看一个例子:

对于一组特定的Weibull参数,此方法可以给出低于几个月的风速:

  

7.492608

     

Feb 7.059587

     

3月7.261821

     

Apr 7.192106

     

7.399982

     

Jun 7.195889

     

7月7.290898

     

8月7.210269

     

7.219063

     

Oct 7.307073

     

Nov 7.135451

     

Dec 7.315633

可以看出,风速的变化并不那么大,实际上,变化会从一个月变为另一个月。如果我在7月和6月的某个月优先考虑11月和12月的几个月,那么威布尔将保持不变。我该怎么做?

在上述链接中列出的代码中进行这些更改的任何主管或建议都会有很大帮助。

根据要求,这里是示例代码。

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

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

MaxSpeed<-17 
nStates<-16 

这些是博客中的输入,MeanSpeed是具有提供的形状和比例参数的位置的年平均风速。 MaxSpeed是一年中可能达到的最高速度。

我想让Maxspeed每个月都说Maxspeed_Jan,Maxspeed_feb ......直到Maxspeed_dec。都有不同的价值观。这应该能够反映全年风速变化的季节性。

然后以某种方式计算以下内容,以反映输出时间序列中的这种变化。

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

获得的结果必须类似于输入的Scale和Shape参数。而不是每个月获得均匀的风速,变化将反映每个月的输入最大风速。

谢谢。

0 个答案:

没有答案