Created
February 18, 2017 19:20
-
-
Save nelsonag/a76824eae14b749f455111697f3f6dc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from sympy import *\n", | |
"N, kc, kt, sc2, st2, sct2 = symbols('N kc kt sc2 st2 sct2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Set the keff estimators and population covariance matrices\n", | |
"k = Matrix([[kc], [kt]])\n", | |
"Sigma = Matrix([[sc2, sct2], [sct2, st2]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Set the transformations\n", | |
"A = Matrix([[1, 0], [1, -1]])\n", | |
"z = A * k\n", | |
"d = Matrix(z[1:])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"Sigmaz = A * Sigma * A.T" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"Sz22 = (N - 1) * Sigmaz[1, 1]\n", | |
"Sz12 = (N - 1) * Sigmaz[0, 1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"kc - (kc - kt)*(sc2 - sct2)/(sc2 - 2*sct2 + st2)\n" | |
] | |
} | |
], | |
"source": [ | |
"# Get khat\n", | |
"khat = kc - Sz12 / Sz22 * d[0]\n", | |
"print(khat)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Matrix([[(sc2*st2 - sct2**2)/(sc2 - 2*sct2 + st2)]])\n", | |
"sc2 - (sc2 - sct2)**2/(sc2 - 2*sct2 + st2)\n" | |
] | |
} | |
], | |
"source": [ | |
"# Get the combined estimator first with the asymptotic variance of khat\n", | |
"e = Matrix([[1], [1]])\n", | |
"eT = e.T\n", | |
"sopt2 = (eT * Sigma**-1 * e)\n", | |
"sopt2 = sopt2**-1\n", | |
"print(sopt2)\n", | |
"a = Sigmaz[0,0] - Sigmaz[0,1] * Sigmaz[1,0] / Sigmaz[1,1]\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(N*(kc - kt)**2 + (N - 1)*(sc2 - 2*sct2 + st2))*(sc2*st2 - sct2**2)/(N*(N - 1)*(sc2 - 2*sct2 + st2)**2)\n" | |
] | |
} | |
], | |
"source": [ | |
"# Now use this asymptotic variance to get khat\n", | |
"sk2 = sopt2 * (1 / N + d[0] * Sz22**-1 * d[0])\n", | |
"print(sk2[0,0].simplify())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"# Compare to my hand calculations\n", | |
"g = sc2 + st2 - 2 * sct2\n", | |
"hand_calcs = (N*(kc - kt)**2 + (N - 1)*g)*(sc2*st2 - sct2**2)/(N*(N - 1)*(g)**2)\n", | |
"print(sk2[0,0].simplify() / hand_calcs)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Good! we're all done!" | |
] | |
} | |
], | |
"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.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment