{ "cells": [ { "cell_type": "markdown", "id": "f569a665-f294-4842-9ff6-d602efc63e40", "metadata": {}, "source": [ "# Log Gaussian 2D" ] }, { "cell_type": "markdown", "id": "6b9201df-6507-46df-96e2-a6580f9bf43b", "metadata": {}, "source": [ "`lamatrix` provides a log Gaussian to fit to data in 2D. You should read through the `lnGaussian` example before reading this one to see some of the caveats. This model expands to two dimensions, but has the same caveats. Keep in mind for now this does not implement the $\\rho$ correlation term in this 2D Gaussian. Below is a derivation of the 2D case of a log Gaussian.\n", "\n", "$$\n", "G(x, y) = A \\frac{1}{2 \\pi \\sigma_x \\sigma_y} \\exp ^{\\left( -\\frac{(x - \\mu_x)^2}{2\\sigma_x^2} - \\frac{(y - \\mu_y)^2}{2\\sigma_y^2} \\right)}\n", "$$\n", "\n", "$$\n", "\\ln G(x, y) = \\ln(A) -\\ln(2 \\pi \\sigma_x \\sigma_y) - \\frac{x^2}{2\\sigma_x^2} + \\frac{x\\mu_x}{\\sigma_x^2} - \\frac{\\mu_x^2}{2\\sigma_x^2} - \\frac{y^2}{2\\sigma_y^2} + \\frac{y\\mu_y}{\\sigma_y^2} - \\frac{\\mu_y^2}{2\\sigma_y^2}\n", "$$\n", "\n", "\n", "$$\n", "a_x = - \\frac{1}{2\\sigma_x^2}\n", "$$\n", "$$\n", "b_x = \\frac{\\mu_x}{\\sigma_x^2}\n", "$$\n", "\n", "$$\n", "a_y = - \\frac{1}{2\\sigma_y^2}\n", "$$\n", "$$\n", "b_y = \\frac{\\mu_y}{\\sigma_y^2}\n", "$$\n", "$$\n", "c = \\ln(A) - \\ln(2\\pi\\sigma_x\\sigma_y) - \\frac{\\mu_x^2}{2\\sigma_x^2} - \\frac{\\mu_y^2}{2\\sigma_y^2}\n", "$$\n", "\n", "$$\n", "\\ln(G(x)) = a_x x^2 + b_x x + a_y y^2 + b_y y + c\n", "$$\n" ] }, { "cell_type": "markdown", "id": "bfba5521-cf4f-4b81-9733-945b3e0e2250", "metadata": {}, "source": [ "Let's look at the equation for this model" ] }, { "cell_type": "code", "execution_count": 1, "id": "47e00fe5-dd14-4dbb-ab90-4df8c172f663", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "X, Y = np.mgrid[-10:10:200j,-9:9:180j]\n", "\n", "\n", "from lamatrix import lnGaussian2D" ] }, { "cell_type": "code", "execution_count": 2, "id": "8e91b1f7-8ca2-46d5-8690-da889d0e683f", "metadata": {}, "outputs": [], "source": [ "model = lnGaussian2D()" ] }, { "cell_type": "code", "execution_count": 3, "id": "4502adb7-bf7c-4ce8-a299-4e2d0f61f843", "metadata": {}, "outputs": [ { "data": { "text/html": [ "