Monday, November 14, 2011

Man's Inhumanity to man.

At the risk of alienating some of our readers, I am enclosing a link to a Yahoo Youtube video
about kidnapper brigands in Mindanao beheading their captives.

It is not for the faint of heart. Here is the link: Youtube

Thursday, November 10, 2011

Why I did not watch Wowowie tv show

It seems that the overall level of intelligence will be lowered when you watch this tv show. If I did catch it public places or buses, I only do intently focus on it when the sexily clad dancers do their thing. Anyways, here is a series of questions and the funny or smart answers the emcee participants have dissed out. (Translated from Tagalog.)


  1. If those who eat greens are called vegetarians, what do you call those who eat humans?
  2. Ans. humanitarian!
  3. What are Michael and Raphael?
    Ans. Ninjas

  4. In what country most Hindus come from?
    Ans. Hindunesia!

  5. What do you take out from an egg before you cook them?
    Ans. Hair! (expecting the "shell")

  6. If H20 is water, what is CO2?
    Ans. Cold water!


  7. Complete the sentence: Beauty is in the eye of the ____? Ans. Tiger! (Beholder is the right answer!)

Wednesday, November 9, 2011

R quick reference

  1. Help and demos.
    The home page of R is http://www.r-project.org
    type help.start() to use a browser to view standard documentation
    type demo() to view demos of the capabilities of R.
    type ?topic to view manual pages of an R function or library.

  2. To recover last computed expression in R:
    Use the special variable .Last.value

  3. How do I display floating point number to a specified number of digits?
    options(digits = 10)
    when you want finer control, you may use sprintf("%.nf", value) where n must be replaced by
    the numerical value of the number of digits.

  4. Creating sequences.
    X <- seq(1,100) X <- seq(1, 100, by = 3) X <- seq(1, 100, length=5) Note that the spacing in sequence X is given w = (endvalue, startvalue)/(length - 1).
  5. What are the elementary operations on sequences or vectors.
    All variables in R are vectors! Two vector operands are first set to the same length,
    recycling elements if necessary.

    Arithmetic operations +, -, *, / works pairwise.
    Functions or operations applied to a vector result in new vector.
    X^2 gives a new vector with the square of the elements in original X.
    X*X gives the same results!


  6. To determine the number of elements in a vector
    length(X)
  7. To extract the kth element(indexing starts at 1)
    X[k]
  8. To return a vector with the kth element deleted.
    X[-k]
  9. To return a vector with some elements deleted.
    X[-c(1,3, 5)] will delete the elements at the first, third and fifth position.
    To repeat a vector.
    rep(X, ntimes) will return a new vector repeated ntimes.
    X <-c(1,2) rep(X, 5) will display [1] 1 2 1 2 1 2 1 2 1 2
  10. To concatenate two or more vectors
    c(X,Y, Z)
  11. To compute the product of all elements in a vector.
    prod(X)
  12. To assign an element v at a specific position k.
    X[k] <- v Not that if k is greater than length of X, X will be enlarged! To filter elements of X meeting a condition. X[condition] For example, X[X > 5] will display a new vector with element values > 5.

    To sum all elements in a vector
    sum(X)
    To sort a vector
    sort(X)


  13. How do you mark lines as comments in R?
    Start each line (leading spaces are ignored) with a #.

  14. How do I edit a user defined function definition?

    Method 1. use fix(function_name)

    You mispelled length(X):

    gm <- function(X){ exp(sum(log(X))/lenggth(X)) } use fix(gm). edit the offending line and save file. Now type gm. The erroneous definition now has been saved! Method 2. use edit(function_name) Type gm <- edit(gm) This has the same behavior as fix. Method 3. body(function_name)) for one line definitions, this may work out well from the command line. body(gm) <- expression({exp(sum(log(X))/length(X))}) Method 4. as.list(body(function_name))) for multiline definitions, use as.list, taking into note that the line 1 is always the starting {. Hence the first line of the function is body(function_name[[2]]. To change a specific line, body(function_name[[line#+1]] <- substitute(newexpression). Reference:http://stackoverflow.com/questions/2458013/what-ways-are-there-to-edit-a-function-in-r
  15. What are reserved variables in R?
    Do not use c, t, q, F, T

  16. In conditional tests, what are the available comparison operators?
    !expr NOT
    >,<, != inequalities = equality
  17. Does R provide bitwise operators?
    bitAnd(a, b)
    bitOr (a, b)
    bitXor(a, b)

    All functions above works on 32 bitwide unsigned integers

  18. What are the available programming control structures in R?
    conditional if
    if (condition) expr
    conditional if-else
    if (cond) expr1 else expr2

    switch
    switch(expr, ...)

    ifelse
    ifelse(test,yes,no)

    looping constructs and cotrol statements:
    for (var in seq) expr
    while (cond) expr
    repeat expr
    break
    next

  19. How do I input a program or data file to R?
    source(filename)

  20. How do I redirect output to a file from R?
    sink(outfilename)

  21. What are the functions for handling libraries or packages?
    library() # list all available packages
    library(lib.loc = .Library) # list all packages in the default library
    library(help = splines) # documentation on package 'splines'
    library(splines) # load package 'splines'
    require(splines) # the same
    search() # "splines", too
    detach("package:splines")

  22. How do I install contributed packages?
    install.packages() interactive install
    install.packages(pkgname) install a specified package.
    remove.packages(pkgname) removes a package

    library() # list all available packages
    library(lib.loc = .Library) # list all packages in the default library
    library(help = splines) # documentation on package 'splines'
    library(splines) # load package 'splines'
    require(splines) # the same
    search() # "splines", too
    detach("package:splines")


    Standard repositories are "http://cran.r-project.org"and its mirror site
    http://lib.stat.cmu.edu/R/CRAN.

    You should be root to install the libraries systemwide.

Tuesday, October 18, 2011

Testing code highlighter for blogger.

import math
printf "the square root of 2 is ", math.sqrt(2)


This is good! The code highlighter is from http://heisencoder.net/2009/01/adding-syntax-highlighting-to-blogger.html.

This is not the first time we applied this highlighter. It is used for example in our other blog , My Other Life as a Programmer.
Because Google is always trying to find ways to make blogs interesting, the CSS code should be inserted after any template css.
The code should be inserted as escaped html, and in case you need an HTML escaper, you can use the online tool at

http://www.accessify.com/tools-and-wizards/developer-tools/quick-escape/default.php

Our blog lost its latex and source code rendering abilities!

Please be patient. Google just recently introduced live view?! on our templates and it destroyed the current addins for latex and program source code rendering! I thought I can easily return it back to the previous simple design!

I will spend more time on these problems. So please be patient.

Tests : $$\cos(x) = e^x$$

It was not blogger.com fault! But the mathtex server has been down actually since June, 2011!. See the posting in
http://watchmath.com/vlog/

We have fixed the Latex problem now. Now on to the code highlighter!


Wednesday, October 12, 2011

Blogger dynamic views design: we are not ready for this!

I tried just recently dynamic views as suggested by the blogger blogging software. I found out that the syntax highlighter does NOT work and that the adsense ads disappeared. So I am restoring the simple design the site used previously.


We shall not stop trying to improve the site! So bear with us a little as we try to increase our google page rank and increase our revenues so we can buy books, gadgets to please you better with original articles as much as possible!

Sunday, October 2, 2011

Python, statistics: Ranking a single array of raw scores

There are some nonparametric statistical routines which expects rank values. If we are given raw scores
like X= [1,3,3,5,1,7], the rank values would be [1,2,2,3,1,4] if tied ranks get the same rank value.

But the ranks can be transformed to [0.5, 2.5, 2.5,3, .5, 4] if ties are replaced by the mean of tied ranks.

Here is Python code to compute ranks based on raw scores according to four strategies published in the reference.


"""
File      scores2ranks.py
Author    Ernesto Adorio, PhD.
               UPDEPP at Clarkfield, Pampanga
               ernesto.adorio@gmail.com
Desc      Conversion of raw scores to ranks using various strategies.
Version   0.0.1 October 1, 2011
License   Educational use only with proper attribution for research purposes.
Reference http://en.wikipedia.org/wiki/Ranking
"""

def scores2ranks(X, ztol = 1.0e-1, breakties = 1):
   """
    Converts raw scores to ranks, returning an array of ranks.
    Args
      X - scores to convert to ranks
      ztol - equality comparison tolerance
      breakties- strategy:
        0 - None.                               1234            ordinal ranking
        1 - replace  ties by mean of tied ranks.1 2.5, 2.5, 4 fractional ranking.
        2 - (competition rank)                  1224            standard competition ranking.
        3 - replace ties by highest tied rank.  1334            modified competition ranking. 
        4 - replace rank after ties in sequence 1223            dense ranking.
    References: For conversion of matrix scores to ranks using fractional ranking:
     http://my-other-life-as-programmer.blogspot.com/2011/02/python-converting-raw-scores-to-ranks.html         
   """
   Z = [(x, i) for i, x in enumerate(X)]
   Z.sort()
   n = len(Z)
   Rx = [0] * n 
   for j, (x,i) in enumerate(Z):
       Rx[i] = j+1
   if breakties == 0:
      return Rx
   s = 1           # sum of ties.
   start = end = 0 # starting and ending marks.
   for i in range(1, n):
       if abs(Z[i][0] -Z[i-1][0]) < ztol and i != n-1:
          pos = Z[i][1]
          s+= Rx[pos]
          end = i 
       else: #end of similar x values.
          if breakties == 1:
             tiedRank = float(s)/(end-start+1)
             for j in range(start, end+1):
                Rx[Z[j][1]] = tiedRank
          if breakties == 2 or  breakties == 4:
             tiedRank = Rx[Z[start][1]]      
             for j in range(start, end+1):
                Rx[Z[j][1]] = tiedRank
          if breakties == 3:
             tiedRank = Rx[Z[end][1]]      
          for j in range(start, end+1):
              Rx[Z[j][1]] = tiedRank
          start = end = i
          s = Rx[Z[i][1]]  
   
 
   if breakties == 4:
         #ensure that  the ranks are in sequence!
         for i, x in enumerate(sorted(list(set(Rx[:])))):
             for j, y in enumerate(Rx):
                 if y == x:
                    Rx[j] = i+1  
   return Rx
 

if __name__ == "__main__":
    X= [1,3,3,5,  1,  7]
    print "X = ", X
    print scores2ranks(X,  breakties = 4)
When the above code is run, it outputs
$ python scores2ranks.py 
X =  [1, 3, 3, 5, 1, 7]
strategy 0 : [1, 3, 4, 5, 2, 6]
strategy 1 : [1.5, 3.5, 3.5, 5.0, 1.5, 6]
strategy 2 : [1, 3, 3, 5, 1, 6]
strategy 3 : [2, 4, 4, 5, 2, 6]
strategy 4 : [1, 2, 2, 3, 1, 4]



I will be grateful if readers will discover any mistake.