Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 3029630
  • /
  • optimPPclust.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:04eae90d38caca6f253dc719ef4dd1667142ea81
directory badge
swh:1:dir:30296303457c13ed29284e041ce0e44c333d8a55

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
optimPPclust.R
#### A modification of R's default optimisation to allow, in particular, verbosity levels used
#### in Hofmeyr and Pavlidis (2017)

### function ppclust.optim() finds the optimal projection vector for clustering using one of the
### objectives in Hofmeyr and Pavlidis (2017)
## arguments:
# v0 = vector. Initial projection vector
# f = function. objective function to be optimised
# df = function. gradient of objective
# X = matrix. dataset being clustered
# params = list. parameters used in f and df
# getb = function. determines the optimal hyperplane orthogonal to a vector v (the value of b in the description of a hyperplane in the paper)
# verbosity = integer. determines the verbosity level of the optimisation. see paper for details
# labels = vector. only used if verbosity > 0. plots of points with different labels are given different colours. the labels are
#           not used to aid the optimisation, only for illustrative purposes
# method = character. describes the objective being used. one of c('MCDC', 'MDH', 'NCutH')
# maxit = integer. maximum number of iterations in optimisation.
# ftol = double. realtive tolerance for termination of optimisation

## output is the optimal projection vector

ppclust.optim <- function(v0, f, df, X, params, getb, verbosity, labels, method, maxit, ftol){
  if(verbosity==0){
    if(method=='MCDC') optim(v0, f, df, X, params, method='BFGS', control = list(fnscale = -1e-4, maxit = maxit, reltol = ftol))
    else optim(v0, f, df, X, params, method='BFGS', control = list(maxit = maxit, reltol = ftol))
  }
  else{
    iter = 0
    if(verbosity==3){
      dirname <- paste(method, ' plots.', format(Sys.time(), "%m_%d_%y_%Ih%Mm%Ss"), sep = '')
      if(method=='MDH') dirname <- paste(dirname, '. alpha=', params$alpha, sep = '')
      dir.create(dirname)
      count <- 0
    }
    if(ncol(X)>2) v2 <- rARPACK::eigs_sym(cov(X), 2)$vectors[,2]
    else v2 <- eigen(cov(X))$vectors[,2]
    v0 <- list(par = v0)
    for(i in 1:30){
      v2 <- v2-v2%*%v0$par*v0$par
      xp <- X%*%cbind(v0$par, v2)
      if(method=='MDH') den <- density(xp[,1], bw = params$h)
      else den <- density(xp[,1])
      if(verbosity==3){
        count <- count + 1
        pdf(paste('~/', dirname, '/', method, ' plot-', count, '.pdf', sep = ''))
      }
      if(is.null(labels)) plot(xp, col = rgb(.5, .5, .5), xlab = '', ylab = '', main = '')
      else plot(xp, col = labels, xlab = '', ylab = '', main = '')
      lines(den$x, min(xp[,2])+.5*den$y/max(den$y)*(max(xp[,2])-min(xp[,2])), lwd = 2)
      if(method=='MDH'){
        s <- sd(xp[,1])
        pen <- den$y + params$C*((den$x<(-params$alpha*s))*(-params$alpha*s-den$x)^2 + (den$x>(params$alpha*s))*(den$x-params$alpha*s)^2)
        lines(den$x, min(xp[,2])+.5*pen/max(den$y)*(max(xp[,2])-min(xp[,2])), lty = 2)
      }
      bcrit <- getb(v0$par, X, params)
      abline(v=bcrit, lwd = 2, col = 2)
      if(verbosity>1){
        SR <- ifelse(is.null(labels), NA, round(success_ratio(xp[,1]<bcrit, labels), 3))
        if(method=='MDH'){
          reldep <- round(md_reldepth(v0$par, X, params), 3)
          denval <- round(f_md(v0$par, X, params), 3)
          title(main = paste('alpha = ', params$alpha, '. density = ', denval, '. relative depth = ', reldep, '. success ratio = ', SR, sep = ''))
        }
        else if(method=='NCutH'){
          NC <- round(f_ncut(v0$par, X, params), 3)
          title(main = paste('normalised cut = ', NC, '. success ratio = ', SR, sep = ''))
        }
        else{
          VR <- round(f_mc(v0$par, X, params), 3)
          title(main = paste('variance ratio = ', VR, '. success ratio = ', SR, sep = ''))
        }
      }
      if(verbosity==3) dev.off()
      if(method=='MCDC') v0 <- optim(v0$par, f, df, X, params, method = 'BFGS', control = list(fnscale = -1e-4, maxit=3, reltol = ftol))
      else v0 <- optim(v0$par, f, df, X, params, method = 'BFGS', control = list(maxit=3, reltol = ftol))
      Sys.sleep(.05)
      v0$par <- v0$par/norm_vec(v0$par)
      iter <- iter + v0$counts[2]
      if(v0$counts[2]==1 || i==30 || iter > maxit) break
    }
    v0
  }
}


back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API