2

I am trying to simulate some data where a response can either be right (1) or wrong (0). I am thus trying to find a distribution in which there are four conditions (in this case degrees of a circle).

Thus, the x axis is pi/2, pi, pi1.5, 2pi. I have normalised this from 0 to 1 to make this easier. On the y axis I would like the probability of answering correct so is 0-1 or 0-100 etc. I am trying to generate/plot a sigmoid function such that the probability is higher when the condition is closer to 1 and lower when the condition is closer to 0.

I cannot seem to be able to generate the sigmoid between 0 and 1, it just gives me a straight line unless i set x = np.linspace (-10,10,10). How can I do this? The code I currently have is below. Thank you!

I was originally going to use a beta distribution as this is better suited (as it is degrees around a circle) but cannot seem to get it into the shape I want. Any help would be greatly appreciated!

def sigmoid(x,x0=0,k=0.5):
            return (1 / (1 + np.exp(-x)))
x = np.linspace(0,1,10)
sallicap
  • 75
  • 1
  • 7
  • Well the sigmoid is `0` for negative infinity and `1` for positive infinity. The idea is thus to noever really rach `0` anc `1`. – Willem Van Onsem Jul 08 '20 at 16:30
  • Ah I see thank you! Would you then suggest normalising my 4 conditions to fit -10 to +10 or is this bad practice? – sallicap Jul 08 '20 at 16:37

1 Answers1

8

As you are happy with normalising to the range [0,1], consider normalising to [-1,1]

import numpy as np
import matplotlib.pyplot as plt

def norm(x):
    # normalise x to range [-1,1]
    nom = (x - x.min()) * 2.0
    denom = x.max() - x.min()
    return  nom/denom - 1.0

def sigmoid(x, k=0.1):
    # sigmoid function
    # use k to adjust the slope
    s = 1 / (1 + np.exp(-x / k)) 
    return s

# un-normalised data
x = np.linspace(-4,+4,100)
# normalise the data
x = norm(x) 

plt.plot(x, sigmoid(x))
plt.show()

Sigmoid activation function

Homer
  • 398
  • 2
  • 6
  • Thank you! How would one alter this function to change the steepness of the slope & where on the x axis it 'begins'? Sorry I have not studied this :) – sallicap Jul 08 '20 at 17:04
  • 1
    No worries :) - I have edited the question (see the code segment) to answer your comment. Just for clarity; the diagram was plotted with k=0.1 which I have specified as the default value for the sigmoid function. – Homer Jul 08 '20 at 17:32