<commit_before><commit_msg>initial add<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook shows how anticorrelations are a reflection of conditioning on a common effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.          0.41504959  0.46025907]\n",
      " [ 0.41504959  1.          0.51578101]\n",
      " [ 0.46025907  0.51578101  1.        ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "npts=100\n",
    "gs=numpy.random.randn(npts)\n",
    "\n",
    "data=numpy.random.randn(npts,3)\n",
    "for d in range(data.shape[1]):\n",
    "    data[:,d]=data[:,d]+gs\n",
    "data=data-numpy.mean(data,0)\n",
    "\n",
    "origcor=numpy.corrcoef(data.T)\n",
    "print(origcor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "meansig=numpy.mean(data,1)\n",
    "meansig=meansig-numpy.mean(meansig)\n",
    "\n",
    "r1_gsreg_beta=numpy.linalg.lstsq(meansig[:,numpy.newaxis],data[:,0])\n",
    "r1_resid=data[:,0] - meansig*r1_gsreg_beta[0]\n",
    "\n",
    "r2_gsreg_beta=numpy.linalg.lstsq(meansig[:,numpy.newaxis],data[:,1])\n",
    "r2_resid=data[:,1] - meansig*r2_gsreg_beta[0]\n",
    "\n",
    "residcor=numpy.corrcoef(r1_resid,r2_resid)[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.text.Text at 0x7f3b3dfb9b38>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f3b3e0335c0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "plt.figure(figsize=(12,6))\n",
    "plt.subplot(1,2,1)\n",
    "plt.scatter(data[:,0],data[:,1])\n",
    "plt.title('original data: r = %0.3f'%origcor[0,1])\n",
    "plt.subplot(1,2,2)  \n",
    "plt.scatter(r1_resid,r2_resid)\n",
    "plt.title('residuals from GSR: r = %0.3f'%residcor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.20160961],\n",
       "       [ 0.20160961,  1.        ]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.corrcoef(r1,r2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
{'fraction_non_alphanumeric': 0.22235056309558188, 'fraction_numerical': 0.045047646549234766, 'mean_word_length': 2.450199203187251, 'pattern_counts': {'":': 70, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4997324', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1453, 'n_words': 196}