Friday, December 16, 2016

Recently I was looking for libraries to write to the serial port on Linux machines so that I could simulate a KVM switch. The PySerial package comes very useful here.
Some examples borrowed from online tutorials and reposted here:


import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyUSB1',
    baudrate=9600,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_TWO,
    bytesize=serial.SEVENBITS
)

ser.isOpen()

print 'Enter your commands below.\r\nInsert "exit" to leave the application.'

input=1
while 1 :
    # get keyboard input
    input = raw_input(">> ")
    if input == 'exit':
        ser.close()
        exit()
    else:
        ser.write(input + '\r\n')
        out = ''
        time.sleep(1)
        while ser.inWaiting() > 0:
            out += ser.read(1)

        if out != '':
            print ">>" + out

We could also talk to a DRAC card.
#codingexercise:
  static int GetKthLargest(int[,] A, int row, int col, int k)  
            {  
            int r = row-1;  
            int c = col-1;  
            int prev = r * col + c; 
           if (k <= 0 || k > prev) return -1;
            int count = 0;  
            while ( count < k)  
            {  
            // compare left and top and increment r or c  
            // the idea is that the k largest elements will be a contiguous block   
            // and the next element for inclusion will be either to the left of this block or   
            // on the top of this block  
            // we just need to propagate the front on all rows one cell at a time.  
            int m = r;  
            int n = c;  
            int rselected = r*col + c; // position of selected on top  
            int cselected = r*col + c; // position of selected at left  
            if (count + 1 == k) return A[r,c];   
            int rmax = Int16.MinValue;  // max value on top  
            int cmax = Int16.MinValue; // max value at left  
            for (int j = n ; j < col && m > 0; j++ )  
            {  
               if ( A[m-1, j] > cmax){  
                    rselected = (m-1)*col+j 
                    cmax = A[m-1,j];  
               }else break;  
            }  
            for (int i = row-1; i >= m && n > 0; i--)  
            {  
               if ( A[i, n-1] > rmax){  
                    cselected = i*col+n-1;  
                    rmax = A[i, n-1];  
               }else break;  
            }  
            if (cmax > rmax) 
            {  
               r = rselected/col;  
               c = rselected%col; 
                
            }else{  
               r = cselected/col;  
               c = cselected%col; 
                
            }  
            if ((r+1)*col + c == prev) 
             {   
                  prev = r*col + c;   
                  row--;   
              }  
            count++;  
            }  
            return A[r,c];  
            } 
    class Program     {         static void Main(string[] args) 
         {
            var A = new int [4, 4] {{10, 13, 15, 19 }, {21, 23, 24, 27}, {29, 31, 33, 37}, {41, 43, 45, 47}}; 
            for (int k = 1; k < 17; k++) 
            { 
                Console.WriteLine("K={0},Value={1}", k, GetKthLargest(A, 4, 4, k)); 
            }
         }
      } 
K=1,Value=47 
K=2,Value=45 
K=3,Value=43 
K=4,Value=41 
K=5,Value=37 
K=6,Value=33 
K=7,Value=31 
K=8,Value=29 
K=9,Value=27 
K=10,Value=24 
K=11,Value=23 
K=12,Value=21 
K=13,Value=19 
K=14,Value=15 
K=15,Value=13 
K=16,Value=10 

also we can optimize the above by removing the inner iterations because they are already sorted and cmax or rmax can be picked and compared.  

Thursday, December 15, 2016

Today we continue to discuss the difference between the frequentist and the Bayesian approach. We said they perceive the probability differently.  A frequentist defines probability as the long run frequency of a certain measurement or observation. The frequentist says there is a single source of truth and measurement samples may be noisy instances of this truth. Bayesians define probability as the chance of a hypothesis given incomplete knowledge.  Bayesians don't see data as a source of truth. A frequentist can calculate probabilities precisely, but often not the probabilities we want. A Bayesian can calculate the probabilities we want but often cannot do it precisely.
The example most often used for this is the repeated coin tossing. When we can't tell whether the coin was biased or fair, the probabilites calculated for k heads and n-k tails could be such that they are further away from the expected truth. This is why we move into the Bayesian domain.  The chance of getting say 13 heads and 7 tails with a fair coin is about 7.4%. Here's how The number of ways to choose k heads in n tosses is n choose k the binomial coefficient of x^k and since each one of these sets would suffice, we write the probability as n-choose-k times p^k times (1-p)^(n-k) where n-choose-k is (n!)/((k!)((n-k)!)). Substituting n = 20, k = 13 we get 7.4%. If we assumed on the other hand that the coin was biased and the long running frequency was 13/20 instead of 1/2, then the chance of getting 13 heads and 7 tails is about 18.4%, a factor of 2.5 higher compared to the fair coin. To really tell apart whether the coin was biased or the experimental evidence just happened to come out this way, we need to include some prior idea that it is more likely that the coin was fair. In other words, to answer the question on the fairness of the coin, we need to have prior information about how likely we think it is. A Bayesian sees an experiment and uses it to test a hypothesis. He now writes the probability calculated as a fair coin to be a conditional probability for his belief that the experiment was done with a fair coin by calculating the sum of all possible complimentary truths i for each of which there is a probability of 13h7t.  This is equivalent to normalizing the probability calculated as a fair coin over all possible truths. While this is adding complexity, it is in fact articulating beliefs. The frequentist calculation was algebraic and therefore has application in significance testing. Majority of the statistics are taught with the frequentist approach. This does not mean Bayesian is not algebraic. Many would argue it has more powerful framework which is why it is considered advanced. And both advanced and simple will give you the same answer in some simple cases. Bayesian approach is considered more practical because it does not add the dimension of frequently repeating something and also answers questions that have practical pertinence. For example, if we have one set of measurements for the time period of a pendulum, then most likely the distribution is a Gaussian with a measured mean and spread and this follows from making certain assumptions on prior distributions. Since science is about gathering evidence that supports certain models of reality, the author Maarten Ambaum concludes that "the Bayesian viewpoint is very likely the most relevant probabilistic framework for science"

#codingexercise
Given a binary tree, you need to prune it such that the final tree contains all root-to-leaf sums more than equal to k

Node PruneKHeavy(Node root, int k, ref sum)
{
 if (root == null) return null;
 sum += root.data;
root.left = PruneKHeavy(root.left, k, sum );
 root. right = PruneKHeavy(root.right, k, sum);
 if (root.left == null && root.right == null)
 {  
   if (sum < k){
       sum -= root.data;
      delete root;
      root = null;
      return root;
   }
 }
sum-= root.data;
return root;



The same strategy works for K light nodes as well.

Wednesday, December 14, 2016

Today we discuss the difference between Frequentist and Bayesian approach. These are two different camps who need to use probabilities to make sense of the world but differ in the way they perceive probability. I will state their well known definition up front and elaborate afterwards. A frequentist defines probability as the long run frequency of a certain measurement or observation. The frequentist says there is a single source of truth and measurement samples may be noisy instances of this truth. Bayesians define probability as the chance of a hypothesis given incomplete knowledge. Bayesians don't see data as a source of truth. They just see data as evidence for a particular hypothesis.  For Bayesian, there is just data which we can use as evidence for a particular hypothesis.

The archetypal example for this is the repeated tossing of a coin to see what the long run frequency is of heads or tails, this long run frequency then asymptotes to the truth.  Bayesians just observe the series of coin tosses and then uses this information to make deductions about, for example, how likely it is that the coin is fair. The frequentist view is best challenged by the question whether there is a true fraction to which the frequency in the coin tossing experiment should asymptote. The suggestion is that as a frequentist we have an idealized view even if it is specific. It can be argued that the coin tosses happen under the same circumstance but in such a way that the outcome is maximally random. There is contradiction here. The coin tosses are the same in so far that each experiment must have the same validity and samples the same set of possible outcomes in the same way. But the coin tosses have to be different in some sense, otherwise the outcomes would be the same. The long run frequency for a real coin tossing is as much a function of the way the experiment is performed as it is a property of the coin. Therefore we need to allow some uncertainity. If we get an asymptotic frequency of 1/2 for heads then we either have done a truly randomized experiment that the coin was fair or we may have done a biased experiment with an unfair coin and the frequentist view cannot differentiate. In the bayesian view, we may be able to say so. In a twenty toss of a coin, we may have 13 heads and 7 tails with a fair coin and the chance for getting this is about 7.4%. With an unbiased coint the frequency is 13/20 instead of 1/2 and the chance increases to 18.4% The Bayesian includes a non-frequentist prior information about how likely we think that the coin was fair to start with.
To state how fair the coin is, Bayesian's use conditional probability as p(fair|13h7t) = p(13h7t|fair)p(fair)/p(13h7t) where the denominator is the sum of all truths with variations of the fairness of the coin. The frequentist gives a probability of an event given a truth which in this case is p(13h7t|fair) and tries to use this information for any statements. Therefore the difference between the two camps is described as follows: A frequentist can calculate probabilities precisely, but often not the probabilities we want. A Bayesian can calculate the probabilities we want but often cannot do it precisely.
Courtesy: Maarten Ambaum


#puzzle
assuming a fair coin and a controlled toss each time, what is the probability of getting k heads in n tosses?
The number of ways to choose k heads in m tosses is n choose k the binomial coefficient of x^k and since each one of these sets would suffice, we write the probability as n-choose-k times p^k times (1-p)^(n-k) where n-choose-k is (n!)/((k!)((n-k)!))

#codingexercise
Given a continuous stream of alphabets determine if we have a palindrome or not. The stream is sending one character at a time.
The key to solving this is to keep track of the count of each character seen.
If the length of the string is even, then each character should appear a multiple of two times.
Else there can be at most one character with an odd count of 1

Bool isPalindrome ( stream s)
{
    var h = new Hashtable ();
    Char c = s.Read ();
    While ( c != EOF)
    {
       If (h.keys.contains (c)) {
            h [c] += 1;
       }
       Else {
             h.Add (c);
       }
       If (h.Values.Sum () %2 == 0)
        {
            Foreach  (int val in Values) {
               If (val % 2 != 0) {
                     Return false;
               }
            }
}
else
{
Int count = 0;
Foreach  (int val in Values) {

               If (val % 2 != 0) {

                     count += 1;

               }
               if (count > 1)
                     return false;
}
    }
return true;
}

Tuesday, December 13, 2016

We continue discussing the paper "Nested sampling  for general Bayesian computation" by Skilling
 With this technique, nested sampling estimates directly how the likelihood function relates to previous value of the discrete random variable. And it can be used to compare two data models for the same set by comparing the conditional probability that is assigned after the relevant evidence is taken into account.
This method directly computes the evidence. This technique simplifies the evidence calculation by not summing over the parameters directly but instead performing it on the cumulated prior mass  that cover likelihood values greater than say lambda. As Lambda increases, the enclosed mass X decreases from one to zero. We were looking at the transformation of computing the evidence based on the prior mass instead of the parameters. We looked at the integration performed. This paper essentially says don't navigate the parameter space. It is sufficient to explore a likelihood weighted space
This method fits nicely into the frequentist versus Bayesian arguments. As we know these are two different schools of thought and vary in their methodology. A frequentist takes a model and reports the outcome. A Bayesian applies apriori information alongwith the model. But that's not all. A Bayesian defines a probability as an indication of the chance of a happening.  Therefore he computes the possible outcomes in terms of probabilities. He samples directly from the data and assumes the parameters are unknown.  A frequentist is someone who believes that the probabilities represent long run frequencies from which events occur. The parameters are fixed throughout the sampling. He invents a fictitious population from which your particular situation is considered a random sample.
There's more to the Frequentist versus Bayesian arguments but an example might help.
If we take an example of tossing coins repeatedly and saying the heads fall with a probability p and tails with a probability 1-p then in 100 tosses we examine 71 heads,  Then what is the probability of the next two consective tosses to be heads. A frequentist bases the answer on frequency where as a Bayesian views the probability as degrees of beliefs in the proposition.
Monte Carlo integration was criticized as being frequentist.  But the same criticism does not hold for nested sampling because it involves a multidimensional integration.
#codingexercise
find distinct palindrome substrings from a given string
void PrintAllPalindromesIn(String a)
{
    for( int i =0; i < a.length; i++)
          printPalindromesWithCenterAt(a,i);
}
void printPalindromesWithCenterAt(string a, int c)
{
int start = c-1;
int end = c+1;
while ( start >= 0 && end <= a.length && a[start] == a[end])
{
console.write(a.substring(start, end-start+1);
start--;
end++;
}
// now repeat with center as c and c+1
}

Monday, December 12, 2016

Yesterday we were looking at LXD containers and how to enable static ip addressing of the containers instead of the conventional DHCP.

We said we needed to specify
 LXD_CONFILE="/etc/default/lxd_dnsmasq.conf"
in /etc/default/lxd-bridge
and for each of the containers set the following
1) entry in the lxd_dnsmasq.conf file
sudo sh -c "echo 'dhcp-host=containername,ipaddress' >> /etc/default/lxd_dnsmasq.conf"
and
2)  eth0 with the static ip address as in the /etc/network/interfaces or equivalent config file with:


auto eth0

iface eth0 inet static

address ABC.DEF.GHI.JKL

netmask 255.255.255.252

network ABC.DEF.GHI.0

gateway www.xxx.yyy.zzz

dns-search example.com sales.example.com dev.example.com

dns-nameservers nameserver1.ip.addr.ess



The gateway is what we set in the route table as


sudo route add default gw www.xxx.yyy.zzz eth0

Remember to initialize the containers with 'lxc init' instead of 'lxc launch'.

#codingexercise
Find all distinct palindromic substrings of a given string
For example, "abaaa" should result in "a" "aa" "aaa" "aba" "b"

        public static void Combine(ref String input, ref StringBuilder candidate, ref List<String> combinations, int level, int start)
        {
            for (int i = start; i < input.Length; i++)
            {
                    if (candidate.contains(input[i]) == false){
                    candidate[level] = input[i];
                    if (candidate.IsPalindrome() && combinations.Contains(candidate) == false)
                         combinations.Add(candidate.ToString());                  
                    if (i < input.Length - 1)
                        Combine(ref input, ref candidate, ref combinations, level + 1, start + 1);
                    candidate[level] = '\0';
            }
          }
        }
bool isPalindrome( List<int> digits, int start, int end)
{bool ret = true;
while(start < end)
{
if (digits[start] != digits[end]) return false;
start++;
end--;
}
return ret;
}
#writeup https://1drv.ms/w/s!Ashlm-Nw-wnWlkDRgzKeR5lVB1Ml

The above code works well for distinct elements of input string. if we also had a frequency table from the input string, we could generate other permutations and check for palindrome.