diff --git a/Code/TransitionDerivatives.ipynb b/Code/TransitionDerivatives.ipynb index f260869..cd19f56 100644 --- a/Code/TransitionDerivatives.ipynb +++ b/Code/TransitionDerivatives.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "recorded-albany", + "id": "innovative-filename", "metadata": {}, "outputs": [], "source": [ @@ -14,7 +14,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "senior-characterization", + "id": "filled-question", "metadata": {}, "outputs": [ { @@ -40,7 +40,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "sublime-trance", + "id": "middle-lease", "metadata": {}, "outputs": [], "source": [ @@ -50,7 +50,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "double-climb", + "id": "regular-grounds", "metadata": { "tags": [] }, @@ -62,7 +62,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "blind-reunion", + "id": "satisfied-hawaiian", "metadata": {}, "outputs": [], "source": [ @@ -75,14 +75,14 @@ { "cell_type": "code", "execution_count": 6, - "id": "worldwide-winning", + "id": "saved-corporation", "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", + " #TODO: ACTUALLY DERIVE A SURVIVAL FUNCTION. THIS IS JUST A PLACEHOLDER. PROBABLY SHOULD BE AN EXPONENTIAL DISTRIBUTION\n", " eta = 1.0/(scaling@stock)\n", " b = 1/debris\n", " \n", @@ -101,7 +101,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "naughty-transport", + "id": "nominated-visitor", "metadata": {}, "outputs": [ { @@ -122,7 +122,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "large-trigger", + "id": "clean-panama", "metadata": {}, "outputs": [ { @@ -153,7 +153,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "prime-projector", + "id": "cardiovascular-music", "metadata": {}, "outputs": [ { @@ -179,7 +179,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "urban-decision", + "id": "funky-illness", "metadata": {}, "outputs": [ { @@ -202,7 +202,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "congressional-kelly", + "id": "charged-dairy", "metadata": {}, "outputs": [ { @@ -240,7 +240,7 @@ }, { "cell_type": "markdown", - "id": "identified-insertion", + "id": "marked-flower", "metadata": {}, "source": [ "## Next step: Construct the intertemporal-transition function(s)\n", @@ -252,10 +252,317 @@ "I am planning on doing the latter, as each version is needed." ] }, + { + "cell_type": "code", + "execution_count": 12, + "id": "outdoor-action", + "metadata": {}, + "outputs": [], + "source": [ + "#setup\n", + "beta = torch.tensor([0.95])\n", + "util_weights = torch.tensor([1.0,1.0,0,0,0])\n", + "sigma = 0.5" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "unavailable-hawaii", + "metadata": {}, + "outputs": [], + "source": [ + "#This is not a good specification of the profit function, but it will work for now.\n", + "def util(x):\n", + " return (util_weights@x)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "composite-cooperative", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor(2., grad_fn=)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "util(stocks)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "activated-advancement", + "metadata": {}, + "outputs": [], + "source": [ + "w = torch.zeros(5)\n", + "w[0]=1" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "wireless-concord", + "metadata": {}, + "outputs": [], + "source": [ + "def single_transition(w, g, util, stocks, debris, launch, beta):\n", + " #TODO: change launch from a direct tensor, to a function.\n", + " bA = beta * jacobian(g, (stocks,debris,launch))[0][0]\n", + " return bA.inverse() @ (w - jacobian(util,stocks))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "detected-cooking", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 0.0372, -1.2487, 0.0372, 0.0372, 0.0164])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "single_transition(w,g,util,stocks,debris,launch,beta)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "sharp-pound", + "metadata": {}, + "outputs": [], + "source": [ + "#need to create recursive transitions\n", + "def recurse_transitions(w, g, util, stocks, debris, launch, beta, iterations):\n", + " #This is of type two from the discussion above\n", + " if iterations <= 1:\n", + " return single_transition(w, g, util, stocks, debris, launch, beta)\n", + " else: \n", + " #Get recursive results\n", + " curse = recurse_transitions(w,g,util,stocks,debris,launch,beta,iterations-1)\n", + " \n", + " #Get updated stocks and debris\n", + " stocks_iterated,debris_iterated = g(stocks, debris, launch) \n", + " \n", + " #Return the updated values\n", + " return single_transition(curse, g, util, stocks_iterated, debris_iterated, launch, beta)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "radical-reason", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 0.0372, -1.2487, 0.0372, 0.0372, 0.0164])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "recurse_transitions(w, g, util, stocks, debris, launch, beta, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "intelligent-angle", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([-0.4740, -1.0278, -0.0434, -0.0434, -0.0575])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "recurse_transitions(w, g, util, stocks, debris, launch, beta, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "jewish-lemon", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([-0.7046, -0.9430, -0.0884, -0.0884, -0.1102])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "recurse_transitions(w, g, util, stocks, debris, launch, beta, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "scenic-literature", + "metadata": {}, + "outputs": [], + "source": [ + "#TODO: manually check single_transition\n", + "stocks_1,debris_1 = g(stocks, debris, launch) \n", + "stocks_2,debris_2 = g(stocks_1, debris_1, launch)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "moving-slave", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 0.0372, -1.2487, 0.0372, 0.0372, 0.0164])" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Recurse 1\n", + "sin1 = single_transition(w,g,util,stocks,debris,launch,beta)\n", + "sin1" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "corrected-radar", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([-0.4740, -1.0278, -0.0434, -0.0434, -0.0575])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Recurse 2\n", + "sin2 = single_transition(sin1 \n", + " ,g\n", + " ,util\n", + " ,stocks_1\n", + " ,debris_1\n", + " ,launch,beta)\n", + "sin2" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "handy-pencil", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([-1.5516, -2.1345, -0.0456, -0.0456, -0.1302])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#recurse 3\n", + "single_transition(sin2,g,util,stocks_2,debris_2,launch,beta)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "attempted-affairs", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([-1.5516, -2.1345, -0.0456, -0.0456, -0.1302])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "recurse_transitions(sin2, g, util, stocks_2, debris_2, launch, beta, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "honest-diana", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([-0.7046, -0.9430, -0.0884, -0.0884, -0.1102])" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#I think this highlights the error.\n", + "recurse_transitions(recurse_transitions(sin1, g, util, stocks_1, debris_1, launch, beta, 1), g, util, stocks_1, debris_1, launch, beta, 1)" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "utility-browse", + "id": "animated-nudist", "metadata": {}, "outputs": [], "source": [] diff --git a/Code/Untitled.ipynb b/Code/Untitled.ipynb new file mode 100644 index 0000000..2402f0a --- /dev/null +++ b/Code/Untitled.ipynb @@ -0,0 +1,80 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "complicated-passage", + "metadata": {}, + "source": [ + "testing a recursive function builder" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "alone-individual", + "metadata": {}, + "outputs": [], + "source": [ + "def base(x,depth):\n", + " return \"y\"+base(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "sustainable-closing", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'yyyx'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "base(\"x\",3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "systematic-actor", + "metadata": {}, + "outputs": [], + "source": [ + "def all_your(fn,depth1):\n", + " if depth1 <= 0:\n", + " return fn\n", + " else:\n", + " ifn = all_your(fn,depth-1)\n", + " return fn(ifn)" + ] + } + ], + "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 +}