Last updated: 2021-07-20

Checks: 7 0

Knit directory: VSA_altitude_hold/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210617) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version fad972d. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    renv/library/
    Ignored:    renv/staging/

Untracked files:
    Untracked:  analysis/simulation_framework.Rmd

Unstaged changes:
    Modified:   analysis/index.Rmd
    Deleted:    analysis/test_simulation_framework.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/vsa_basic_operators.Rmd) and HTML (docs/vsa_basic_operators.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html 3f691b8 Ross Gayler 2021-07-20 WIP
html b2c3085 Ross Gayler 2021-07-13 WIP
Rmd b57b029 Ross Gayler 2021-07-13 WIP
html b57b029 Ross Gayler 2021-07-13 WIP
Rmd 2fd00dc Ross Gayler 2021-07-13 WIP
html 2fd00dc Ross Gayler 2021-07-13 WIP
Rmd eccb5b1 Ross Gayler 2021-07-11 WIP
html f7a9155 Ross Gayler 2021-07-11 WIP
Rmd e040157 Ross Gayler 2021-07-11 WIP

This notebook …

The reasoning behind the design choices is explained in XXX.

1 Generate atomic vectors

Define a function to calculate the weighted sum of an arbitrary number of VSA vectors.

# function to make an atomic VSA vector

vsa_mk_atom <- function(
  vsa_dim, # integer - dimensionality of VSA vector
  seed = NULL # integer - seed for random number generator
) # value # one randomly selected VSA vector of dimension vsa_dim
{  
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  if(missing(vsa_dim))
    stop("vsa_dim must be specified")
  
  if(!(is.vector(vsa_dim, mode = "integer") && length(vsa_dim) == 1))
    stop("vsa_dim must be an integer")
  
  if(vsa_dim < 1)
    stop("vsa_dim must be (a lot) greater than zero")
  
  # check that the specified seed is an integer
  if(!is.null(seed) &&!(is.vector(seed, mode = "integer") && length(seed) == 1))
    stop("seed must be an integer")
  
  # if seed is set the the vector is fixed
  # otherwise it is randomised
  set.seed(seed)
  
  # Construct a random bipolar vector
  sample(c(-1L, 1L), size = vsa_dim, replace = TRUE)
}

Do some very small scale testing.

vsa_mk_atom(10L)
 [1]  1 -1 -1  1  1 -1 -1 -1 -1  1
vsa_mk_atom(10L)
 [1]  1 -1 -1 -1 -1 -1 -1 -1  1  1
  • Multiple calls generate different vectors.
vsa_mk_atom(10L, seed = 1L)
 [1] -1  1 -1 -1  1 -1 -1 -1  1  1
vsa_mk_atom(10L, seed = 1L)
 [1] -1  1 -1 -1  1 -1 -1 -1  1  1
  • Setting the seed to the same value generates the same vector.

2 Cosine similarity of vectors

Define a function to calculate the cosine similarity of two VSA vectors.

# function to calculate the cosine similarity  of two VSA vectors
# Allow for the possibility that the vectors might not be bipolar

vsa_sim <- function(
  v1, v2 # numeric - VSA vectors of identical dimension (not necessarily bipolar)
) # value # numeric - cosine similarity of the VSA vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1) || missing(v2)) 
    stop("two VSA vector arguments must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be an numeric vector")
  
  if(!is.vector(v2, mode = "numeric"))
    stop("v2 must be an numeric vector")
  
  vsa_dim <- length(v1)
  
  if(length(v2) != vsa_dim)
    stop("v1 and v2 must be the same length")
  
  sum(v1*v2) / sqrt(sum(v1*v1) * sum(v2*v2))
}

Do some very small scale testing.

# create vectors to add
v1 <- vsa_mk_atom(10L)
v2 <- vsa_mk_atom(10L)

v1
 [1] -1  1  1  1 -1  1  1 -1  1 -1
v2
 [1]  1  1  1 -1 -1  1 -1  1 -1  1
vsa_sim(v1, v1)
[1] 1
vsa_sim(v1, -v1)
[1] -1
vsa_sim(v1, v2)
[1] -0.2
vsa_sim(v1, v2/3)
[1] -0.2
  • ksk asklwhfklhfo iqhwdhdmnd

3 Multiply vectors

4 Add vectors

Define a function to calculate the weighted sum of an arbitrary number of VSA vectors.

# function to add (weighted sum) an arbitrary number of VSA vectors
# Weighted add is implemented as weighted sampling from the source vectors

vsa_add <- function(
  ..., # >= 2 VSA vectors of identical dimension as arguments to add
  sample_spec, # integer vector - sorce (argument VSA vector) for each element of result
  sample_wt # numeric vector - argument vector sampling weights
) # value # one VSA vector, the weighted sum (sampled) of the argument vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  args_list <- list(...)
  args_n <- length(args_list)
  
  if(args_n < 2) 
    stop("number of source VSA vector arguments must be >= 2")
  
  if(!all(sapply(args_list, is.vector, mode = "integer")))
    stop("all source VSA vectors must be integer vectors")
  
  vsa_dim <- length(args_list[[1]])
  
  if(!all(sapply(args_list, length) == vsa_dim))
    stop("all source VSA vectors must be the same length")
  
  if(!missing(sample_spec) && !missing(sample_wt))
    stop("at most one of wt and sample_spec can be given")
  
  if(!missing(sample_spec))
    # sample_spec supplied
  {
    if(!is.vector(sample_spec, mode = "integer"))
      stop("sample_spec must be an integer vector")
    
    if(length(sample_spec) != vsa_dim)
      stop("sample_spec must be same length as source VSA vectors")
    
    if(!all(sample_spec %in% 1:args_n))
      stop("each element of sample_spec must be the index of a source VSA vector")
  }
  else
    # sample spec not supplied - make a new random one
  {
    # create a sampling weight vector if not supplied
    if(missing(sample_wt))
      sample_wt <- rep(1, length.out = args_n) # equal weighting for all source VSA vectors
    
    if(length(sample_wt) != args_n)
      stop("number of weights must equal number of source VSA vectors")
    
    if(min(sample_wt) < 0)
      stop("all weights must be >= 0")
    
    if(max(sample_wt) <= 0)
      stop("at least one weight must be > 0")
    
    # For each element of the result work out which source VSA vector to sample
    sample_spec <- sample.int(n = args_n, size = vsa_dim,
                              replace = TRUE, prob = sample_wt)
  }
  
  ### Set up the selection matrix ###
  # Each row corresponds to an element of the output vector
  # Each row specifies the (row,col) cell to select from the VSA source vectors
  sel <- as.matrix(data.frame(row = 1L:vsa_dim, col = sample_spec),
                   ncol = 2, byrow = FALSE)
  
  ### Construct the result vector
  as.data.frame(args_list)[sel]
}

Do some very small scale testing.

# create vectors to add
# make unique valuse so they can be uniquely tracked
x1 <- 10L:19L
x2 <- 20L:29L
x3 <- 30L:39L

# specify the sampling
vsa_add(x1,x2,x3, sample_spec = c(1L,2L,3L,1L,2L,3L,1L,2L,3L,1L))
 [1] 10 21 32 13 24 35 16 27 38 19
vsa_add(x1,x2,x3, sample_spec = c(1L,2L,3L,1L,2L,3L,1L,2L,3L,1L))
 [1] 10 21 32 13 24 35 16 27 38 19
  • Sampling is fixed when sample_spec is specified.
vsa_add(x1,x2,x3, sample_wt = c(0, 0, 1))
 [1] 30 31 32 33 34 35 36 37 38 39
  • Extreme random weighting works as expected.
vsa_add(x1,x2,x3)
 [1] 20 31 32 33 34 25 36 27 18 29
vsa_add(x1,x2,x3)
 [1] 10 31 12 13 14 15 26 27 28 29
vsa_add(x1,x2,x3)
 [1] 10 21 12 13 14 15 16 37 28 19
  • Randomised sampling is different on every occasion.

5 Negate vectors

6 Permute vectors


sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 21.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] here_1.0.1 fs_1.5.0  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        whisker_0.4       knitr_1.33        magrittr_2.0.1   
 [5] workflowr_1.6.2   R6_2.5.0          rlang_0.4.11      fansi_0.5.0      
 [9] stringr_1.4.0     tools_4.1.0       xfun_0.24         utf8_1.2.1       
[13] git2r_0.28.0      htmltools_0.5.1.1 ellipsis_0.3.2    rprojroot_2.0.2  
[17] yaml_2.2.1        digest_0.6.27     tibble_3.1.2      lifecycle_1.0.0  
[21] bookdown_0.22     crayon_1.4.1      later_1.2.0       vctrs_0.3.8      
[25] promises_1.2.0.1  glue_1.4.2        evaluate_0.14     rmarkdown_2.9    
[29] stringi_1.7.2     compiler_4.1.0    pillar_1.6.1      httpuv_1.6.1     
[33] renv_0.13.2       pkgconfig_2.0.3