{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Solutions to Exercises\n", "\n", "All the code below must be run in context. None will work here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 01-0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Econcat = np.concatenate([np.linalg.eig(vec)[0].reshape((1, M)) for vec in A])\n", "# Econcat = np.concatenate([np.linalg.eig(vec)[0][None] for vec in A])\n", "print np.allclose(Econcat, Evec)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%capture out\n", "from IPython.parallel import Client\n", "\n", "N = 1000\n", "M = 4\n", "np.random.seed(201)\n", "A = np.random.random((N, M, M))\n", "%timeit Evec = np.array(zip(*map(np.linalg.eig, A))[0])\n", "for Neng in range(1, 9):\n", " ##%pxconfig --targets :2\n", " dview = engines[:Neng]\n", " dview.block = True\n", " dview.activate()\n", " dview.scatter('A', A.copy())\n", " %px import numpy as np\n", " %timeit %px Evec = np.array(zip(*map(np.linalg.eig, A))[0])\n", " Evec_parallel = dview.gather('Evec')\n", " #print np.allclose(Evec, Evec_parallel)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print out.stdout.split('\\n')[0].split()\n", "values = [l.split()[5] for l in out.stdout.split('\\n')[1:9]]\n", "plt.plot(np.arange(1, 9), values)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 02-0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pymks\n", "??pymks.bin" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 02-1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following, we will just check that one test response is reasonably close to a response calculated with the influence coefficients. We construct a `test_microstructre` and use the `fipy_response` function to calculate `test_response`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.random.seed(103)\n", "test_microstructure = np.random.random(N**2)\n", "test_response = fipy_response(test_microstructure, dt=dt, N=N)\n", "\n", "binned_test_microstructure = bin(test_microstructure, Nbin).reshape((N, N, Nbin))\n", "Fm = np.fft.fft2(binned_test_microstructure, axes=(0, 1))\n", "Fr = np.sum(Fm * Fcoeff, axis=-1)\n", " \n", "calc_response = np.fft.ifft2(Fr, axes=(0, 1)).real.flatten()\n", "\n", "\n", "print test_response[:20]\n", "print calc_response[:20]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 03-0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import scipy.signal\n", "import scipy.ndimage\n", "?scipy.signal.fftconvolve\n", "\n", "y_alt = scipy.ndimage.convolve(X[0], filter(np.linspace(0, 20, Nspace)), mode='wrap')\n", "print np.allclose(np.roll(y_alt, 40), y[0])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 03-1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "X0 = np.random.random((1, Nspace))\n", "y0 = model.predict(X0)\n", "import scipy.ndimage\n", "y_alt = scipy.ndimage.convolve(X0[0], filter(np.linspace(0, 20, Nspace)), mode='wrap')\n", "print np.allclose(np.roll(y_alt, 40), y0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 04-0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "\n", "mse = metrics.mean_squared_error\n", "\n", "Nbins = np.arange(2, 100, 10)\n", "\n", "errors = []\n", "\n", "for Nbin in Nbins:\n", " print Nbin\n", " model = MKSRegressionModel(Nbin=Nbin)\n", " model.fit(X, y)\n", " errors.append(mse(model.predict(X), y))\n", " \n", "plt.plot(Nbins, errors)\n", "plt.xlabel('Nbin')\n", "plt.ylabel('MSE')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 04-1\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "errors = []\n", "\n", "Nbins = np.arange(2, 20)\n", "for Nbin in Nbins:\n", " model = MKSRegressionModel(Nbin=Nbin)\n", " model.fit(X_train, y_train)\n", " errors.append(mse(model.predict(X_test), y_test))\n", " \n", "plt.plot(Nbins, errors)\n", "plt.xlabel('Nbin')\n", "plt.ylabel('MSE')\n", "\n", "argmin = np.argmin(errors)\n", "print \"optimal Nbin: {0}, mse: {1:1.3e}\".format(Nbins[argmin], errors[argmin])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 05-0" ] }, { "cell_type": "code", "collapsed": false, "input": [ "??model.resize_coeff" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 06-0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 06-1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "??FastMKSRegressionModel" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }