Sampling Functions¶
Functions for generating random basis hypervectors.
sample_random¶
vsax.sampling.sample_random(dim, n, key=None)
¶
Sample n random real-valued vectors from normal distribution.
Generates random vectors with elements drawn from a standard normal distribution N(0, 1). These are suitable for use with MAP operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Dimensionality of each vector. |
required |
n
|
int
|
Number of vectors to sample. |
required |
key
|
Optional[PRNGKey]
|
JAX random key. If None, uses PRNGKey(0). |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
JAX array of shape (n, dim) containing sampled vectors. |
Example
import jax key = jax.random.PRNGKey(42) vectors = sample_random(512, 10, key) assert vectors.shape == (10, 512) assert not jnp.iscomplexobj(vectors)
Source code in vsax/sampling/random.py
sample_complex_random¶
vsax.sampling.sample_complex_random(dim, n, key=None)
¶
Sample n random complex-valued vectors with random phases.
Generates unit-magnitude complex vectors with uniformly random phases in [0, 2π). These are suitable for use with FHRR operations.
The vectors have the form: exp(i * θ) where θ ~ Uniform(0, 2π).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Dimensionality of each vector. |
required |
n
|
int
|
Number of vectors to sample. |
required |
key
|
Optional[PRNGKey]
|
JAX random key. If None, uses PRNGKey(0). |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
JAX array of shape (n, dim) containing complex unit-magnitude vectors. |
Example
import jax key = jax.random.PRNGKey(42) vectors = sample_complex_random(512, 10, key) assert vectors.shape == (10, 512) assert jnp.iscomplexobj(vectors)
All magnitudes should be 1.0¶
assert jnp.allclose(jnp.abs(vectors), 1.0)
Source code in vsax/sampling/random.py
sample_fhrr_random¶
vsax.sampling.sample_fhrr_random(dim, n, key=None)
¶
Sample n random real-valued vectors suitable for FHRR operations.
Generates random vectors by sampling in the frequency domain with conjugate symmetry, ensuring the IFFT produces real-valued results. This is the mathematically correct way to generate random vectors for FHRR circular convolution operations.
The frequency-domain representation satisfies: - F[0] is real (DC component) - F[k] = conj(F[D-k]) for k=1..D-1 (conjugate symmetry) - For even D: F[D/2] is real (Nyquist frequency)
This ensures that ifft(F) produces real-valued vectors (imaginary part is negligible numerical noise), which are suitable for FHRR binding and unbinding operations with high accuracy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Dimensionality of each vector (must be >= 2). |
required |
n
|
int
|
Number of vectors to sample. |
required |
key
|
Optional[PRNGKey]
|
JAX random key. If None, uses PRNGKey(0). |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
JAX array of shape (n, dim) containing real-valued vectors |
ndarray
|
suitable for FHRR operations. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dim < 2. |
Example
import jax from vsax.ops import FHRROperations from vsax.similarity import cosine_similarity key = jax.random.PRNGKey(42) vectors = sample_fhrr_random(512, 10, key) assert vectors.shape == (10, 512) assert not jnp.iscomplexobj(vectors)
Use with FHRR operations¶
ops = FHRROperations() a, b = vectors[0], vectors[1] bound = ops.bind(a, b) recovered = ops.unbind(bound, b)
High similarity due to correct sampling¶
assert cosine_similarity(recovered, a) > 0.99
Note
This function differs from sample_complex_random() in that it enforces conjugate symmetry in the frequency domain, guaranteeing real-valued time-domain vectors. Use this function for FHRR applications that work in the time domain with real-valued vectors.
Source code in vsax/sampling/random.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
sample_binary_random¶
vsax.sampling.sample_binary_random(dim, n, key=None, bipolar=True)
¶
Sample n random binary vectors.
Generates random binary vectors with values uniformly sampled from: - Bipolar mode: {-1, +1} - Binary mode: {0, 1}
These are suitable for use with Binary VSA operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Dimensionality of each vector. |
required |
n
|
int
|
Number of vectors to sample. |
required |
key
|
Optional[PRNGKey]
|
JAX random key. If None, uses PRNGKey(0). |
None
|
bipolar
|
bool
|
If True, sample from {-1, +1}. If False, sample from {0, 1}. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
JAX array of shape (n, dim) containing binary values. |
Example
import jax key = jax.random.PRNGKey(42)
Bipolar sampling¶
bipolar_vecs = sample_binary_random(512, 10, key, bipolar=True) assert bipolar_vecs.shape == (10, 512) assert jnp.all(jnp.isin(bipolar_vecs, jnp.array([-1, 1])))
Binary sampling¶
binary_vecs = sample_binary_random(512, 10, key, bipolar=False) assert jnp.all(jnp.isin(binary_vecs, jnp.array([0, 1])))