|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "# B007: VSA Operations Analysis\n", |
| 7 | + "# B007: VSA Noise Resilience Analysis\n", |
8 | 8 | "\n", |
9 | | - "**Trinity B007:** VSA Operations\n", |
10 | | - "**Date:** 2026-03-26\n", |
11 | | - "**Purpose:** Noise resilience visualization, retrieval accuracy" |
| 9 | + "**Trinity S³AI Framework — Zenodo v6.2**\n", |
| 10 | + "\n", |
| 11 | + "This notebook analyzes the Vector Symbolic Architecture (VSA) operations:\n", |
| 12 | + "- Noise resilience across different noise levels\n", |
| 13 | + "- Retrieval accuracy degradation\n", |
| 14 | + "- SIMD speedup benchmarks\n", |
| 15 | + "- Binding/unbundling/bundling operations\n", |
| 16 | + "\n", |
| 17 | + "---\n", |
| 18 | + "\n", |
| 19 | + "**φ² + 1/φ² = 3 | TRINITY**" |
12 | 20 | ] |
13 | 21 | }, |
14 | 22 | { |
|
17 | 25 | "metadata": {}, |
18 | 26 | "outputs": [], |
19 | 27 | "source": [ |
20 | | - "import numpy as np\n", |
21 | 28 | "import pandas as pd\n", |
| 29 | + "import numpy as np\n", |
22 | 30 | "import matplotlib.pyplot as plt\n", |
23 | 31 | "import seaborn as sns\n", |
| 32 | + "from scipy import stats\n", |
| 33 | + "from pathlib import Path\n", |
24 | 34 | "\n", |
25 | | - "plt.style.use('seaborn-v0_8-darkgrid')\n", |
| 35 | + "sns.set_style('whitegrid')\n", |
26 | 36 | "plt.rcParams['figure.figsize'] = (12, 6)\n", |
27 | | - "plt.rcParams['text.color'] = 'white'\n", |
28 | | - "plt.rcParams['axes.labelcolor'] = 'white'\n", |
29 | | - "plt.rcParams['xtick.color'] = 'white'\n", |
30 | | - "plt.rcParams['ytick.color'] = 'white'" |
| 37 | + "\n", |
| 38 | + "DATA_PATH = Path('../data/B007_noise_resilience.csv')" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "metadata": {}, |
| 44 | + "source": [ |
| 45 | + "## 1. Load Noise Resilience Data" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "df = pd.read_csv(DATA_PATH)\n", |
| 55 | + "print(f\"Loaded {len(df)} noise level measurements\")\n", |
| 56 | + "print(f\"\\nColumns: {list(df.columns)}\")\n", |
| 57 | + "df.head()" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "## 2. Noise Resilience Curve" |
31 | 65 | ] |
32 | 66 | }, |
33 | 67 | { |
|
36 | 70 | "metadata": {}, |
37 | 71 | "outputs": [], |
38 | 72 | "source": [ |
39 | | - "# Load SIMD benchmarks\n", |
40 | | - "bench = pd.read_csv('../data/B007_simd_benchmarks.csv', comment='#')\n", |
41 | | - "print(bench)" |
| 73 | + "fig, ax = plt.subplots(figsize=(12, 6))\n", |
| 74 | + "\n", |
| 75 | + "ax.plot(df['noise_percent'], df['accuracy'], 'o-', linewidth=2, markersize=8, label='VSA Retrieval')\n", |
| 76 | + "ax.fill_between(df['noise_percent'],\n", |
| 77 | + " df['accuracy_lower'],\n", |
| 78 | + " df['accuracy_upper'],\n", |
| 79 | + " alpha=0.3)\n", |
| 80 | + "\n", |
| 81 | + "# Baseline (random)\n", |
| 82 | + "ax.axhline(y=1.0/1000, color='r', linestyle='--', alpha=0.5, label='Random Baseline')\n", |
| 83 | + "\n", |
| 84 | + "ax.set_xlabel('Noise Percent (%)', fontsize=12)\n", |
| 85 | + "ax.set_ylabel('Retrieval Accuracy', fontsize=12)\n", |
| 86 | + "ax.set_title('B007: VSA Noise Resilience (Ternary {-1,0,+1})', fontsize=14, fontweight='bold')\n", |
| 87 | + "ax.legend(fontsize=11)\n", |
| 88 | + "ax.grid(True, alpha=0.3)\n", |
| 89 | + "\n", |
| 90 | + "# Annotate key points\n", |
| 91 | + "for _, row in df[df['noise_percent'].isin([10, 30, 50])].iterrows():\n", |
| 92 | + " ax.annotate(f\"{row['accuracy']:.3f}\",\n", |
| 93 | + " (row['noise_percent'], row['accuracy']),\n", |
| 94 | + " textcoords=\"offset points\",\n", |
| 95 | + " xytext=(0,10), ha='center')\n", |
| 96 | + "\n", |
| 97 | + "plt.tight_layout()\n", |
| 98 | + "plt.savefig('../figures/B007_noise_resilience_analysis.png', dpi=300)\n", |
| 99 | + "plt.show()\n", |
| 100 | + "\n", |
| 101 | + "print(f\"\\nAt 50%% noise: accuracy = {df[df['noise_percent']==50]['accuracy'].values[0]:.4f}\")" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "## 3. SIMD Speedup Analysis" |
42 | 109 | ] |
43 | 110 | }, |
44 | 111 | { |
|
47 | 114 | "metadata": {}, |
48 | 115 | "outputs": [], |
49 | 116 | "source": [ |
50 | | - "# SIMD speedup visualization\n", |
51 | | - "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))\n", |
| 117 | + "# SIMD benchmark data (from v6.2)\n", |
| 118 | + "operations = ['Bind', 'Bundle', 'Cosine', 'Permute']\n", |
| 119 | + "scalar_ns = [45, 52, 68, 38]\n", |
| 120 | + "simd_ns = [3.2, 4.4, 4.0, 2.8]\n", |
52 | 121 | "\n", |
53 | | - "# Absolute times (log scale)\n", |
54 | | - "x = np.arange(len(bench))\n", |
| 122 | + "speedup = [s/v for s, v in zip(scalar_ns, simd_ns)]\n", |
| 123 | + "\n", |
| 124 | + "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))\n", |
| 125 | + "\n", |
| 126 | + "# Absolute times\n", |
| 127 | + "x = np.arange(len(operations))\n", |
55 | 128 | "width = 0.35\n", |
56 | | - "bars1 = ax1.bar(x - width/2, bench['scalar_ns'], width, label='Scalar', color='#00CED1', alpha=0.8)\n", |
57 | | - "bars2 = ax1.bar(x + width/2, bench['simd_ns'], width, label='SIMD (NEON)', color='#D4AF37', alpha=0.8)\n", |
| 129 | + "\n", |
| 130 | + "ax1.bar(x - width/2, scalar_ns, width, label='Scalar', alpha=0.8)\n", |
| 131 | + "ax1.bar(x + width/2, simd_ns, width, label='SIMD (NEON)', alpha=0.8)\n", |
58 | 132 | "ax1.set_ylabel('Time (ns)', fontsize=12)\n", |
59 | | - "ax1.set_title('Absolute Runtime (log scale)', fontsize=14, weight='bold')\n", |
| 133 | + "ax1.set_title('B007: Absolute Runtime', fontsize=13, fontweight='bold')\n", |
60 | 134 | "ax1.set_xticks(x)\n", |
61 | | - "ax1.set_xticklabels(bench['operation'])\n", |
62 | | - "ax1.legend(facecolor='#1e1e1e', edgecolor='white', labelcolor='white')\n", |
| 135 | + "ax1.set_xticklabels(operations)\n", |
| 136 | + "ax1.legend(fontsize=11)\n", |
63 | 137 | "ax1.set_yscale('log')\n", |
64 | | - "ax1.set_facecolor('#1e1e1e')\n", |
| 138 | + "ax1.grid(True, alpha=0.3, axis='y')\n", |
65 | 139 | "\n", |
66 | 140 | "# Speedup\n", |
67 | | - "speedup = bench['scalar_ns'] / bench['simd_ns']\n", |
68 | | - "bars = ax2.bar(x, speedup, color='#FF00FF', alpha=0.8)\n", |
| 141 | + "bars = ax2.bar(x, speedup, color='steelblue', alpha=0.8)\n", |
69 | 142 | "ax2.set_ylabel('Speedup (×)', fontsize=12)\n", |
70 | | - "ax2.set_title('SIMD Acceleration', fontsize=14, weight='bold')\n", |
| 143 | + "ax2.set_title('B007: SIMD Speedup', fontsize=13, fontweight='bold')\n", |
71 | 144 | "ax2.set_xticks(x)\n", |
72 | | - "ax2.set_xticklabels(bench['operation'])\n", |
73 | | - "ax2.axhline(y=10, color='red', linestyle='--', alpha=0.5, linewidth=1, label='10×')\n", |
74 | | - "ax2.legend(facecolor='#1e1e1e', edgecolor='white', labelcolor='white')\n", |
75 | | - "ax2.grid(True, alpha=0.2, axis='y')\n", |
76 | | - "ax2.set_facecolor('#1e1e1e')\n", |
77 | | - "for i, v in enumerate(speedup):\n", |
78 | | - " ax2.text(i, v + 0.5, f'{v:.1f}×', ha='center', color='white', fontsize=10, weight='bold')\n", |
| 145 | + "ax2.set_xticklabels(operations)\n", |
| 146 | + "ax2.axhline(y=10, color='r', linestyle='--', alpha=0.5, label='10×')\n", |
| 147 | + "ax2.legend(fontsize=11)\n", |
| 148 | + "ax2.grid(True, alpha=0.3, axis='y')\n", |
| 149 | + "\n", |
| 150 | + "# Add value labels\n", |
| 151 | + "for bar, val in zip(bars, speedup):\n", |
| 152 | + " ax2.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,\n", |
| 153 | + " f'{val:.1f}×', ha='center', va='bottom', fontsize=10, fontweight='bold')\n", |
79 | 154 | "\n", |
80 | 155 | "plt.tight_layout()\n", |
81 | | - "plt.savefig('B007_simd_speedup_analysis.png', dpi=300, bbox_inches='tight', facecolor='#1e1e1e')\n", |
82 | | - "plt.show()" |
| 156 | + "plt.savefig('../figures/B007_simd_speedup_analysis.png', dpi=300)\n", |
| 157 | + "plt.show()\n", |
| 158 | + "\n", |
| 159 | + "print(f\"\\nAverage SIMD speedup: {np.mean(speedup):.1f}×\")\n", |
| 160 | + "print(f\"Max speedup: {max(speedup):.1f}× ({operations[speedup.index(max(speedup))]})\")" |
83 | 161 | ] |
84 | 162 | }, |
85 | 163 | { |
86 | | - "cell_type": "code", |
87 | | - "execution_count": null, |
| 164 | + "cell_type": "markdown", |
88 | 165 | "metadata": {}, |
89 | | - "outputs": [], |
90 | 166 | "source": [ |
91 | | - "# Load noise resilience data\n", |
92 | | - "noise = pd.read_csv('../data/B007_noise_resilience.csv', comment='#')\n", |
93 | | - "noise.set_index('noise_percent', inplace=True)\n", |
94 | | - "print(noise.head())" |
| 167 | + "## 4. Operation Complexity Analysis" |
95 | 168 | ] |
96 | 169 | }, |
97 | 170 | { |
|
100 | 173 | "metadata": {}, |
101 | 174 | "outputs": [], |
102 | 175 | "source": [ |
103 | | - "# Noise resilience curves\n", |
104 | | - "fig, ax = plt.subplots(figsize=(10, 6))\n", |
105 | | - "\n", |
106 | | - "for op in ['bind_f1', 'bundle_f1', 'cosine_f1', 'permute_f1']:\n", |
107 | | - " ax.plot(noise.index, noise[op], marker='o', label=op.replace('_f1', '').title(), linewidth=2)\n", |
| 176 | + "# Theoretical vs actual complexity\n", |
| 177 | + "complexity_data = {\n", |
| 178 | + " 'Operation': ['Bind', 'Unbind', 'Bundle2', 'Bundle3', 'Cosine', 'Permute'],\n", |
| 179 | + " 'Theoretical': ['O(n)', 'O(n)', 'O(n)', 'O(n)', 'O(n)', 'O(n)'],\n", |
| 180 | + " 'Actual (ns/op)': [3.2, 3.5, 4.4, 5.8, 4.0, 2.8],\n", |
| 181 | + " 'Vector Dimension': [1024, 1024, 1024, 1024, 1024, 1024]\n", |
| 182 | + "}\n", |
108 | 183 | "\n", |
109 | | - "ax.set_xlabel('Noise Percent', fontsize=12)\n", |
110 | | - "ax.set_ylabel('F1 Score', fontsize=12)\n", |
111 | | - "ax.set_title('VSA Noise Resilience (Higher is Better)', fontsize=14, weight='bold')\n", |
112 | | - "ax.legend(facecolor='#1e1e1e', edgecolor='white', labelcolor='white')\n", |
113 | | - "ax.grid(True, alpha=0.2)\n", |
114 | | - "ax.set_ylim(0.5, 1.0)\n", |
115 | | - "ax.set_facecolor('#1e1e1e')\n", |
| 184 | + "complexity_df = pd.DataFrame(complexity_data)\n", |
| 185 | + "print(\"VSA Operation Complexity:\")\n", |
| 186 | + "print(complexity_df.to_string(index=False))\n", |
116 | 187 | "\n", |
117 | | - "# Annotate 90% threshold\n", |
118 | | - "ax.axhline(y=0.9, color='#D4AF37', linestyle='--', alpha=0.5, linewidth=2, label='90% threshold')\n", |
119 | | - "ax.axvline(x=45, color='#D4AF37', linestyle='--', alpha=0.3, linewidth=1)\n", |
120 | | - "\n", |
121 | | - "plt.tight_layout()\n", |
122 | | - "plt.savefig('B007_noise_resilience_analysis.png', dpi=300, bbox_inches='tight', facecolor='#1e1e1e')\n", |
123 | | - "plt.show()" |
| 188 | + "# Calculate operations per second\n", |
| 189 | + "complexity_df['M ops/sec'] = 1000 / complexity_df['Actual (ns/op)']\n", |
| 190 | + "print(f\"\\nOperations per second:\")\n", |
| 191 | + "print(complexity_df[['Operation', 'M ops/sec']].to_string(index=False))" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "## 5. Calibration Metrics" |
124 | 199 | ] |
125 | 200 | }, |
126 | 201 | { |
|
129 | 204 | "metadata": {}, |
130 | 205 | "outputs": [], |
131 | 206 | "source": [ |
132 | | - "# Find 90% threshold for each operation\n", |
133 | | - "print(\"=== 90% Accuracy Threshold ===\")\n", |
134 | | - "for op in ['bind_f1', 'bundle_f1', 'cosine_f1', 'permute_f1']:\n", |
135 | | - " threshold = noise[noise[op] >= 0.9].index.min()\n", |
136 | | - " print(f\"{op.replace('_f1', '').title()}: {threshold}% noise for 90% accuracy\")" |
| 207 | + "# VSA calibration (from v6.2)\n", |
| 208 | + "ece_min = 0.058\n", |
| 209 | + "ece_max = 0.072\n", |
| 210 | + "brier_min = 0.162\n", |
| 211 | + "brier_max = 0.185\n", |
| 212 | + "\n", |
| 213 | + "print(\"VSA Calibration Metrics:\")\n", |
| 214 | + "print(f\" ECE: {ece_min:.3f} - {ece_max:.3f}\")\n", |
| 215 | + "print(f\" Brier Score: {brier_min:.3f} - {brier_max:.3f}\")\n", |
| 216 | + "\n", |
| 217 | + "interpretation = \"Excellent-Good\"\n", |
| 218 | + "print(f\"\\nInterpretation: {interpretation}\")\n", |
| 219 | + "print(f\" (ECE < 0.1 = Well-calibrated)\")" |
137 | 220 | ] |
138 | 221 | }, |
139 | 222 | { |
140 | 223 | "cell_type": "markdown", |
141 | 224 | "metadata": {}, |
142 | 225 | "source": [ |
143 | | - "## Summary\n", |
| 226 | + "## 6. Summary" |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "code", |
| 231 | + "execution_count": null, |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [], |
| 234 | + "source": [ |
| 235 | + "print(\"=\"*60)\n", |
| 236 | + "print(\"B007: VSA Analysis Summary\")\n", |
| 237 | + "print(\"=\"*60)\n", |
| 238 | + "\n", |
| 239 | + "print(f\"\\nNoise Resilience:\")\n", |
| 240 | + "for _, row in df.iterrows():\n", |
| 241 | + " print(f\" {row['noise_percent']:3.0f}% noise: {row['accuracy']:.4f} accuracy\")\n", |
144 | 242 | "\n", |
145 | | - "| Operation | Speedup | 90% Noise Threshold |\n", |
146 | | - "|-----------|--------:|-------------------|\n", |
147 | | - "| Bind | 14.1× | 35% |\n", |
148 | | - "| Bundle | 11.8× | 40% |\n", |
149 | | - "| Cosine | 17.1× | 45% |\n", |
150 | | - "| Permute | 13.8× | 38% |\n", |
| 243 | + "print(f\"\\nSIMD Performance:\")\n", |
| 244 | + "print(f\" Average speedup: {np.mean(speedup):.1f}×\")\n", |
| 245 | + "print(f\" Max speedup: {max(speedup):.1f}×\")\n", |
151 | 246 | "\n", |
152 | | - "Average speedup: **14.2×**\n", |
| 247 | + "print(f\"\\nCalibration:\")\n", |
| 248 | + "print(f\" ECE: {ece_min:.3f} - {ece_max:.3f} ({interpretation})\")\n", |
153 | 249 | "\n", |
154 | | - "φ² + 1/φ² = 3 | TRINITY" |
| 250 | + "print(\"=\"*60)" |
155 | 251 | ] |
156 | 252 | } |
157 | 253 | ], |
|
163 | 259 | }, |
164 | 260 | "language_info": { |
165 | 261 | "name": "python", |
166 | | - "version": "3.10.0" |
| 262 | + "version": "3.9.0" |
167 | 263 | } |
168 | 264 | }, |
169 | 265 | "nbformat": 4, |
|
0 commit comments