R/np.udens.compact.public.R

Defines functions .np_udens_compact_public_errors .np_udens_compact_public_prepare .np_udens_compact_active .np_udens_compact_request

.np_udens_compact_request <- function(bws,control,center,method,calibration=NULL,sd.explicit=FALSE) {
  inherits(bws,'bandwidth') && is.null(control) &&
    !identical(calibration,'legacy') &&
    isTRUE(bws[['ckertype',exact=TRUE]]%in%c('uniform','epanechnikov')) &&
    isTRUE(bws[['ndim',exact=TRUE]]==1L) && isTRUE(bws[['ncon',exact=TRUE]]==1L) &&
    identical(bws[['type',exact=TRUE]],'fixed') && isTRUE(bws[['ckerorder',exact=TRUE]]==2L) &&
    isTRUE(bws[['ckerbound',exact=TRUE]]%in%c('fixed','range')) &&
    identical(center,'bias-corrected') && (identical(method,'asymptotic') ||
      (identical(method,'bootstrap') && isTRUE(sd.explicit) &&
       isTRUE(calibration%in%c('symmetric-sd','asymmetric-sd'))))
}

.np_udens_compact_active <- function(state) {
  identical(state[['construction',exact=TRUE]],'density-fixed-compact-cubic-v1') ||
    .np_udens_compact_normal_q2_active(state) || .np_udens_compact_q2_active(state)
}

.np_udens_compact_public_prepare <- function(bws,x,method,bootstrap,band,calibration,alpha,seed) {
  if(!identical(bws[['ckerbound',exact=TRUE]],'fixed'))
    stop("Compact corrected bands require known ckerbound='fixed' bounds; range bounds are not supported",call.=FALSE)
  if(!identical(bws[['type',exact=TRUE]],'fixed') || !isTRUE(bws[['ckerorder',exact=TRUE]]==2L) ||
     !isTRUE(bws[['ndim',exact=TRUE]]==1L) || !isTRUE(bws[['ncon',exact=TRUE]]==bws[['ndim',exact=TRUE]]))
    stop('Compact corrected bands require fixed order-2 bandwidths and one continuous coordinate',call.=FALSE)
  normal <- .np_udens_bounded_normal_request(bws,method,band,calibration,alpha)
  if(!normal && !band%in%c('pointwise','simultaneous','bonferroni','all'))
    stop("Compact corrected bands require explicit band='pointwise', 'simultaneous', 'bonferroni' or 'all'",call.=FALSE)
  variant<-'asymptotic'
  if(identical(method,'bootstrap')) {
    if(!identical(bootstrap,'inid')||!isTRUE(calibration%in%c('symmetric-sd','asymmetric-sd')))
      stop("Compact corrected SD bands require explicit bootstrap='inid' and interval='symmetric-sd' or interval='asymmetric-sd'",call.=FALSE)
    variant<-calibration
  } else if(!identical(method,'asymptotic')||!is.null(calibration))
    stop('Compact corrected bands require asymptotic errors without bootstrap calibration',call.=FALSE)
  if(!is.data.frame(x)||nrow(x)<10L||ncol(x)!=bws[['ndim',exact=TRUE]]||
     anyDuplicated(names(x))||any(!nzchar(names(x)))||!all(vapply(x,is.numeric,logical(1L)))||any(!is.finite(as.matrix(x))))
    stop('Compact corrected bands require at least ten finite numeric observations',call.=FALSE)
  lower<-bws[['ckerlb',exact=TRUE]];upper<-bws[['ckerub',exact=TRUE]]
  if(!is.numeric(lower)||!is.numeric(upper)||length(lower)!=ncol(x)||length(upper)!=ncol(x)||
     any(!is.finite(c(lower,upper)))||any(upper<=lower))
    stop('Compact corrected bands require finite ordered coordinatewise bounds',call.=FALSE)
  width<-upper-lower;jacobian<-prod(width)
  if(any(!is.finite(width))||!is.finite(jacobian)||jacobian<=0||!is.finite(1/jacobian))
    stop('Compact corrected-band support units are not representable',call.=FALSE)
  normalized<-sweep(sweep(as.matrix(x),2,lower),2,width,'/')
  physical.h<-as.double(.np_physical_bandwidth(bws));h<-physical.h/width
  if(length(h)!=ncol(x)||any(!is.finite(h))||any(h<=0)||any(normalized<=0|normalized>=1))
    stop('Compact corrected bands require positive bandwidths and strictly interior observations',call.=FALSE)
  if(!is.numeric(alpha)||length(alpha)!=1L||!is.finite(alpha)||alpha<=0||alpha>=1)
    stop('Compact corrected bands require alpha in (0,1)',call.=FALSE)
  npValidateNonNegativeInteger(seed,'random.seed')
  state<-list(x=x,normalized=normalized,h=h,physical.h=physical.h,lower=lower,width=width,jacobian=jacobian,
    kernel=bws[['ckertype',exact=TRUE]],q=ncol(x),n=nrow(x),variant=variant,plot.law=if(method=='asymptotic')'gaussian' else 'inid',
    plot.random.seed=seed,construction='density-fixed-compact-cubic-v1')
  if(normal)state$normal.pmzsd<-TRUE
  state
}

.np_udens_compact_public_errors <- function(state,grid,alpha,B,band) {
  if(.np_udens_compact_normal_q2_active(state))
    return(.np_udens_compact_normal_q2_public(state,grid,alpha,band))
  if(identical(state[['normal.pmzsd',exact=TRUE]],TRUE))
    return(.np_udens_bounded_normal_public(state,grid,alpha,band,state$kernel))
  if(!is.data.frame(grid)||nrow(grid)<1L||!identical(names(grid),names(state$x))||
     !all(vapply(grid,is.numeric,logical(1L)))||any(!is.finite(as.matrix(grid))))
    stop('invalid Compact corrected-band evaluation frame',call.=FALSE)
  normalized<-sweep(sweep(as.matrix(grid),2,state$lower),2,state$width,'/')
  if(any(normalized<=0|normalized>=1))
    stop('Compact corrected bands require strictly interior evaluation points; literal support endpoints are excluded',call.=FALSE)
  progress<-function(stage,done,total).np_plot_progress_notify(stage=paste('Compact density:',stage),done=done,total=total,unit='work')
  result<-.np_plot_corrected_activity_run('Constructing fixed-bound Compact density intervals',
    if(.np_udens_compact_q2_active(state))
      .np_udens_compact_q2_hidden(state$normalized,state$h,normalized,state$kernel,alpha,band,B,state$plot.random.seed,progress,variant=state$variant,physical=.np_udens_compact_geometry(state,grid))
    else .np_udens_compact_hidden(state$normalized[,1L],state$h,normalized[,1L],state$kernel,alpha,band,B,state$plot.random.seed,progress,variant=state$variant,physical=.np_udens_compact_geometry(state,grid)))
  for(key in c('center','lower','upper','pilot_se','empirical_se'))result[[key]]<-result[[key]]/state$jacobian
  if(any(!is.finite(unlist(result[c('center','lower','upper','pilot_se','empirical_se')]))))
    stop('Compact corrected-band output units are not representable',call.=FALSE)
  interval<-array(c(result$lower,result$upper),c(nrow(grid),2L,1L),dimnames=list(NULL,c('lower','upper'),as.character(alpha)))
  intervals<-setNames(list(interval),band)
  inference<-list(center=result$center,bands=intervals,intervals=intervals,absolute.intervals=intervals,
    pilot.residual.scale=result$pilot_se,empirical.residual.scale=result$empirical_se,
    resolution=list(draws=result$B,signed.lower.index=result$k_signed,
      signed.upper.index=if(!is.null(result$k_signed))result$B+1L-result$k_signed else NULL,
      absolute.index=result$k_absolute,guide.risk=alpha/2,residual.risk=alpha/2,
      signed.tail.probability=alpha/(4*if(band=='bonferroni'&&state$variant!='asymptotic')result$family_size else 1),
      absolute.tail.probability=alpha/(2*if(band=='bonferroni'&&state$variant!='asymptotic')result$family_size else 1),
      gaussian.quantile.type=if(state$variant=='asymptotic'&&band=='simultaneous')7L else NULL),
    settings=list(construction=state$construction,calibration=state$variant,endpoint.construction=result$construction,law=state$plot.law,
      family='one supplied finite interior grid',grid.size=nrow(grid),q=state$q,n=state$n,
      alpha=alpha,B=result$B,resolution.limited=result$B>0L&&(if(state$variant=='asymmetric-sd')floor(result$B*alpha/4) else floor(result$B*alpha/2))<20L,
      critical=result$critical,available.bands=band,
      bounds=cbind(state$lower,state$lower+state$width),bandwidth=state$h*state$width,
      guide.risk=alpha/2,residual.risk=alpha/2,
      calibration.status=if(state$variant=='asymptotic')'fixed-bound compact cubic complete pilot/empirical residual envelope plus guide region'else
        'fixed-bound compact refitted residual bootstrap at original empirical scale; complete pilot/empirical interval envelope plus guide region'),
    numerical.checks=result$checks)
  out<-list(boot.err=cbind(result$center-result$lower,result$upper-result$center,result$center),
    boot.all.err=NULL,inference=inference,bxp=list())
  if(band%in%c('bonferroni','all'))out<-.np_udens_bounded_public_companions(out,result,band,state$jacobian,alpha)
  out
}

Try the np package in your browser

Any scripts or data that you put into this service are public.

np documentation built on Nov. 25, 2026, 1:06 a.m.