title: How do we fit a sigmoid function in Python?
body: <p>For reproducibility reasons, I am sharing the simple dataset I am working <a href="https://drive.google.com/open?id=1Ip-ez4X4FGI07lWr6gmyz3PzIaUDbhR-" rel="nofollow noreferrer">here</a>.</p>
  
  <p>To make it clear of what I am doing - from column 2, I am reading the current row and compare it with the value of the previous row. If it is greater, I keep comparing. If the current value is smaller than the previous row's value, I want to divide the current value (smaller) by the previous value (larger). Accordingly, below is my source code.</p>
  
  <pre><code>import numpy as np
  import scipy.stats
  import matplotlib.pyplot as plt
  import seaborn as sns
  from scipy.stats import beta
  
  protocols = {}
  
  types = {"data_v": "data_v.csv"}
  
  for protname, fname in types.items():
      col_time,col_window = np.loadtxt(fname,delimiter=',').T
      trailing_window = col_window[:-1] # "past" values at a given index
      leading_window  = col_window[1:]  # "current values at a given index
      decreasing_inds = np.where(leading_window &lt; trailing_window)[0]
      quotient = leading_window[decreasing_inds]/trailing_window[decreasing_inds]
      quotient_times = col_time[decreasing_inds]
  
      protocols[protname] = {
          "col_time": col_time,
          "col_window": col_window,
          "quotient_times": quotient_times,
          "quotient": quotient,
      }
      plt.figure(); plt.clf()
      plt.plot(quotient_times, quotient, ".", label=protname, color="blue")
      plt.ylim(0, 1.0001)
      plt.title(protname)
      plt.xlabel("quotient_times")
      plt.ylabel("quotient")
      plt.legend()
      plt.show()
      sns.distplot(quotient, hist=False, label=protname)
  </code></pre>
  
  <p>This gives the following plots.</p>
  
  <p><a href="https://i.stack.imgur.com/k1mLq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k1mLq.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MnTp9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MnTp9.png" alt="enter image description here"></a></p>
  
  <p>As we can see from the plots</p>
  
  <ul>
  <li><strong>Data-V</strong> has a quotient of 0.8 when the <code>quotient_times</code> is less than 3 and the quotient remains 0.5 if the <code>quotient_times</code> is
  greater than 3.</li>
  </ul>
  
  <p>I have also fitted it into a beta distribution using the following code</p>
  
  <pre><code>xt = plt.xticks()[0]  
  xmin, xmax = min(xt), max(xt)  
  lnspc = np.linspace(xmin, xmax, len(quotient))
  
  alpha,beta,loc,scale = stats.beta.fit(quotient)  
  pdf_beta = stats.beta.pdf(lnspc, alpha, beta,loc, scale)  
  plt.plot(lnspc, pdf_beta, label="Data-V", color="darkblue", alpha=0.9)
  plt.xlabel('$quotient$')
  #plt.ylabel(r'$p(x|\alpha,\beta)$')
  plt.title('Beta Distribution')
  plt.legend(loc="best", frameon=False)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6B5Hy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6B5Hy.png" alt="enter image description here"></a></p>
  
  <p>How can we fit the <code>quotient</code> (defined above) into a sigmoid function to have a plot something like the following? </p>
  
  <p><a href="https://i.stack.imgur.com/u3ds1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u3ds1.png" alt="enter image description here"></a></p>