Math-Speak: Step-wise functions take real-numbered data and convert it to discrete data. Code-Speak: float to int
2. What is a Step Function?
A Step Function is a Mathematical concept. It's another name for a piecewise constant function.
What this means is when you graph it, it's not a nice smooth line like you're used to seeing in graphs... It's short horizontal pieces that are disjointed. They sort of look like steps.
Graph of a regular function. Image by KSmrq - licensed under CC by 2.5
Graph of a Stepwise Function. Notice the disjointed nature. Licensed under public domain.
3. How do you define a Step Function?
Mathematically, it's super simple. Here's the mathematical definition of the step function pictured in the second graph above:
for -∞ <= x < x1, y = α0 for x1 <= x <= x2, y = α1 for x2 <= x < x3, y = α2 for x3 <= x < x4, y = α3 for x4 <= x < ∞, y = α4
4. Lets Get Fancy With Code
Ok, now here's some stuff I developed a couple years ago... It's a method for converting real-numbered data into integer plot points.
Say you have interest in taking all your disparate data and converting it to some real number. Say you analyze all my demographics for some reason and you decide I am a .9375, whatever that means to you.
Ok, now your boss says we need to make that mean something. Let's say we want to implement a stepwise function that maps all the real numbers "fairly" into A, B, C, or D.
(This is what "Data Science" is all about... taking the real world and Categorizing it)
Mathematically, it looks like this:
for 0 <= demographicScore < .25, category = A for .25 <= demographicScore <= .5, category = B for .5 <= demographicScore < .75, category = C for .75 <= demographicScore < 1, category = D
You can code that straight up with just a bunch of if-else statements... That's boring.
Let's look at the general solution
5. Implementing Generalized StepWise Functions (in Groovy)
Task: Given a variable sized list, randomly select an element from a list
Any time you write an Algorithm, you must verify it. Let's take a look at how I'd verify this algorithm.
To verify is to test. To test, you must pick your test cases. To keep things simple, I'll choose a test value from each of the 4 categories. This will not be an exhaustive test coverage. It would not stand up to my production standards. But it is fit for illustrative purposes.
Let's assume a list size of 4. Then we expect our randomlySelectFromList function to behave in a stepwise manner like this:
0 <= x < .25 --> 0 .25 <= x < .5 --> 1 .5 <= x < .75 --> 2 .75 <= x < 1 --> 3