|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# BSS-ANOVA eigendecomposition at increasing resolution (to later find asymptotic eigenvalue ratios)\n", |
| 8 | + "\n", |
| 9 | + "Importing packages." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 4, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import numpy as np\n", |
| 19 | + "import matplotlib.pyplot as plt\n", |
| 20 | + "from scipy.interpolate import CubicSpline\n", |
| 21 | + "from scipy.optimize import curve_fit" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "Defining the BSS-ANOVA kernel main effect $\\kappa_1$, outlined in equation 8 of [Fast variable selection makes Karhunen-Loève decomposed Gaussian process BSS-ANOVA a speedy and accurate choice for dynamic systems identification](docs/_static/arXiv.2205.13676v2.pdf),\n", |
| 29 | + "\n", |
| 30 | + "$\\kappa_1(x,x') = \\mathcal{B}(x)\\mathcal{B}_1(x') + \\mathcal{B}_2(x)\\mathcal{B}_2(x') + \\frac{1}{24}\\mathcal{B}_4(|x-x'|)$\n", |
| 31 | + "\n", |
| 32 | + "where\n", |
| 33 | + "\n", |
| 34 | + "$\\begin{cases} \\mathcal{B}_1(x) = x - \\frac{1}{2} \\\\ \\mathcal{B}_2(x) = x^2 - x + \\frac{1}{6} \\\\ \\mathcal{B}_4(x) = x^4 - 2x^3 + x^2 - \\frac{1}{30} \\end{cases}$" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 5, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "def b1(x):\n", |
| 44 | + " return x - 1/2\n", |
| 45 | + "\n", |
| 46 | + "def b2(x):\n", |
| 47 | + " return x**2 - x + 1/6\n", |
| 48 | + "\n", |
| 49 | + "def b4(x):\n", |
| 50 | + " return x**4 - 2*x**3 + x**2 - 1/30\n", |
| 51 | + "\n", |
| 52 | + "def k1(xi, xj):\n", |
| 53 | + " return b1(xi)*b1(xj) + b2(xi)*b2(xj) - b4(np.abs(xi-xj))/24" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "Taking eigenvalues for increasing resolution of covariance matrix (i.e., BSS-ANOVA kernel). Because only 20 Bernoulli polynomials could be computed in MATLAB prior to significant rounding error in plots, only need first 20 eigenvalues." |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 22, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "name": "stderr", |
| 70 | + "output_type": "stream", |
| 71 | + "text": [ |
| 72 | + "/tmp/ipykernel_28833/3318184751.py:18: ComplexWarning: Casting complex values to real discards the imaginary part\n", |
| 73 | + " eigvals[res_iter, :] = eigval[:n] # in future, plot columns which are basis function scales\n" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "source": [ |
| 78 | + "n = 20 # number of Bernoulli polynomials (i.e., number of eigenvalues to save)\n", |
| 79 | + "res_n = 5 # number of points to plot\n", |
| 80 | + "res_lb = 1600 # lower bound (of plot)\n", |
| 81 | + "res_ub = 2000 # upper bound (of plot)\n", |
| 82 | + "\n", |
| 83 | + "eigvals = np.zeros([res_n, n])\n", |
| 84 | + "res_x = np.linspace(res_lb, res_lb + np.round((res_ub-res_lb)/(res_n-1))*(res_n-1), res_n, dtype=int)\n", |
| 85 | + "res_iter = 0\n", |
| 86 | + "for res in res_x:\n", |
| 87 | + " x = np.linspace(0, 1, res)\n", |
| 88 | + " kernel = np.zeros([res, res])\n", |
| 89 | + "\n", |
| 90 | + " for i in range(res):\n", |
| 91 | + " for j in range(res):\n", |
| 92 | + " kernel[i, j] = k1(x[i], x[j])\n", |
| 93 | + " eigval, eigvec = np.linalg.eig(kernel)\n", |
| 94 | + "\n", |
| 95 | + " eigvals[res_iter, :] = eigval[:n] # in future, plot columns which are basis function scales\n", |
| 96 | + " res_iter += 1\n", |
| 97 | + "\n", |
| 98 | + "progress = np.concatenate([res_x[:, np.newaxis], eigvals], axis=1)\n", |
| 99 | + "np.savetxt(f'current_progress_{res_lb}_{res_ub}.txt', progress) # res points by basis function order (i.e., 'k' or eigenvalue id)\n", |
| 100 | + "\n", |
| 101 | + "# !!! NOTE !!!\n", |
| 102 | + "# Manually combine multiple 'current_progress_{res_lb}_{res_ub}.txt' files into single 'BSS-ANOVA_eigenvalues_for_20x20_thru_2000x2000.txt'" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "metadata": { |
| 107 | + "kernelspec": { |
| 108 | + "display_name": "Python 3", |
| 109 | + "language": "python", |
| 110 | + "name": "python3" |
| 111 | + }, |
| 112 | + "language_info": { |
| 113 | + "codemirror_mode": { |
| 114 | + "name": "ipython", |
| 115 | + "version": 3 |
| 116 | + }, |
| 117 | + "file_extension": ".py", |
| 118 | + "mimetype": "text/x-python", |
| 119 | + "name": "python", |
| 120 | + "nbconvert_exporter": "python", |
| 121 | + "pygments_lexer": "ipython3", |
| 122 | + "version": "3.10.12" |
| 123 | + } |
| 124 | + }, |
| 125 | + "nbformat": 4, |
| 126 | + "nbformat_minor": 2 |
| 127 | +} |
0 commit comments