Monday, September 22, 2014

Change of variables in expectation

Expectation

For discrete variables it's defined as

\begin{align} E(x) = \sum_x x p(x) \end{align}

Expectation of a function

Substitute $x$ with $f(x)$ in the above formula:

\begin{align*} E(f(x)) &= \sum_{f(x)} f(x) p(f(x)) \\ &= \sum_{f(x)} f(x) \sum_{x_0 | f(x_0) = f(x)} p(x_0) \\ &= \sum_x f(x) p(x) \end{align*}

But let's go an extra mile:

Expectation of a function of a function

\begin{align} E(f(g(x))) &= \sum_{f(g(x))} f(g(x)) p(f(g(x))) \\ &= \sum_{f(g(x))} f(g(x)) \sum_{D_1} p(g(x)) \\ &= \sum_{f(g(x))} f(g(x)) \sum_{D_1} \sum_{D_2} p(x) \\ &= \sum_x f(g(x)) p(x) \end{align}

If you let $y = g(x)$, then from (3) we can have:

\begin{align} &\sum_{f(g(x))} f(g(x)) \sum_{D_1} p(g(x)) \\ &= \sum_{f(y)} f(y) \sum_{D_1} p(y) \\ &= \sum_{y} f(y) p(y) \\ \end{align}

Connecting (5) and (8) we get a beautiful transformation:

\begin{align} \sum_{y} f(y) p(y) = \sum_x f(g(x)) p(x) \end{align}

This can go on and on, of course:

\begin{align} \sum_{y} f(y) p(y) &= \sum_x f(g(x)) p(x) \\ &= \sum_z f(g(h(z))) p(z) \\ &= \cdots \end{align}

Friday, August 1, 2014

Sunday, July 27, 2014

WordPress br tag problem solved

WordPress automatically inserts br tags, well, everywhere. After some researching, I came up with a CSS trick to minimize its effect on my blog:

 .entry-content br {
  display: block !important;
line-height: 1% !important;
margin-top: -0.2em !important;
margin-bottem: -0.2em !important;
}

A more colorful commandline

To distinguish different filetypes by colors, do this in your ~/.bash_aliases:

# some aliases
alias lla='ls -AlFGh'
alias ll='ls -lFGh'
alias la='ls -AG'
alias ls='ls -G'
alias l1='ls -1G'
alias grep='grep --color=always'
alias fgrep='fgrep --color=always'
alias egrep='egrep --color=always'

For the ls command, -G is doing the magic.

Make a python script pipable

Python is a very nice language for scripting, it is only natural that it can be used as a pipe receiver. Here is the simplest example:

#!/usr/bin/env python3

# file: /tmp/echo.py
import sys

fromstdin = sys.stdin.read()
print(fromstdin)

Make it executable, then try this in shell:

echo "hi, there" | /tmp/echo.py

Karabiner: a keymapping tool for osx

I have found a wonder tool for remapping keys on osx: Karabiner - Software for OS X

User manual: Karabiner - Software for OS X

User's keymap settings are stored in a file called private.xml, it can opened like this:

Note that there is also a "Event Viewer" in which you can look up keycodes and app identifiers in action.

After browsing through the documentation I have come up with a xml configuration specifically effective for Eclipse text editor area:

<?xml version="1.0"?>
<root>
  <appdef>
    <appname>ECLIPSE</appname>
    <equal>org.eclipse.platform.ide</equal>
  </appdef>
  <item>
    <name>Esc to ctrl-bracket three times in Eclipse</name>
    <identifier>private.esc.to.c.bracket.three.times.eclipse</identifier>
    <only>ECLIPSE</only>
    <uielementrole_not>AXTextArea</uielementrole_not>
    <autogen>
      __KeyToKey__
      KeyCode::ESCAPE,
      KeyCode::BRACKET_LEFT, ModifierFlag::CONTROL_L, KeyCode::BRACKET_LEFT, ModifierFlag::CONTROL_L, KeyCode::BRACKET_LEFT, ModifierFlag::CONTROL_L 
    </autogen>
  </item>
</root>

Since I use vim mode (vrapper plugin), I use the Esc key a lot, so I mapped Esc to its equivalent ctrl-[ repeated three times.

This is indeed a extraordinary piece of software, I will definitely learn more about it later!