{ "cells": [ { "cell_type": "markdown", "id": "c70df536-cea0-4cdf-bd6a-601824a52768", "metadata": {}, "source": [ "# Custom Noise CBX\n", "\n", "This notebook showcases, how to use a custom noise function." ] }, { "cell_type": "code", "execution_count": null, "id": "33394a7d-caa6-4f01-aa88-4dd69d5c400d", "metadata": {}, "outputs": [], "source": [ "from cbx.dynamics import CBO\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "ef05a88a-d000-4ea8-af21-714df66858fa", "metadata": {}, "source": [ "## Define the custom noise function\n", "\n", "In this case we select that the noise should be zero" ] }, { "cell_type": "code", "execution_count": null, "id": "319270d9-01d4-49c0-8ac5-ab94bd6c327b", "metadata": {}, "outputs": [], "source": [ "def custom_noise(dyn):\n", " return np.zeros(dyn.drift.shape)" ] }, { "cell_type": "markdown", "id": "08dac699-4772-4409-8e25-2a398216a2f4", "metadata": {}, "source": [ "## Define a loss function and test the method" ] }, { "cell_type": "code", "execution_count": null, "id": "aae8cac4-7a04-41a2-b9ee-a0c8c26e27da", "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " return np.linalg.norm(x, axis=-1)\n", "\n", "x0 = np.random.normal(0,1,(4,7,26))\n", "dyn_cn = CBO(f, x=x0, noise=custom_noise, max_it=10, verbosity = 0)\n", "x_cn = dyn_cn.optimize()" ] }, { "cell_type": "markdown", "id": "762bcbf2-6861-4c21-8475-3e6b45e35fdb", "metadata": {}, "source": [ "Using this noise is equivalent to specifying ``sigma=0`` with standard CBO. So let's test, if this is the case:" ] }, { "cell_type": "code", "execution_count": null, "id": "f1f11124-6a10-4e54-abef-4168cbfd6dbc", "metadata": {}, "outputs": [], "source": [ "dyn = CBO(f, x=x0, sigma=0, max_it=10, verbosity = 0)\n", "x = dyn.optimize()\n", "print('L-infinty Error between the custom noise solution and standard CBO with sigma=0: ' + str(np.abs(x-x_cn).max()))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }