【问题标题】:Adding seasonal variations to wind speed time series向风速时间序列添加季节变化
【发布时间】:2017-12-29 18:06:54
【问题描述】:

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

谁能建议我需要对代码进行哪些更改和更改?

【问题讨论】:

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


    【解决方案1】:

    我对生成风速时间序列了解不多,但也许这些指南可以帮助您提高代码的可读性/可重用性:

    #1 你可能想要一个生成风速时间的函数 系列给出了一些观察结果和季节性最大风速。所以首先尝试在这样的块中定义你的代码:

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

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

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

    #4当你的行已经很长时避免使用endline cmets,并删除结束的半列。

    #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") 
    

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 2014-08-20
      • 1970-01-01
      • 2016-07-26
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多