EMBLAZON JAVA
  • Home
  • Technical Writing
    • Media Science
    • Sciencey Science
    • Programming Secrets >
      • Pair Programming
      • Interview Tips
    • Genetic Engineering >
      • Genetic Engineering Dont's
      • Dinosaur Repository
    • Parallel Computing
    • Web Development >
      • What is Email
      • What is a URL
    • Electricity >
      • The Truth in the Oscilloscope
    • Identity Management >
      • Online Identity
    • Mathematics >
      • Multiplication
      • Axioms
      • Step Functions
      • MultiBase Numbers
    • JVM >
      • Java 8 Optional
      • From Java to Groovy
      • Geb And The Functional Automated Test Ecosystem
      • The Art of Integration
    • Postgres Dev Env Setup
    • Elasticity Pasticity
    • Throttling
  • Websites
  • Blog
  • Meanwhile in Chicago

Step Functions

1. Step Functions In a Nutshell

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
Graph of a regular function. Image by KSmrq - licensed under CC by 2.5
Graph of a stepwise function
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

This is a job for... Stepwise Function!

Axioms (Assumptions):
  • We have a function called random() that randomly returns a float between 0 and 1

The problem here is to convert from a float to an integer in a stepwise manner.

def randomlySelectFromList(List things) {
    def randFloat = random()
    def rawIndex = (randFloat * things.size())
    def normalizedIndex = floor(rawIndex)
    return things[normalizedIndex]
}

6. Verification

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

Test case A:
randFloat = .1
Expected outcome: 0
rawIndex = .1 * 4 = .4
normalizedIndex = floor(rawIndex) = floor(.4) = 0
.1 -> 0 √

Test case B:
randFloat = .3
Expected outcome: 1
rawIndex = .3 * 4 = 1.2
normalizedIndex = floor(rawIndex) = floor(1.2) = 1
.3 -> 1 √

Test case C:
randFloat = .6
Expected outcome: 2
rawIndex = .6 * 4 = 2.4
normalizedIndex = floor(rawIndex) = floor(2.4) = 2
.6 -> 2 √

Test case D:
randFloat = .8
Expected outcome: 3
rawIndex = .8 * 4 = 3.2
normalizedIndex = floor(rawIndex) = floor(3.2) = 3
.8 -> 3 √

JAVA

GROOVY

SPRING

Copyright © 2018 Emblazon Java, all rights reserved
  • Home
  • Technical Writing
    • Media Science
    • Sciencey Science
    • Programming Secrets >
      • Pair Programming
      • Interview Tips
    • Genetic Engineering >
      • Genetic Engineering Dont's
      • Dinosaur Repository
    • Parallel Computing
    • Web Development >
      • What is Email
      • What is a URL
    • Electricity >
      • The Truth in the Oscilloscope
    • Identity Management >
      • Online Identity
    • Mathematics >
      • Multiplication
      • Axioms
      • Step Functions
      • MultiBase Numbers
    • JVM >
      • Java 8 Optional
      • From Java to Groovy
      • Geb And The Functional Automated Test Ecosystem
      • The Art of Integration
    • Postgres Dev Env Setup
    • Elasticity Pasticity
    • Throttling
  • Websites
  • Blog
  • Meanwhile in Chicago