diff --git a/Code/ThoughtsOnUsingPytorch.ipynb b/Code/ThoughtsOnUsingPytorch.ipynb index a85c5ee..d7d7445 100644 --- a/Code/ThoughtsOnUsingPytorch.ipynb +++ b/Code/ThoughtsOnUsingPytorch.ipynb @@ -2,8 +2,8 @@ "cells": [ { "cell_type": "code", - "execution_count": 25, - "id": "meaningful-piece", + "execution_count": 1, + "id": "optional-means", "metadata": {}, "outputs": [], "source": [ @@ -15,7 +15,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "prescribed-roller", + "id": "running-space", "metadata": {}, "outputs": [], "source": [ @@ -28,7 +28,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "amateur-taste", + "id": "electoral-apache", "metadata": {}, "outputs": [], "source": [ @@ -39,7 +39,7 @@ { "cell_type": "code", "execution_count": null, - "id": "accepted-bonus", + "id": "vertical-modem", "metadata": {}, "outputs": [], "source": [] @@ -47,7 +47,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "burning-peeing", + "id": "caring-negative", "metadata": {}, "outputs": [], "source": [ @@ -57,7 +57,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "special-hydrogen", + "id": "constitutional-coaching", "metadata": {}, "outputs": [], "source": [ @@ -67,7 +67,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "played-fairy", + "id": "uniform-implementation", "metadata": {}, "outputs": [ { @@ -80,7 +80,7 @@ "x = tensor([4.], requires_grad=True)\n", "y = tensor([29.], grad_fn=)\n", "\n", - "df = \n", + "df = \n", "\n", "gradient of func(x) = \n", "tensor([11.])\n" @@ -122,7 +122,7 @@ }, { "cell_type": "markdown", - "id": "scientific-egypt", + "id": "major-server", "metadata": {}, "source": [ "# Try this\n", @@ -164,7 +164,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "flexible-hotel", + "id": "authentic-calculator", "metadata": {}, "outputs": [], "source": [ @@ -175,7 +175,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "floating-alert", + "id": "expired-judgment", "metadata": {}, "outputs": [ { @@ -199,7 +199,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "historical-elevation", + "id": "similar-joshua", "metadata": {}, "outputs": [ { @@ -223,7 +223,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "proud-brake", + "id": "centered-geneva", "metadata": {}, "outputs": [ { @@ -245,7 +245,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "protective-grave", + "id": "lightweight-operator", "metadata": {}, "outputs": [ { @@ -268,7 +268,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "coated-guard", + "id": "assumed-mustang", "metadata": {}, "outputs": [ { @@ -293,7 +293,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "competitive-acting", + "id": "noble-acoustic", "metadata": {}, "outputs": [], "source": [ @@ -304,7 +304,7 @@ { "cell_type": "code", "execution_count": 14, - "id": "statutory-starter", + "id": "choice-checkout", "metadata": {}, "outputs": [ { @@ -324,8 +324,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "id": "engaged-teddy", + "execution_count": 15, + "id": "imported-prefix", "metadata": {}, "outputs": [ { @@ -341,7 +341,7 @@ " [ 56., 204.]], grad_fn=))" ] }, - "execution_count": 16, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -352,7 +352,7 @@ }, { "cell_type": "markdown", - "id": "stretch-mercury", + "id": "documentary-charlotte", "metadata": {}, "source": [ "So, I think I can construct a gradient, and possibly invert it/choose some other solution method." @@ -360,7 +360,7 @@ }, { "cell_type": "markdown", - "id": "disciplinary-opposition", + "id": "civic-conversion", "metadata": {}, "source": [ "basic transition functions\n", @@ -372,8 +372,8 @@ }, { "cell_type": "code", - "execution_count": 54, - "id": "missing-robinson", + "execution_count": 16, + "id": "casual-heater", "metadata": {}, "outputs": [], "source": [ @@ -392,8 +392,8 @@ }, { "cell_type": "code", - "execution_count": 51, - "id": "driving-wright", + "execution_count": 17, + "id": "expired-intro", "metadata": {}, "outputs": [ { @@ -404,7 +404,7 @@ " tensor([2., 3.], requires_grad=True))" ] }, - "execution_count": 51, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -416,34 +416,81 @@ }, { "cell_type": "code", - "execution_count": 52, - "id": "infectious-plaintiff", + "execution_count": 23, + "id": "negative-balance", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(tensor([[-0.1172],\n", - " [-0.0781]]),\n", + " [-0.0781]], grad_fn=),\n", " tensor([[1.0031, 0.0041],\n", - " [0.0027, 1.0017]]),\n", + " [0.0027, 1.0017]], grad_fn=),\n", " tensor([[1., 0.],\n", " [0., 1.]]))" ] }, - "execution_count": 52, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "jacobian(gs,(D,b,a))" + "x = jacobian(gs,(D,b,a),create_graph=True)\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "empirical-control", + "metadata": {}, + "outputs": [], + "source": [ + "def X(D,b,a):\n", + " return jacobian(gs,(D,b,a),create_graph=True)" ] }, { "cell_type": "code", - "execution_count": 53, - "id": "proud-dance", + "execution_count": 33, + "id": "toxic-riverside", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[[-1.2447e+09, -0.0000e+00],\n", + " [-1.1439e+09, -0.0000e+00]],\n", + " \n", + " [[-0.0000e+00, -1.5585e+07],\n", + " [-0.0000e+00, -1.8206e+07]]]),\n", + " tensor([[[-1.3814e+09, -1.2731e+09],\n", + " [-1.2731e+09, -1.1647e+09]],\n", + " \n", + " [[-8.0607e+06, -9.5143e+06],\n", + " [-9.5143e+06, -1.0968e+07]]]),\n", + " tensor([[[0., 0.],\n", + " [0., 0.]],\n", + " \n", + " [[0., 0.],\n", + " [0., 0.]]]))" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "jacobian(X,(d,b,a))[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "personal-asbestos", "metadata": {}, "outputs": [ { @@ -457,7 +504,7 @@ " [0.0000, 0.0010]]))" ] }, - "execution_count": 53, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -469,7 +516,15 @@ { "cell_type": "code", "execution_count": null, - "id": "satisfied-briefs", + "id": "normal-seating", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "unavailable-burke", "metadata": {}, "outputs": [], "source": [] diff --git a/Code/TransitionDerivatives.ipynb b/Code/TransitionDerivatives.ipynb new file mode 100644 index 0000000..f260869 --- /dev/null +++ b/Code/TransitionDerivatives.ipynb @@ -0,0 +1,285 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "recorded-albany", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from torch.autograd.functional import jacobian" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "senior-characterization", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([1.0000, 1.0000, 1.0000, 1.0000, 0.5000], requires_grad=True)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#set states\n", + "stocks = torch.ones(5)\n", + "#Last one is different\n", + "stocks[-1] = 0.5\n", + "#now add the tracking requirement in place\n", + "stocks.requires_grad_()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "sublime-trance", + "metadata": {}, + "outputs": [], + "source": [ + "launch = torch.ones(5, requires_grad=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "double-climb", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "debris = torch.tensor([2.2],requires_grad=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "blind-reunion", + "metadata": {}, + "outputs": [], + "source": [ + "scaling = torch.ones(5)\n", + "delta = 0.9 \n", + "launch_debris_rate = 0.05\n", + "collision_debris_rate = 0.07" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "worldwide-winning", + "metadata": {}, + "outputs": [], + "source": [ + "def survival(stock, debris):\n", + " #Gompertz distribution for simplicity\n", + " #commonly used with saturation\n", + " #TODO: ACTUALLY DERIVE A SURVIVAL FUNCTION. THIS IS JUST A PLACEHOLDER\n", + " eta = 1.0/(scaling@stock)\n", + " b = 1/debris\n", + " \n", + " return 1 - ( b*eta*torch.exp(eta+b*stock-eta*torch.exp(b*stock)))\n", + "\n", + "\n", + "def g(stock, debris, launches):\n", + " new_stock = stock*survival(stock,debris) + launches\n", + " \n", + " #Ignoring autocatalysis\n", + " new_debris = (1-delta)*debris + launch_debris_rate * launches.sum() + collision_debris_rate*(1-survival(stock,debris)) @ stock\n", + " \n", + " return (new_stock, new_debris)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "naughty-transport", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([0.8600, 0.8600, 0.8600, 0.8600, 0.8802], grad_fn=)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "survival(stocks,debris)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "large-trigger", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[-0.0142, 0.0271, 0.0271, 0.0271, 0.0271],\n", + " [ 0.0271, -0.0142, 0.0271, 0.0271, 0.0271],\n", + " [ 0.0271, 0.0271, -0.0142, 0.0271, 0.0271],\n", + " [ 0.0271, 0.0271, 0.0271, -0.0142, 0.0271],\n", + " [ 0.0251, 0.0251, 0.0251, 0.0251, -0.0142]]),\n", + " tensor([[0.0825],\n", + " [0.0825],\n", + " [0.0825],\n", + " [0.0825],\n", + " [0.0634]]))" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Get the derivatives seperately\n", + "jacobian(survival, (stocks,debris))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "prime-projector", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[-0.0142, 0.0271, 0.0271, 0.0271, 0.0271, 0.0825],\n", + " [ 0.0271, -0.0142, 0.0271, 0.0271, 0.0271, 0.0825],\n", + " [ 0.0271, 0.0271, -0.0142, 0.0271, 0.0271, 0.0825],\n", + " [ 0.0271, 0.0271, 0.0271, -0.0142, 0.0271, 0.0825],\n", + " [ 0.0251, 0.0251, 0.0251, 0.0251, -0.0142, 0.0634]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Get the derivatives as a single result\n", + "torch.cat(jacobian(survival, (stocks,debris)), axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "urban-decision", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([1.8600, 1.8600, 1.8600, 1.8600, 1.4401], grad_fn=),\n", + " tensor([0.5134], grad_fn=))" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Testing state updates\n", + "g(stocks, debris, launch)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "congressional-kelly", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((tensor([[0.8457, 0.0271, 0.0271, 0.0271, 0.0271],\n", + " [0.0271, 0.8457, 0.0271, 0.0271, 0.0271],\n", + " [0.0271, 0.0271, 0.8457, 0.0271, 0.0271],\n", + " [0.0271, 0.0271, 0.0271, 0.8457, 0.0271],\n", + " [0.0126, 0.0126, 0.0126, 0.0126, 0.8731]]),\n", + " tensor([[0.0825],\n", + " [0.0825],\n", + " [0.0825],\n", + " [0.0825],\n", + " [0.0317]]),\n", + " tensor([[1., 0., 0., 0., 0.],\n", + " [0., 1., 0., 0., 0.],\n", + " [0., 0., 1., 0., 0.],\n", + " [0., 0., 0., 1., 0.],\n", + " [0., 0., 0., 0., 1.]])),\n", + " (tensor([[0.0042, 0.0042, 0.0042, 0.0042, 0.0013]]),\n", + " tensor([[0.0747]]),\n", + " tensor([[0.0500, 0.0500, 0.0500, 0.0500, 0.0500]])))" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Note the two tuples of jacobians: the first is for stock evolution, the second is for debris evolution\n", + "jacobian(g, (stocks,debris,launch))" + ] + }, + { + "cell_type": "markdown", + "id": "identified-insertion", + "metadata": {}, + "source": [ + "## Next step: Construct the intertemporal-transition function(s)\n", + " - Note: There are a couple of different ways to do this\n", + " - Just a single period transition function, manually iterated\n", + " - A recursive function that creates a $p$ period iterated function\n", + " - A recursive function that returns a list of functions iterated from 1 to $p$ periods\n", + "\n", + "I am planning on doing the latter, as each version is needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "utility-browse", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Code/test_double.ipynb b/Code/test_double.ipynb index 6d867ae..c9bf59d 100644 --- a/Code/test_double.ipynb +++ b/Code/test_double.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "4104cbdc-87e8-4be6-b2aa-064a5c0d1bde", + "id": "inappropriate-technician", "metadata": {}, "outputs": [], "source": [ @@ -15,7 +15,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "bf266b6c-e071-45e4-8001-69fe4a70517a", + "id": "rubber-accreditation", "metadata": {}, "outputs": [], "source": [ @@ -26,7 +26,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "ad508a39-9690-45e0-af0b-c2c9f6819ad3", + "id": "adverse-efficiency", "metadata": {}, "outputs": [], "source": [ @@ -37,7 +37,7 @@ { "cell_type": "code", "execution_count": 35, - "id": "baabba71-6bcd-46cb-8d31-336087eb6e9f", + "id": "biblical-finance", "metadata": {}, "outputs": [], "source": [ @@ -48,7 +48,7 @@ { "cell_type": "code", "execution_count": 36, - "id": "3f098bbe-6ed9-48e7-ae9a-a88992a72541", + "id": "native-congress", "metadata": {}, "outputs": [ { @@ -70,7 +70,7 @@ { "cell_type": "code", "execution_count": 37, - "id": "f6dd665f-2965-4e7f-9d3b-2c1ce3ff522e", + "id": "residential-sight", "metadata": {}, "outputs": [ { @@ -100,7 +100,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8ca4b6a4-1a13-4ad4-8856-a38ac69d96a6", + "id": "still-province", "metadata": {}, "outputs": [], "source": [] @@ -122,7 +122,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.5" + "version": "3.8.8" } }, "nbformat": 4,