{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Hipster Effect: An IPython Interactive Exploration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*This notebook originally appeared as a [post](http://jakevdp.github.io/blog/2014/11/11/the-hipster-effect-interactive/) on the blog [Pythonic Perambulations](http://jakevdp.github.io). The content is BSD licensed.* It has been adapted to use HoloViews by [Philipp Rudiger](http://philippjfr.com)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This week I started seeing references all over the internet to [this paper](http://arxiv.org/abs/1410.8001): *The Hipster Effect: When Anticonformists All Look The Same*. It essentially describes a simple mathematical model which models conformity and non-conformity among a mutually interacting population, and finds some interesting results: namely, **conformity among a population of self-conscious non-conformists is similar to a phase transition in a time-delayed thermodynamic system**. In other words, with enough hipsters around responding to delayed fashion trends, a plethora of facial hair and fixed gear bikes is a natural result.\n",
    "\n",
    "Also naturally, upon reading the paper I wanted to try to reproduce the work. The paper solves the problem analytically for a continuous system and shows the precise values of certain phase transitions within the long-term limit of the postulated system. Though such theoretical derivations are useful, I often find it more intuitive to simulate systems like this in a more approximate manner to gain hands-on understanding."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mathematically Modeling Hipsters\n",
    "\n",
    "We'll start by defining the problem, and going through the notation suggested in the paper. We'll consider a group of $N$ people, and define the following quantities:\n",
    "\n",
    "- $\\epsilon_i$ : this value is either $+1$ or $-1$. $+1$ means person $i$ is a hipster, while $-1$ means they're a conformist.\n",
    "- $s_i(t)$ : this is also either $+1$ or $-1$. This indicates person $i$'s choice of style at time $t$. For example, $+1$ might indicated a bushy beard, while $-1$ indicates clean-shaven.\n",
    "- $J_{ij}$ : The influence matrix. This is a value greater than zero which indicates how much person $j$ influences person $i$.\n",
    "- $\\tau_{ij}$ : The delay matrix. This is an integer telling us the length of delay for the style of person $j$ to affect the style of person $i$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The idea of the model is this: on any given day, person $i$ looks at the world around him or her, and sees some previous day's version of everyone else. This information is $s_j(t - \\tau_{ij})$.\n",
    "\n",
    "The amount that person $j$ influences person $i$ is given by the influence matrix, $J_{ij}$, and after putting all the information together, we see that person $i$'s mean impression of the world's style is\n",
    "\n",
    "$$\n",
    "m_i(t) = \\frac{1}{N} \\sum_j J_{ij} \\cdot s_j(t - \\tau_{ij})\n",
    "$$\n",
    "\n",
    "Given the problem setup, we can quickly check whether this impression matches their own current style:\n",
    "\n",
    "- if $m_i(t) \\cdot s_i(t) > 0$, then person $i$ matches those around them\n",
    "- if $m_i(t) \\cdot s_i(t) < 0$, then person $i$ looks different than those around them\n",
    "\n",
    "A hipster who notices that their style matches that of the world around them will risk giving up all their hipster cred if they don't change quickly; a conformist will have the opposite reaction. Because $\\epsilon_i$ = $+1$ for a hipster and $-1$ for a conformist, we can encode this observation in a single value which tells us what which way the person will lean that day:\n",
    "\n",
    "$$\n",
    "x_i(t) = -\\epsilon_i m_i(t) s_i(t)\n",
    "$$\n",
    "\n",
    "Simple! If $x_i(t) > 0$, then person $i$ will more likely switch their style that day, and if $x_i(t) < 0$, person $i$ will more likely maintain the same style as the previous day. So we have a formula for how to update each person's style based on their preferences, their influences, and the world around them."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But the world is a noisy place. Each person might have other things going on that day, so instead of using this value directly, we can turn it in to a probabilistic statement.  Consider the function\n",
    "\n",
    "$$\n",
    "\\phi(x;\\beta) = \\frac{1 + \\tanh(\\beta \\cdot x)}{2}\n",
    "$$\n",
    "\n",
    "We can plot this function quickly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "hv.extension('bokeh', 'matplotlib', width=90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%output backend='matplotlib'\n",
    "%%opts NdOverlay [aspect=1.5 figure_size=200 legend_position='top_left']\n",
    "x = np.linspace(-1, 1, 1000)\n",
    "curves = hv.NdOverlay(key_dimensions=['$\\\\beta$'])\n",
    "for beta in [0.1, 0.5, 1, 5]:\n",
    "    curves[beta] = hv.Curve(zip(x, 0.5 * (1 + np.tanh(beta * x))),\n",
    "                            '$x$', '$\\\\phi(x;\\\\beta)$')\n",
    "curves"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This gives us a nice way to move from our preference $x_i$ to a probability of switching styles. Here $\\beta$ is inversely related to noise. For large $\\beta$, the noise is small and we basically map $x > 0$ to a 100% probability of switching, and $x<0$ to a 0% probability of switching. As $\\beta$ gets smaller, the probabilities get less and less distinct."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Code\n",
    "\n",
    "Let's see this model in action. We'll start by defining a class which implements everything we've gone through above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class HipsterStep(object):\n",
    "    \"\"\"Class to implement hipster evolution\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    initial_style : length-N array\n",
    "        values > 0 indicate one style, while values <= 0 indicate the other.\n",
    "    is_hipster : length-N array\n",
    "        True or False, indicating whether each person is a hipster\n",
    "    influence_matrix : N x N array\n",
    "        Array of non-negative values. influence_matrix[i, j] indicates\n",
    "        how much influence person j has on person i\n",
    "    delay_matrix : N x N array\n",
    "        Array of positive integers. delay_matrix[i, j] indicates the\n",
    "        number of days delay between person j's influence on person i.\n",
    "    \"\"\"\n",
    "    def __init__(self, initial_style, is_hipster,\n",
    "                 influence_matrix, delay_matrix,\n",
    "                 beta=1, rseed=None):\n",
    "        self.initial_style = initial_style\n",
    "        self.is_hipster = is_hipster\n",
    "        self.influence_matrix = influence_matrix\n",
    "        self.delay_matrix = delay_matrix\n",
    "        \n",
    "        self.rng = np.random.RandomState(rseed)\n",
    "        self.beta = beta\n",
    "        \n",
    "        # make s array consisting of -1 and 1\n",
    "        self.s = -1 + 2 * (np.atleast_2d(initial_style) > 0)\n",
    "        N = self.s.shape[1]\n",
    "        \n",
    "        # make eps array consisting of -1 and 1\n",
    "        self.eps = -1 + 2 * (np.asarray(is_hipster) > 0)\n",
    "        \n",
    "        # create influence_matrix and delay_matrix\n",
    "        self.J = np.asarray(influence_matrix, dtype=float)\n",
    "        self.tau = np.asarray(delay_matrix, dtype=int)\n",
    "        \n",
    "        # validate all the inputs\n",
    "        assert self.s.ndim == 2\n",
    "        assert self.s.shape[1] == N\n",
    "        assert self.eps.shape == (N,)\n",
    "        assert self.J.shape == (N, N)\n",
    "        assert np.all(self.J >= 0)\n",
    "        assert np.all(self.tau > 0)\n",
    "\n",
    "    @staticmethod\n",
    "    def phi(x, beta):\n",
    "        return 0.5 * (1 + np.tanh(beta * x))\n",
    "            \n",
    "    def step_once(self):\n",
    "        N = self.s.shape[1]\n",
    "        \n",
    "        # iref[i, j] gives the index for the j^th individual's\n",
    "        # time-delayed influence on the i^th individual\n",
    "        iref = np.maximum(0, self.s.shape[0] - self.tau)\n",
    "        \n",
    "        # sref[i, j] gives the previous state of the j^th individual\n",
    "        # which affects the current state of the i^th individual\n",
    "        sref = self.s[iref, np.arange(N)]\n",
    "\n",
    "        # m[i] is the mean of weighted influences of other individuals\n",
    "        m = (self.J * sref).sum(1) / self.J.sum(1)\n",
    "        \n",
    "        # From m, we use the sigmoid function to compute a transition probability\n",
    "        transition_prob = self.phi(-self.eps * m * self.s[-1], beta=self.beta)\n",
    "        \n",
    "        # Now choose steps stochastically based on this probability\n",
    "        new_s = np.where(transition_prob > self.rng.rand(N), -1, 1) * self.s[-1]\n",
    "        \n",
    "        # Add this to the results, and return\n",
    "        self.s = np.vstack([self.s, new_s])\n",
    "        return self.s\n",
    "    \n",
    "    def step(self, N):\n",
    "        for i in range(N):\n",
    "            self.step_once()\n",
    "        return self.s\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll create a function which will return an instance of the HipsterStep class with the appropriate settings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_sim(Npeople=500, hipster_frac=0.8, initial_state_frac=0.5, delay=20, log10_beta=0.5, rseed=42):\n",
    "\n",
    "    rng = np.random.RandomState(rseed)\n",
    "\n",
    "    initial_state = (rng.rand(1, Npeople) > initial_state_frac)\n",
    "    is_hipster = (rng.rand(Npeople) > hipster_frac)\n",
    "\n",
    "    influence_matrix = abs(rng.randn(Npeople, Npeople))\n",
    "    influence_matrix.flat[::Npeople + 1] = 0\n",
    "\n",
    "    delay_matrix = 1 + rng.poisson(delay, size=(Npeople, Npeople))\n",
    "\n",
    "    return HipsterStep(initial_state, is_hipster, influence_matrix, delay_matrix=delay_matrix,\n",
    "                       beta=10 ** log10_beta, rseed=rseed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exploring this data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we've defined the simulation, we can start exploring this data. I'll quickly demonstrate how to advance simulation time and get the results.\n",
    "\n",
    "First we initialize the model with a certain fraction of hipsters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sim = get_sim(hipster_frac=0.8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To run the simulation a number of steps we execute ``sim.step(Nsteps)`` giving us a matrix of identities for each invidual at each timestep."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = sim.step(200)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can simply go right ahead and visualize this data using an Image Element type, defining the dimensions and bounds of the space."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%opts Image [width=600]\n",
    "hv.Image(result.T, ['Time', 'individual'], 'State', bounds=(0, 0, 100, 500))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that you know how to run the simulation and access the data have a go at exploring the effects of different parameters on the population dynamics or apply some custom analyses to this data. Here are two quick examples of what you can do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%opts Curve [width=350] (line_color='black') Image [width=350]\n",
    "hipster_frac = hv.HoloMap(kdims='Hipster Fraction')\n",
    "hipster_curves = hipster_frac.clone(shared_data=False)\n",
    "for i in np.linspace(0.1, 1, 10):\n",
    "    sim = get_sim(hipster_frac=i)\n",
    "    img = hv.Image(sim.step(200).T.astype('int8'), ['Time', 'individual'], 'Bearded',\n", 
    "                   bounds=(0, 0, 500, 500), group='Population Dynamics')\n",
    "    hipster_frac[i] = img\n",
    "    agg = img.aggregate('Time', function=np.mean, spreadfn=np.std)\n",
    "    hipster_curves[i] = hv.ErrorBars(agg) * hv.Curve(agg)\n",
    "(hipster_frac + hipster_curves)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Your turn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What intuitions can you develop about this system? How do the different parameters affect it?"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
