Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First implementation of KSB #47

Merged
merged 13 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ $ conda env create -f environment.yml
$ conda activate shear
```

## Install the `shrbk` library
## Install the `shrbk` library

`shrbk` is the library made for this book.
`shrbk` is the library made for this book.
Install it ither in development mode

```bash
Expand Down
219 changes: 219 additions & 0 deletions notebooks/taylor_series.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Taylor Series for Approximations\n",
"\n",
"Taylor series are commonly used in physics to approximate functions making them easier to handle specially when solving equations. In this notebook we give a visual example on how it works and the biases that it introduces.\n",
"\n",
"## Theoretical Formula\n",
"\n",
"Consider a function $f$ that is $n$ times differentiable in a point $a$. Then by Taylor's theorem, for any point $x$ in the domain of f, we have the Taylor expansion about the point $a$ is defined as:\n",
"\\begin{equation}\n",
"f(x) = f(a) + \\sum_{k=1}^n \\frac{f^{k}(a)}{k!}(x-a)^k + o\\left((x-a)^n\\right) \\quad,\n",
"\\end{equation}\n",
"where $f^{(k)}$ is the derivative of order $k$ of $f$. Usually, we consider $a=0$ which gives:\n",
"\\begin{equation}\n",
"f(x) = f(0) + \\sum_{k=1}^n \\frac{f^{k}(0)}{k!}(x)^k + o\\left((x)^n\\right) \\quad.\n",
"\\end{equation}\n",
"\n",
"For example, the exponential, $e$ is infinitely differentiable with $e^{(k)}=e$ and $e^0=1$. This gives us the following Taylor expansion:\n",
"\\begin{equation}\n",
"e(x) = 1 + \\sum_{k=1}^\\infty \\frac{x^k}{k!} \\quad.\n",
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualising Taylor Expansion Approximation and its Bias\n",
"\n",
"Let us see visually how the Taylor expansion approximatees a given function. We start by defining our function below, for example we will consider the exponential function, $e$ again up to order 3."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"code_folding": [
0
]
},
"outputs": [],
"source": [
"#### FOLDED CELL\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"from IPython.display import Markdown as md\n",
"from sympy import Symbol, series, lambdify, latex\n",
"from sympy.functions import *\n",
"from ipywidgets import interactive_output\n",
"import ipywidgets as widgets\n",
"from sympy.parsing.sympy_parser import parse_expr\n",
"import numpy as np\n",
"\n",
"x = Symbol('x')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"order = 3\n",
"func = exp(x)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"code_folding": [
0
]
},
"outputs": [],
"source": [
"#### FOLDED CELL\n",
"taylor_exp = series(func,x,n=order+1)\n",
"approx = lambdify(x, sum(taylor_exp.args[:-1]), \"numpy\")\n",
"func_np = lambdify(x, func, \"numpy\")\n",
"latex_func = '$'+latex(func)+'$'\n",
"latex_taylor = '\\\\begin{equation} '+latex(taylor_exp)+' \\end{equation}'"
]
},
{
"cell_type": "markdown",
"metadata": {
"variables": {
" latex_func ": "$e^{x}$",
"latex_taylor": "\\begin{equation} 1 + x + \\frac{x^{2}}{2} + \\frac{x^{3}}{6} + O\\left(x^{4}\\right) \\end{equation}"
}
},
"source": [
"The Taylor expansion of {{ latex_func }} is :\n",
"{{latex_taylor}}\n",
"\n",
"Now let's plot the function and its expansion while considering a point, noted $p$, to study the biais that we introduce when we approximate the function by its expansion:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"code_folding": [
0
]
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5f5d027e2c284af4869d9e9c6ea083f9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output(layout=Layout(height='650px'))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d8b970e851ef4e688880bc1db4a46238",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatSlider(value=3.0, description='Point Absciss', max=4.0, min=-4.0, step=0.2), IntSlider(val…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#### FOLDED CELL\n",
"order = widgets.IntSlider(min=0,max=20,step=1,value=3,description='Order')\n",
"x_min = -4\n",
"x_max = 4\n",
"x1 = widgets.FloatSlider(min=x_min,max=x_max,value=3,step=0.2,description='Point Absciss')\n",
"func = widgets.Text('exp(x)',description='Function')\n",
"text_offset = np.array([-0.15,2.])\n",
"\n",
"\n",
"ui = widgets.HBox([x1, order, func])\n",
"\n",
"def f(order=widgets.IntSlider(min=1,max=10,step=1,value=3)\n",
" ,x1=1.5\n",
" ,func='exp(x)'):\n",
" func_sp = parse_expr(func)\n",
" taylor_exp = series(func_sp,x,n=order+1)\n",
" approx = lambdify(x, sum(taylor_exp.args[:-1]), \"numpy\")\n",
" func_np = lambdify(x, func_sp, \"numpy\")\n",
" n_points = 1000\n",
" x_array = np.linspace(x_min,x_max,n_points)\n",
" approx_array = np.array([approx(z) for z in x_array])\n",
" func_array = np.array([func_np(z) for z in x_array])\n",
" func_x1 = func_np(x1)\n",
" approx_x1 = approx(x1)\n",
" plt.figure(42,figsize=(10,10))\n",
" plt.plot(x_array,approx_array,color='blue',label='Taylor Expansion')\n",
" plt.plot(x_array,func_array,color='green',label=func)\n",
" plt.plot(0,approx(0),color='black',marker='o')\n",
" plt.annotate(r'(0,0)',[0,approx(0)],xytext=text_offset)\n",
" plt.plot([x1,x1]\n",
" ,[-np.max(np.abs([np.min(func_array),np.max(func_array)])),min(approx_x1, func_x1)]\n",
" ,'--',color='black',marker='x')\n",
" plt.plot([x1,x1],[approx_x1, func_x1],'r--',marker='x')\n",
" plt.annotate(r'$p_{approx}$',[x1,approx(x1)],xytext=[x1,approx(x1)]-text_offset)\n",
" plt.annotate(r'$p$',[x1,func_np(x1)],xytext=[x1,func_np(x1)]-text_offset)\n",
" plt.xlim([x_min,x_max])\n",
" plt.ylim(-np.max(np.abs([np.min(func_array),np.max(func_array)]))\n",
" ,np.max(np.abs([np.min(func_array),np.max(func_array)])))\n",
" plt.legend()\n",
" plt.show()\n",
" print('Approximation bias : {}'.format(func_x1-approx_x1))\n",
" return None\n",
"interactive_plot = widgets.interactive_output(f, {'order': order, 'x1': x1, 'func': func})\n",
"interactive_plot.layout.height = '650px'\n",
"display(interactive_plot, ui)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the further $p$ gets away from the point of the expansion (in that case $0$), the higher the approximation bias gets. Samely, the lower the order of approximation is, the higher the approximation bias gets."
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
35 changes: 35 additions & 0 deletions shearbook/_bibliography/z_moments_methods.bib
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,38 @@ @ARTICLE{2011MNRAS.410.2156V
adsurl = {https://ui.adsabs.harvard.edu/abs/2011MNRAS.410.2156V},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}

@ARTICLE{1995ApJ.KSB,
author = {{Kaiser}, Nick and {Squires}, Gordon and {Broadhurst}, Tom},
title = "{A Method for Weak Lensing Observations}",
journal = {\apj},
keywords = {COSMOLOGY: OBSERVATIONS, COSMOLOGY: DARK MATTER, GALAXIES: FORMATION, COSMOLOGY: GRAVITATIONAL LENSING, COSMOLOGY: LARGE-SCALE STRUCTURE OF UNIVERSE, Astrophysics},
year = 1995,
month = aug,
volume = {449},
pages = {460},
doi = {10.1086/176071},
archivePrefix = {arXiv},
eprint = {astro-ph/9411005},
primaryClass = {astro-ph},
adsurl = {https://ui.adsabs.harvard.edu/abs/1995ApJ...449..460K},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}

@ARTICLE{2003MNRAS.343..459H,
author = {{Hirata}, Christopher and {Seljak}, Uro{\v{s}}},
title = "{Shear calibration biases in weak-lensing surveys}",
journal = {\mnras},
keywords = {gravitational lensing, methods: data analysis, Astrophysics},
year = 2003,
month = aug,
volume = {343},
number = {2},
pages = {459-480},
doi = {10.1046/j.1365-8711.2003.06683.x},
archivePrefix = {arXiv},
eprint = {astro-ph/0301054},
primaryClass = {astro-ph},
adsurl = {https://ui.adsabs.harvard.edu/abs/2003MNRAS.343..459H},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}
7 changes: 5 additions & 2 deletions shearbook/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
chapters:
- file: intro/about
- file: intro/run_code
- file: intro/weak_lensing

- part: Shape Measurement
chapters:
Expand All @@ -17,8 +18,6 @@
- file: methods/moments-methods
sections:
- file: methods/ksb
- file: methods/ksb-variants
- file: methods/viola2011
- file: methods/moments-methods-ref
- file: model/model-intro
sections:
Expand All @@ -35,6 +34,10 @@
sections:
- file: calibration/calibration-ref

- part: Appendix
chapters:
- file: appendix/taylor_series

- part: Contributing
chapters:
- file: contrib/contributors
Expand Down
Binary file added shearbook/appendix/.DS_Store
Binary file not shown.
Loading