R/datagennoml.R

Defines functions datagenNoml

Documented in datagenNoml

#' Data Generating Function
#'
#'@description A function creating parameters with abrupt
#'changes and sequential data generated by those parameters
#'in Gaussian form.
#'@param T Index of time series data.
#'@param k Number of abrupt changes
#'@param a Parameter space, upper bound.
#'@param b Parameter space, lower bound.
#'@param sig Variance of normal distribution with 1 as default.
#'@return Return `theta_true` as ture underlying paramaeters and
#'`Y` as data sequence.
#'
#'@export
#'@examples
#'
#'data=datagenNoml(T=1000,k=6,a=-10,b=10,sig=1)
#'y=data[[1]]
#'theta_t=data[[2]]
#'plot(data.frame(x=1:T,y=y),type='l',col='lightblue',
#'ylim=c(-10,10),ylab='Generate Data/True mean',xlab='time index')
#'par(new=TRUE)
#'plot(data.frame(x=1:T,y=theta_t),type='l',
#'col='red',ylim=c(-10,10),ylab='',xlab='',axes=F)
#'title(main="Normal Data Sequence")
#'


datagenNoml = function(T,k,a,b,sig=1)
{
  #generate the data sequence: from normal dist. with changing means
  #also return the true parameter sequence
  #k: number of abrupt changes
  if(k==0)
  {
    theta_true=(a+(b-a)*runif(1))*rep(1,T)
    Y=rnorm(T,theta_true,sig*rep(1,T))
  }


  change_point=floor(T/(k+1))*c(1:k)
  change_point=c(1,change_point,T)
  theta_list=a+(b-a)*runif(k+1)
  theta_true=rep(0,T)

  for(j in 1:(k+1))
  {
    theta_true[change_point[j]:change_point[j+1]]=theta_list[j]
  }

  #generate data
  Y=rnorm(T,theta_true,sig*rep(1,T))

  return(list(Y,theta_true))

}
azure10h/PFSMC documentation built on Feb. 5, 2020, 5:11 a.m.