Created
June 23, 2018 00:50
-
-
Save iewaij/2433d550f2952dd4fc311ba8bcb0b2dc 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Timing of Passing Variance and Standard Deviation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"using LsqFit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"500-element Array{Float64,1}:\n", | |
" -0.0118268 \n", | |
" -0.00898858 \n", | |
" 0.0350296 \n", | |
" 0.024487 \n", | |
" -0.0115987 \n", | |
" -0.0550782 \n", | |
" -0.00132984 \n", | |
" 0.11274 \n", | |
" -0.0046853 \n", | |
" -0.000245233\n", | |
" 0.123724 \n", | |
" -0.203811 \n", | |
" 0.0135445 \n", | |
" ⋮ \n", | |
" -0.245624 \n", | |
" -0.649134 \n", | |
" -2.10216 \n", | |
" 1.60312 \n", | |
" -1.78739 \n", | |
" 2.14365 \n", | |
" -4.17214 \n", | |
" 4.63409 \n", | |
" 0.268578 \n", | |
" 7.51552 \n", | |
" 2.61642 \n", | |
" -3.60903 " | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model(t, p) = p[1] * exp.(-p[2] * t)\n", | |
"tdata = linspace(0,50,500)\n", | |
"ydata = model(tdata, [1.0, 2.0]) + 0.1.*tdata.*randn(length(tdata))\n", | |
"p0 = [0.5, 0.5]\n", | |
"fit = curve_fit(model, tdata, ydata, p0)\n", | |
"r = fit.resid" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" 0.597241 seconds (386.06 k allocations: 20.982 MiB, 1.25% gc time)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"2×2 Array{Float64,2}:\n", | |
" 8.75331e-8 9.97119e-8\n", | |
" 9.97119e-8 1.19886e-7" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function pass_std(r, p0, tdata, ydata)\n", | |
" std_error = abs.(r)\n", | |
" sqrt_wt = 1./std_error\n", | |
" f(p) = sqrt_wt .* ( model(tdata, p) - ydata )\n", | |
" wt = sqrt_wt.^2\n", | |
" fit = LsqFit.lmfit(f, p0, wt)\n", | |
" estimate_covar(fit)\n", | |
"end\n", | |
"@time pass_std(r, p0, tdata, ydata)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" 1.169573 seconds (1.45 M allocations: 77.075 MiB, 1.72% gc time)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"2×2 Array{Float64,2}:\n", | |
" 3.08189e-10 3.42388e-10\n", | |
" 3.42388e-10 3.80553e-10" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# make sure you've restarted the kernel\n", | |
"function pass_var(r, p0)\n", | |
" var_error =r.^2\n", | |
" wt = 1./var_error\n", | |
" sqrt_wt = sqrt.(wt)\n", | |
" f(p) = sqrt_wt .* ( model(tdata, p) - ydata ) \n", | |
" fit = LsqFit.lmfit(f, p0, wt)\n", | |
" estimate_covar(fit)\n", | |
"end\n", | |
"@time pass_var(r, p0)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 0.6.3", | |
"language": "julia", | |
"name": "julia-0.6" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "0.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment