Skip to content

Instantly share code, notes, and snippets.

@karlnapf
Created March 29, 2016 18:31
Show Gist options
  • Save karlnapf/e75cadb1dcd36ae2a8ca3fe11279b3f8 to your computer and use it in GitHub Desktop.
Save karlnapf/e75cadb1dcd36ae2a8ca3fe11279b3f8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.\n",
" warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')\n"
]
}
],
"source": [
"# this is at git commit: 6ef795ba6e83706b903dc84a43a36e496597c394 \n",
"from modshogun import *\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# use scipy for generating samples\n",
"from scipy.stats import norm, laplace\n",
"\n",
"def sample_gaussian_vs_laplace(n=220, mu=0.0, sigma2=1, b=np.sqrt(0.5)): \n",
" # sample from both distributions\n",
" X=norm.rvs(size=n, loc=mu, scale=sigma2)\n",
" Y=laplace.rvs(size=n, loc=mu, scale=b)\n",
" \n",
" return X,Y\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(220,) (220,)\n",
"time taken: 20.2334070206\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEACAYAAACwB81wAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEX9JREFUeJzt3X+MZWV9x/H3B1cWkLjZquwaFn80WAXTshpdNTRxWitC\n07CkaRA1qUpsTKnV1CaFNU122zQqTTSx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9WSSi4CvAr805ZrWq5kZyySnA18GPtYdQc+kFeqcifGsqmeANyR5MbA/ydur6p/W\nuo6VDFDnqsdzrEf4VfXOqvqVvuWXu39vBhYWPxYl2Qo8ssx7PNz9+yjwFdbmlgxHgVf0/byta1u6\nzVkrbDNpK9ZZVU8sfhSsqn8AXpjkF9auxIHMwliuaFbGMskGeiF6XVXddIJNZmI8V6pzVsazr54f\nA7cAb1ry0kyM56Ll6hxmPNdySudm4APd+vuBn/uFSHJad4RAkhcBFwDfW4Pavg2cneSVSU4GLuvq\n7Xcz8LtdbW8FfrQ4RbWGVqyzf64xyQ56p97+cG3L7HXP8vOLszCWi5atc4bG8ovAwar63DKvz8p4\nPmedszCeSV6a7gzBJKcC76R38kO/qY/nIHUOM55reS+dq4F9SS4HHgQuBUjycuDzVfVb9KaDvpLe\nbRc2ANdX1f5JF1bLXCiW5MO9l+tvqurrSX4zyQPAT4APTrquYeoEfifJ7wPHgaeAd691nUluoHfn\nwZck+T6wGziZGRrLQepkNsbyfOB9wL3dfG4Bn6B3ptbMjOcgdTID4wm8HNibZPHkkOuq6huz9v/6\nIHUyxHh64ZUkNcJHHEpSIwx8SWqEgS9JjTDwJakRBr4kNcLAl6RGGPiS1AgDX5Ia8X9e2zrHyUWB\nUAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f6b285249d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# plain performance test\n",
"\n",
"X,Y = sample_gaussian_vs_laplace()\n",
"print X.shape, Y.shape\n",
"\n",
"feats_p = RealFeatures(X.reshape(1,len(X)))\n",
"feats_q = RealFeatures(Y.reshape(1,len(Y)))\n",
"width=1\n",
"k = GaussianKernel(10, width) # should have a constructor that does not require to specify cache size\n",
"\n",
"mmd = QuadraticTimeMMD() # should take p and q as argument\n",
"mmd.set_p(feats_p)\n",
"mmd.set_q(feats_q)\n",
"mmd.set_kernel(k)\n",
"\n",
"# this should be able to receive a CSignal abort, made my Python stall a couple of times\n",
"#mmd.set_num_null_samples(10000)\n",
"\n",
"start = time.time()\n",
"mmd.set_num_null_samples(1000) \n",
"null_samples = mmd.sample_null() # seems to take quite long, and not running on all my cpus (only 2/4 it seems). I havent checked properly, but this is only first impression\n",
"print \"time taken:\", time.time()-start\n",
"plt.hist(null_samples, bins=20);"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1000,) (1000,)\n",
"time taken: 0.198345899582\n",
"time taken: 32.5579361916\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXUAAAEACAYAAABMEua6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAD3BJREFUeJzt3X+MZWddx/H3py3tFioVqt1VSotFFyORAIGCoDCh1taq\nlD/8AVVLi0FCFAgaQovozh/+wY8YRAwx6LJphY1BBFoVQtvUiykE+rtboJQalS3QDiKLpNpdWvfr\nH3OnToaZvfeec2Zm59n3K7mZe859zjnfnJx85pln7nNOqgpJUhuO2+wCJEnDMdQlqSGGuiQ1xFCX\npIYY6pLUEENdkhoyMdST7E6ykGTfsnXPTXJTktvHP5+zvmVKkqYxTU99D3D+inXvAN5aVc8CdgHv\nHLowSdLsJoZ6Vd0IHFix+n7g1PH77we+NnBdkqQOMs2M0iRnAX9fVc8YL58JfBooIMALquq+9SxU\nkjRZ13+U7gZeV1VnAm8E3j9cSZKkrrr21L9TVY9f9vl/VdWpa2zrzWUkqYOqyqzbnDBlu4xfS+5N\n8uKq+lSSc4EvD12YVpdkvqrmN7uOFnguh+X5HFbXDvHEUE+yF5gDTkuyn8Vvu/w28N4kJwIHx8uS\npE02MdSr6uI1PnrewLVIknpyRunWM9rsAhoy2uwCGjPa7AI05T9Kex0gKcfUJWk2XbPTnrokNcRQ\nl6SGGOqS1BBDXZIaYqhLUkOmnVG6YZI8GfitHru4par+Yah6JGkrOepCHfgNeMEfw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GLBzk2uSoMO1\n2SnUq+q8tT4bD/pvr6qFJDuAb6yxj/vHP/8jyUdZ/DPZUF/s2Zy5bPmM8bqVbZ48oY0WTTyfVfXg\nsvefSPLeJE+sqm9tUI0t8docUJdrcz2GX64BLh2/fyVw9coGSR6b5JTx+8cBPwd8fh1q2YpuBn40\nyVlJTgRezuI5Xe4a4BJ4dGbvt5eGvPQ9Jp7P5WO+Sc5h8au+BvrawtrjvF6bs1vzfHa5Ntfj3i9v\nBz6U5FXAV4BfHRf0Q8BfVtUvsjh089HxLQROAD5YVdeuQy1bzlqTupK8ZvHjel9VfTzJhUn+Bfhv\n4LLNrPloNs35BH45yWuBh4GHgF/bvIqPbkn2sng3xtOS7Ad2ASfitdnJpPNJh2vTyUeS1BC/UihJ\nDTHUJakhhrokNcRQl6SGGOqS1BBDXZIaYqhLUkMMdUlqyP8BD6gfMnPGCbMAAAAASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f6b28524810>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# same as above but with more data\n",
"\n",
"X,Y = sample_gaussian_vs_laplace(n=1000)\n",
"print X.shape, Y.shape\n",
"\n",
"feats_p = RealFeatures(X.reshape(1,len(X)))\n",
"feats_q = RealFeatures(Y.reshape(1,len(Y)))\n",
"width=1\n",
"k = GaussianKernel(10, width)\n",
"\n",
"joint_features = RealFeatures(np.hstack((X, Y)).reshape(1, len(X)+len(Y)))\n",
"k.init(joint_features,joint_features)\n",
"start = time.time()\n",
"k.get_kernel_matrix()\n",
"print \"time taken:\", time.time()-start\n",
"\n",
"mmd = QuadraticTimeMMD()\n",
"mmd.set_p(feats_p)\n",
"mmd.set_q(feats_q)\n",
"mmd.set_kernel(k)\n",
"\n",
"start = time.time()\n",
"mmd.set_num_null_samples(100) # Is the kernel matrix precomputed in here? It should be. Why isnt this faster?\n",
"# when doing permutation test, is the kernel matrix only computed once and then summed over differently? Or is it copied around?\n",
"# what takes the time here?\n",
"null_samples = mmd.sample_null()\n",
"print \"time taken:\", time.time()-start\n",
"plt.hist(null_samples, bins=20);"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5000,) (5000,)\n",
"time taken: 11.0228209496\n",
"time taken: 164.471257925\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAEACAYAAABWLgY0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADmlJREFUeJzt3X2sZHddx/H3Z7uWp8ryJFvTpUWeAwErmFJSkq4hSFsT\n+g8JD0a0idoQGohELSEkbWJi5B8fEEytqZhiCMSKsFrQYvBKqqEU2m0r7EILCKXQ9QEaQqFY4Osf\nc0ont/funHvvPNz99v1KJnvOzG/OfPbcnc+c+5s5s6kqJEk97Vl1AEnS4ljyktSYJS9JjVnyktSY\nJS9JjVnyktTYzJJPciDJx5N8NsltSd60ybh3Jrk9yeEkZ84/qiRpq/aOGPMD4C1VdTjJKcBnklxX\nVUcfGJDkfODpVfXMJC8GrgDOXkxkSdJYM4/kq+ruqjo8LH8HOAKctm7YhcDVw5gbgH1J9s85qyRp\ni7Y0J5/kqcCZwA3rbjoNuHNq/S4e+kIgSVqy0SU/TNVcA7x5OKKXJO1yY+bkSbKXScG/t6o+vMGQ\nu4CnTK0fGK5bvx2/KEeStqGqst07zrwwmW//w+PcfgFw7bB8NvDJTcbVmMdb9QW4fNUZzPnwywkU\n1IzLZZtcTy3uMTe7bP6Yq96Xu+VnPq/9u5PunHkkn+Qc4JeB25LcPAnN24Azhge+sqo+kuSCJHcA\n9wIXbesVR5I0VzNLvqr+DThpxLhL5pJIkjQ3nvG6sbVVBxhpbdUBRlpbdYCR1lYdYLaDqw4w1tqq\nA4y0tuoAi5Zhvmc5D5ZUbffNA6m5yQcTtvt8DNt5bq3iMR9O5rV/d9KdHslLUmOWvCQ1ZslLUmOW\nvCQ1ZslLUm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seUlqzJKXpMb+\nH8nwSggAS41fAAAAAElFTkSuQmCC\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f6b2839a110>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# same as above, how far can be go in terms of data?\n",
"\n",
"X,Y = sample_gaussian_vs_laplace(n=5000)\n",
"print X.shape, Y.shape\n",
"\n",
"feats_p = RealFeatures(X.reshape(1,len(X)))\n",
"feats_q = RealFeatures(X.reshape(1,len(Y)))\n",
"width=1\n",
"k = GaussianKernel(10, width)\n",
"\n",
"joint_features = RealFeatures(np.hstack((X, Y)).reshape(1, len(X)+len(Y)))\n",
"k.init(joint_features,joint_features)\n",
"start = time.time()\n",
"k.get_kernel_matrix()\n",
"print \"time taken:\", time.time()-start\n",
"\n",
"mmd = QuadraticTimeMMD()\n",
"mmd.set_p(feats_p)\n",
"mmd.set_q(feats_q)\n",
"mmd.set_kernel(k)\n",
"\n",
"start = time.time()\n",
"mmd.set_num_null_samples(10)\n",
"null_samples = mmd.sample_null()\n",
"print \"time taken:\", time.time()-start\n",
"plt.hist(null_samples, bins=20);"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(100,)\n",
"0 9.79900360107e-05\n",
"1 5.28958201408\n",
"2 4.12843990326\n",
"3 5.09187102318\n",
"4 4.14835810661\n",
"5 5.15286803246\n",
"6 3.57714080811\n",
"7 3.16793704033\n",
"8 2.932336092\n",
"9 3.41076993942\n",
"10 2.9093170166\n",
"11 3.13496112823\n",
"12 3.31558704376\n",
"13 2.8714017868\n",
"14 2.85468292236\n",
"15 2.90135788918\n",
"16 2.92182588577\n",
"17 3.0945250988\n",
"18 3.04426503181\n",
"19 2.88243985176\n"
]
}
],
"source": [
"# are p-values uniformly distributed if H0 is true?\n",
"# Initial try, too slow. Made smaller below, but this case also is interesting\n",
"\n",
"X,_ = sample_gaussian_vs_laplace(n=100)\n",
"X2,_ = sample_gaussian_vs_laplace(n=100)\n",
"\n",
"print X.shape\n",
"\n",
"feats_p = RealFeatures(X.reshape(1,len(X)))\n",
"feats_q = RealFeatures(X2.reshape(1,len(X2)))\n",
"width=1\n",
"k = GaussianKernel(10, width)\n",
"\n",
"mmd = QuadraticTimeMMD()\n",
"mmd.set_p(feats_p)\n",
"mmd.set_q(feats_q)\n",
"mmd.set_kernel(k)\n",
"\n",
"\n",
"mmd.set_num_null_samples(500) # if this is not called, there is no default and p-values are all nan\n",
"\n",
"#mmd.set_null_approximation_method()\n",
"\n",
"num_runs = 20\n",
"p_values = np.zeros(num_runs)\n",
"\n",
"last = time.time()\n",
"for i in range(num_runs):\n",
" print i, time.time()-last\n",
" last = time.time()\n",
" \n",
" # this is a usual use-case: repeating the same experiment multiple times.\n",
" # I guess we can save computation here, e.g. the precomputed kernel matrix\n",
" # .... maybe use some kind of lazy evaluation?\n",
" # also, notice the inhomogeneous times. Is that just by chance? Sometimes there are outliers up to 9s.\n",
" stat = mmd.compute_statistic()\n",
" p_values[i] = mmd.compute_p_value(stat)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXUAAAEACAYAAABMEua6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADeBJREFUeJzt3V2sZfVdxvHvU8aiLXVCR5mJvIqmxZDipLFYQy+GaoR6\nA+GiWky1JhoSpRK9sNDEwI1pekOsMZjQUoKJpFEaLVAptMKkwZQWbXmnWKpQwc7UFmiKEaXl58XZ\nA4fxzOx99t5nrzW/+X6SlayzX2Y9WWftZ9b5n/U/K1WFJKmH1wwdQJK0PJa6JDViqUtSI5a6JDVi\nqUtSI5a6JDUytdSTnJTkziQPJ3kwyfsnj1+Z5KkkX54s5299XEnS4WTadepJdgG7quq+JMcB/wxc\nAPwq8L2qunrrY0qSZrFt2guqah+wb7L+fJJHgRMnT2cLs0m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OtZ+KdZx3scs++JR4JcAJuPHbwL+daUpVysc+qfUTffmUs7U6xATkZJc\nsvZ0XVtVf5/kV5I8DvwXa8MO7cyyL4A/Bt4IXDM5S32xqs4eLvXyzbgfXvWWlYdckRk/H19Ncjvw\nAPAD4NqqemTA2FtixuPiQ8D1Se5nrez+qKqeGS711klyI7AH2JHkG6xd+fNaFuhNJx9JUiPezk6S\nGrHUJakRS12SGrHUJakRS12SGrHUJakRS12SGrHUJamR/wNLLN+0xeubDQAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f6b28524210>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# are p-values uniformly distributed if H0 is true?\n",
"# permutation test version. TODO test other null approx methods for that\n",
"num_runs = 200\n",
"p_values = np.zeros(num_runs)\n",
"\n",
"last = time.time()\n",
"for i in range(num_runs):\n",
" X,_ = sample_gaussian_vs_laplace(n=100)\n",
" X2,_ = sample_gaussian_vs_laplace(n=100)\n",
"\n",
" feats_p = RealFeatures(X.reshape(1,len(X)))\n",
" feats_q = RealFeatures(X2.reshape(1,len(X2)))\n",
" width=1\n",
" k = GaussianKernel(10, width)\n",
"\n",
" mmd = QuadraticTimeMMD()\n",
" mmd.set_p(feats_p)\n",
" mmd.set_q(feats_q)\n",
" mmd.set_kernel(k)\n",
"\n",
" mmd.set_num_null_samples(50)\n",
" stat = mmd.compute_statistic() # would be good if compute_p_value() with no arguments computed the statistic itself\n",
" p_values[i] = mmd.compute_p_value(stat)\n",
"\n",
"# does this look more or less uniform (it has to be)?\n",
"plt.hist(p_values);"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.055\n"
]
}
],
"source": [
"# does the type1 error (wrong alarm) calibration work.\n",
"# if so, it should be equal to the alpha on average\n",
"# permutation test version. TODO test other null approx methods for that\n",
"\n",
"num_runs = 200\n",
"rejections = np.zeros(num_runs)\n",
"\n",
"last = time.time()\n",
"for i in range(num_runs):\n",
" X,_ = sample_gaussian_vs_laplace(n=100)\n",
" X2,_ = sample_gaussian_vs_laplace(n=100)\n",
"\n",
" feats_p = RealFeatures(X.reshape(1,len(X)))\n",
" feats_q = RealFeatures(X2.reshape(1,len(X2)))\n",
" width=1\n",
" k = GaussianKernel(10, width)\n",
"\n",
" mmd = QuadraticTimeMMD()\n",
" mmd.set_p(feats_p)\n",
" mmd.set_q(feats_q)\n",
" mmd.set_kernel(k)\n",
"\n",
" mmd.set_num_null_samples(50)\n",
" alpha=0.05\n",
" rejections[i] = mmd.perform_test(alpha)\n",
"\n",
"# we expect roughtly 0.05 (false) rejection rate here\n",
"print np.mean(rejections)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Other\n",
"\n",
" * I just realised that `CHypothesisTest::compute_p_value` uses this `find_position_to_insert`. This method should go away (delete it), and we should just do `np.mean(stat>null_samples)` which automatically gives a p-value\n",
" \n",
" * The print out of `KernelManager::kernel_at() : getting the kernel 0` should be removed. As well as all the others ... this makes things slow\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment