Skip to content

Instantly share code, notes, and snippets.

@rofr
Created November 19, 2012 13:01
Show Gist options
  • Save rofr/4110520 to your computer and use it in GitHub Desktop.
Save rofr/4110520 to your computer and use it in GitHub Desktop.
LoanApplication distance function
public double calculateDistance(LoanApplication item, LoanApplication item2) {
double distance = 0;
//nominal attributes
if (item.getCarrier() != item2.getCarrier()) distance += 1;
if (item.getGender() != item2.getGender()) distance += 1;
int dayDiff = Math.abs(item.getDayOfWeek() - item2.getDayOfWeek());
if (dayDiff > 3) {
dayDiff -= 1 + 2 * (dayDiff - 4);
}
distance += dayDiff * dayDiff;
int timeOfDayDiff = Math.abs(item.getTimeOfDay() - item2.getTimeOfDay());
if(timeOfDayDiff > 11) {
timeOfDayDiff -= 1 + 2 * (timeOfDayDiff - 12);
}
distance += timeOfDayDiff * timeOfDayDiff;
//scaled numeric range attributes
//TODO: tune scale factors
int requestedAmountDiff = item.getRequestedAmount() - item2.getRequestedAmount();
requestedAmountDiff /= 100;
distance += requestedAmountDiff * requestedAmountDiff;
int yearlyIncomeDiff = item.getYearlyIncome() - item2.getYearlyIncome();
yearlyIncomeDiff /= 10000;
distance += yearlyIncomeDiff * yearlyIncomeDiff;
return Math.sqrt(distance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment