Last updated: 2021-07-31

Checks: 6 1

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.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

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 031cb94. 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/local/
    Ignored:    renv/staging/

Unstaged changes:
    Modified:   R/functions.R
    Modified:   analysis/vsa_basic_operators.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 dcdf309 Ross Gayler 2021-07-20 WIP
html 5f5c544 Ross Gayler 2021-07-20 WIP
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

For this project we are using bipolar vectors (\(V \in \{-1, +1\}^D\)).

The vectors will be dense. That is, there will be no zero elements. We will not be investigating the effect of sparsity in this project.

Define a function to create a randomly selected bipolar VSA vector.

# function to make an atomic VSA vector

vsa_mk_atom_bipolar <- 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 (much) 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)
}

The seed argument allows for a reproducible random selection.

The vector elements are integers rather than floats. This halves the required storage space and is a minor gesture towards optimisation. Much greater optimisation could be probably be achieved by using bit strings rather than integers, but that’s not worth the effort at this stage.

Where possible all other operations will be defined to accept floats and integers to allow moving away from a strictly bipolar representation if necessary.

Do some very small scale testing.

v1 <- vsa_mk_atom_bipolar(10L)
v2 <- vsa_mk_atom_bipolar(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
all(v1 == v2)
[1] FALSE
  • Multiple calls generate different vectors.
v1 <- vsa_mk_atom_bipolar(10L, seed = 1L)
v2 <- vsa_mk_atom_bipolar(10L, seed = 1L)

v1
 [1] -1  1 -1 -1  1 -1 -1 -1  1  1
v2
 [1] -1  1 -1 -1  1 -1 -1 -1  1  1
all(v1 == v2)
[1] TRUE
  • Setting the seed to the same value generates the same vector.

2 Vector measures

We really only need the cosine similarity of two vectors. However, define functions for the components of the cosine in case they are useful.

2.1 Vector magnitude

Define a function to calculate the of a VSA vector.

# function to calculate the magnitude of a VSA vector
# Allow for the possibility that the vector might not be bipolar

vsa_mag <- function(
  v1 # numeric - VSA vector (not necessarily bipolar)
) # value # numeric - magnitude of the VSA vector
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1)) 
    stop("VSA vector argument (v1) must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be an numeric vector")
 
  # No numerical analysis considerations 
  sqrt(sum(v1*v1))
}

I have not taken any numerical analysis considerations into account, so don’t hold any strong expectations for accuracy in extreme cases.

Do some very small scale testing.

vsa_mag(0)
[1] 0
vsa_mag(1)
[1] 1
vsa_mag(2)
[1] 2
vsa_mag(-2)
[1] 2
  • The magnitude of a scalar is its absolute value.
vsa_mk_atom_bipolar(9L) %>% vsa_mag()
[1] 3
vsa_mk_atom_bipolar(100L) %>% vsa_mag()
[1] 10
vsa_mk_atom_bipolar(1e4L) %>% vsa_mag()
[1] 100
vsa_mk_atom_bipolar(1e8L) %>% vsa_mag()
[1] 10000
  • The magnitude of a bipolar vector is the square root of its dimensionality.

As the vector dimensionality is increased the operations take longer to execute and eventually something will break, e.g. you will run out of RAM or the dimensionality will be too large to be represented as an integer.

(vsa_mk_atom_bipolar(100L) * 1L) %>% vsa_mag()
[1] 10
(vsa_mk_atom_bipolar(100L) * 1.3) %>% vsa_mag()
[1] 13
(vsa_mk_atom_bipolar(100L) * -5) %>% vsa_mag()
[1] 50
  • Rescaling the vector is equivalent to rescaling its magnitude (\(\Vert kV \Vert = \vert k \vert \Vert V \Vert\)).

2.2 Vector dot product

Define a function to calculate the dot product of two VSA vectors.

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

vsa_dotprod <- 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 (v1, v2) must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be a numeric vector")
  
  if(!is.vector(v2, mode = "numeric"))
    stop("v2 must be a numeric vector")
  
  vsa_dim <- length(v1)
  
  if(length(v2) != vsa_dim)
    stop("v1 and v2 must be the same length")
  
  # No numerical analysis considerations 
    sum(v1*v2)
}

I have not taken any numerical analysis considerations into account, so don’t hold any strong expectations for accuracy in extreme cases.

Do some very small scale testing.

vsa_dotprod(1, 1)
[1] 1
vsa_dotprod(1, 0)
[1] 0
vsa_dotprod(1, 3)
[1] 3
vsa_dotprod(2, 3)
[1] 6
vsa_dotprod(-2, 3)
[1] -6
vsa_dotprod(-2, -3)
[1] 6
  • The dot product of two scalars is their product.
vsa_dotprod(vsa_mk_atom_bipolar(9L, seed = 42L), vsa_mk_atom_bipolar(9L, seed = 42L))
[1] 9
vsa_dotprod(vsa_mk_atom_bipolar(100L, seed = 43L), vsa_mk_atom_bipolar(100L, seed = 43L))
[1] 100
vsa_dotprod(vsa_mk_atom_bipolar(1e4L, seed = 44L), vsa_mk_atom_bipolar(1e4L, seed = 44L))
[1] 10000
vsa_dotprod(vsa_mk_atom_bipolar(1e8L, seed = 45L), vsa_mk_atom_bipolar(1e8L, seed = 45L))
[1] 100000000
  • The dot product of a vector with itself is equal to the square of its magnitude.
  • The dot product of a bipolar vector with itself is equal to its dimensionality.
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 108
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] -68
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 88
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 2
  • The dot product of two randomly selected high dimensional vectors is approximately zero (relative to the dimensionality).

2.3 Vector cosine similarity

Define a function to calculate the cosine of the angle between 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_bipolar(10L)
v2 <- vsa_mk_atom_bipolar(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.6
vsa_sim(v1, v2/3)
[1] -0.6
  • 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 21 22 33 34 15 26 17 38 29
vsa_add(x1,x2,x3)
 [1] 20 31 12 23 14 15 16 27 28 39
vsa_add(x1,x2,x3)
 [1] 10 31 22 23 24 15 16 37 28 29
  • 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] magrittr_2.0.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        workflowr_1.6.2  
 [5] R6_2.5.0          rlang_0.4.11      fansi_0.5.0       stringr_1.4.0    
 [9] tools_4.1.0       xfun_0.24         utf8_1.2.2        git2r_0.28.0     
[13] htmltools_0.5.1.1 ellipsis_0.3.2    rprojroot_2.0.2   yaml_2.2.1       
[17] digest_0.6.27     tibble_3.1.3      lifecycle_1.0.0   bookdown_0.22    
[21] crayon_1.4.1      later_1.2.0       vctrs_0.3.8       promises_1.2.0.1 
[25] glue_1.4.2        evaluate_0.14     rmarkdown_2.9     stringi_1.7.3    
[29] compiler_4.1.0    pillar_1.6.2      httpuv_1.6.1      renv_0.14.0      
[33] pkgconfig_2.0.3