Friday, October 3, 2014

I will cover pricing models in this post.  Specifically we will see how to cluster and classify numerical data.  This kind of data differs from the examples we have seen earlier in that there are many different attributes. One way to overcome this limitation is to use KNN algorithm.  KNN stands for K-nearest neighbors.   To make numerical predictions we need to know which variables are important. Let us take an example of a numerical prediction with one variable.  The book we review gives the example of pricing a wine bottle based on age and initialized by rating. In particular, it says if the age of the bottle is past its peak then it is almost immediately cheap and if it hasn’t attained its peak age yet then its price shoots up.  It does this by adjusting the price with a difference which is calculated as a constant times the delta between the actual age and peak age in the case when age has exceeded the peak. On the other hand, it increases the price to the order of five fold.  The choice to use these weights is interesting as we will see shortly. 

Given that we can come up with a random sample.  To price the items, we will be using the neighbors of the item that are most similar to it so that we can average and get it closer to the others. There are two issues here. One is if we choose a small value for n, then the predicted price will be too dependent on variations. At the same time if we choose a large value for n, then the predicted price will not be accurate. The second issue is that we need to define how similar two items are.  In the latter case, we use the Euclidean distance which is the square root of the sum of squares of the differences between age and rating.  It’s interesting to note that this similarity function treats the age and rating the same. This is a well known weakness of KNN and one that is overcome with  weighted neighbors, inverse function and Gaussian smoothing.

#coding Exercise
Given an array of non-negative integers, each element represents the maximum jump length at that position. Starting from the first of an array can we reach the last?

bool canJump(int[] A)
{
  if (A == null) return false;
 int index = 0;
 while (index < A.Length)
 {
   if (index + A[i] == A.length - 1) return true;
   if (index +A[i] <= index) return false;
   index = index + A[i];
  }
 return false;
}

#coding exercise
Two linked lists are given, representing two non-negative numbers. The digits are stored in reverse order and each node contains a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6-> 4)
Output: (7->0->8)

List<int> AddTwoNumbers(List<int> l1, List<int> l2)
{
// parameter validation and return
int num1;
int num2;
int factor = 1;
for (int i =0; i< l1.Count; i++)
{
checked { num1 += l1[i] * factor;}
checked { factor = factor * 10;}
}
factor = 1;
for (int i =0; i< l2.Count; i++)
{
checked { num2 += l2[i] * factor;}
checked { factor = factor * 10; }
}
List<int> result = new List<int>();
int num3;
checked { num3 = num1 + num2; }
int count = 0;
int temp = num3;
while(temp != 0)
{
result.Add(temp % 10);
temp = temp / 10;
count++;
}
return result;
}
I'm not feeling well since yesterday night.

No comments:

Post a Comment