You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Orbits/Code/TransitionDerivatives.ipynb

286 lines
7.3 KiB
Plaintext

{
"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
}