cleaned up files, got a working transition function

temporaryWork
youainti 5 years ago
parent 823880e07a
commit c9e6637157

@ -2,8 +2,8 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 1,
"id": "meaningful-piece", "id": "optional-means",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -15,7 +15,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"id": "prescribed-roller", "id": "running-space",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -28,7 +28,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"id": "amateur-taste", "id": "electoral-apache",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -39,7 +39,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "accepted-bonus", "id": "vertical-modem",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []
@ -47,7 +47,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"id": "burning-peeing", "id": "caring-negative",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -57,7 +57,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 5,
"id": "special-hydrogen", "id": "constitutional-coaching",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -67,7 +67,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 6,
"id": "played-fairy", "id": "uniform-implementation",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -80,7 +80,7 @@
"x = tensor([4.], requires_grad=True)\n", "x = tensor([4.], requires_grad=True)\n",
"y = tensor([29.], grad_fn=<AddBackward0>)\n", "y = tensor([29.], grad_fn=<AddBackward0>)\n",
"\n", "\n",
"df = <AddBackward0 object at 0x7fd45460eb20>\n", "df = <AddBackward0 object at 0x7ffa3aa74d30>\n",
"\n", "\n",
"gradient of func(x) = \n", "gradient of func(x) = \n",
"tensor([11.])\n" "tensor([11.])\n"
@ -122,7 +122,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "scientific-egypt", "id": "major-server",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Try this\n", "# Try this\n",
@ -164,7 +164,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 7,
"id": "flexible-hotel", "id": "authentic-calculator",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -175,7 +175,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 8,
"id": "floating-alert", "id": "expired-judgment",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -199,7 +199,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 9,
"id": "historical-elevation", "id": "similar-joshua",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -223,7 +223,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 10,
"id": "proud-brake", "id": "centered-geneva",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -245,7 +245,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 11,
"id": "protective-grave", "id": "lightweight-operator",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -268,7 +268,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 12,
"id": "coated-guard", "id": "assumed-mustang",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -293,7 +293,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 13,
"id": "competitive-acting", "id": "noble-acoustic",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -304,7 +304,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 14,
"id": "statutory-starter", "id": "choice-checkout",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -324,8 +324,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 15,
"id": "engaged-teddy", "id": "imported-prefix",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -341,7 +341,7 @@
" [ 56., 204.]], grad_fn=<ViewBackward>))" " [ 56., 204.]], grad_fn=<ViewBackward>))"
] ]
}, },
"execution_count": 16, "execution_count": 15,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -352,7 +352,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "stretch-mercury", "id": "documentary-charlotte",
"metadata": {}, "metadata": {},
"source": [ "source": [
"So, I think I can construct a gradient, and possibly invert it/choose some other solution method." "So, I think I can construct a gradient, and possibly invert it/choose some other solution method."
@ -360,7 +360,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "disciplinary-opposition", "id": "civic-conversion",
"metadata": {}, "metadata": {},
"source": [ "source": [
"basic transition functions\n", "basic transition functions\n",
@ -372,8 +372,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 54, "execution_count": 16,
"id": "missing-robinson", "id": "casual-heater",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -392,8 +392,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 51, "execution_count": 17,
"id": "driving-wright", "id": "expired-intro",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -404,7 +404,7 @@
" tensor([2., 3.], requires_grad=True))" " tensor([2., 3.], requires_grad=True))"
] ]
}, },
"execution_count": 51, "execution_count": 17,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -416,34 +416,81 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 52, "execution_count": 23,
"id": "infectious-plaintiff", "id": "negative-balance",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"(tensor([[-0.1172],\n", "(tensor([[-0.1172],\n",
" [-0.0781]]),\n", " [-0.0781]], grad_fn=<ViewBackward>),\n",
" tensor([[1.0031, 0.0041],\n", " tensor([[1.0031, 0.0041],\n",
" [0.0027, 1.0017]]),\n", " [0.0027, 1.0017]], grad_fn=<ViewBackward>),\n",
" tensor([[1., 0.],\n", " tensor([[1., 0.],\n",
" [0., 1.]]))" " [0., 1.]]))"
] ]
}, },
"execution_count": 52, "execution_count": 23,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "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", "cell_type": "code",
"execution_count": 53, "execution_count": 33,
"id": "proud-dance", "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": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -457,7 +504,7 @@
" [0.0000, 0.0010]]))" " [0.0000, 0.0010]]))"
] ]
}, },
"execution_count": 53, "execution_count": 19,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -469,7 +516,15 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "satisfied-briefs", "id": "normal-seating",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "unavailable-burke",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []

@ -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=<RsubBackward1>)"
]
},
"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=<AddBackward0>),\n",
" tensor([0.5134], grad_fn=<AddBackward0>))"
]
},
"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
}

@ -3,7 +3,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"id": "4104cbdc-87e8-4be6-b2aa-064a5c0d1bde", "id": "inappropriate-technician",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -15,7 +15,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"id": "bf266b6c-e071-45e4-8001-69fe4a70517a", "id": "rubber-accreditation",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -26,7 +26,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 10,
"id": "ad508a39-9690-45e0-af0b-c2c9f6819ad3", "id": "adverse-efficiency",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -37,7 +37,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 35, "execution_count": 35,
"id": "baabba71-6bcd-46cb-8d31-336087eb6e9f", "id": "biblical-finance",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -48,7 +48,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 36, "execution_count": 36,
"id": "3f098bbe-6ed9-48e7-ae9a-a88992a72541", "id": "native-congress",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -70,7 +70,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 37,
"id": "f6dd665f-2965-4e7f-9d3b-2c1ce3ff522e", "id": "residential-sight",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -100,7 +100,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "8ca4b6a4-1a13-4ad4-8856-a38ac69d96a6", "id": "still-province",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []
@ -122,7 +122,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.5" "version": "3.8.8"
} }
}, },
"nbformat": 4, "nbformat": 4,

Loading…
Cancel
Save