Thursday, December 20, 2007

IBM T40 Internal Modem

The driver for the part of internal modem is loadable from snd_intel8x0m module. To create a modem device, download slmodemd which creates a node at /dev/ttySL0 for the modem which later can be used for the connection via PPP. Enable alsa mode (README) when installing slmodemd.
To run:
modprobe snd_intel8x0m
slmodemd --alsa hw:1

Saturday, November 24, 2007

Options delta hedging and volatility arbitrage

In options theory, delta is one of the greeks parameter that defines the sensitivity of the options price to the underlying asset price. Now, if we hold a portfolio consisting of 1 call option and delta number of corresponding stock (underlying asset), we have created a delta neutral portfolio. Theoretically, the price of portfolio is neutral to any small change of the stock price (note, delta is 1st order derivative) and will earn the risk free rate over a very short period of time. This is one of the reasoning that could be used to derive the famous Black-Scholes PDE. The reason that this applies to only small change in both price and time is because there are other parameters that affect the price of the option, namely gamma and theta.Now, gamma is defined as the measure of the rate of change in the delta (2nd order derivative) and theta is the measure of price sensitivity to the passage of time, thereby they explain the small change constraint.
Mathematically, for call option (european in particular), delta is defined as:

where c is the call option price, and S is the current stock price, N(d1) is the cdf of standard normal distribution defined within 0 and 1. The figure below will explain the delta concept.

The figure shows the call price with respect to the corresponding stock price. As we can see, the red line shows the slope of the curve at one particular stock price. This the delta of the call price at that particular stock price. Above that point, the call price increases at a greater rate than the stock price, and below that point the call price decreases at a slower rate. This is defined by the gamma parameter of the option, i.e. the rate of change of delta.
Now, here comes the interesting part, looking at the curve, one might wonder if there is an exploit such that under volatile market, profit could be earned irrespective of the direction of stock price with no initial investment at all (free lunch and/or arbitrage). Well, there is actually, I believe this is the so-called volatility arbitrage. This is done, again, by setting a portfolio consisting of long call and short delta number of stock price, as explained in the beginning.
Now, let's do some matlab simulations. The code for the simulation is given below:
function value = voltrade(S0,X,r,mu,t,sig,steps,NoPaths)
dt = t/steps;
drift = (mu - 0.5 * sig ^ 2) * dt;
vol = sig * sqrt(dt);
[price, delta, gamma] = tri_Eprice(S0, X, r, t, steps, sig, 0, 1);
Z = [zeros(NoPaths, 1) normrnd(drift, vol, NoPaths, steps)];
Z = (cumsum (Z'))';
S = S0*exp(Z);
ttm= [ones(NoPaths, steps) * dt];
ttm = cumsum (ttm')';
flipttm = [fliplr(ttm) (zeros(NoPaths, 1)+0.0000001)];
ttm = [(zeros(NoPaths, 1)+0.0000001) ttm];
callprice = blackscholes(S, X, r, flipttm, sig, 0) .* exp(-r.*ttm);
Sp = S .* exp(-r.*ttm);
value = mean(callprice - callprice(1,1) + (S0 - Sp) * delta);
subplot (2,1,1);
plot (value);
xlabel ('time step');
ylabel ('portfolio return');
subplot (2,1,2);
plot (mean(S));
xlabel ('time step');
ylabel ('stock price');
Note, the above function will call some external functions that are not defined here. Anyway, the objective of the function is to generate Monte Carlo simulation of stock prices, and at each time step to find the corresponding call option price, and also then to find the return of portfolio based on longing one call option and shorting delta number of stock. The function parameters are : S0 (current stock price), X (call option exercise price), r (risk free rate), mu (expected stock return), t (time to maturity), sig (volatility of stock), steps (number of delta t), and NoPaths (number of paths for Monte Carlo simulation).
Anyway, let's just run the function, first let's see what happen when the expected stock return, mu, is set to 30 % (which is set to be large enough to provide a greater certainty of large positive return, as the simulation doesn't really involve change in volatility parameter in order to explain the concept)
voltrade (50,50,0.05,0.3,0.5,0.4,1000,1000);
The return of the portfolio would look as below:

As you can see, the portfolio, will generate an "expected" positive return of approx. $ 0.8 after 6 months with zero initial investment. It is "expected" because, well, it's only a simulation based on Monte Carlo, which is then based on random numbers generated from certain distribution (lognormal) that is assumed to mimic certain parameters of stock price (there are a lot of assumptions here, this is finance, talking mostly about uncertainty).
Ok, now, let's see what happen if the stock generate an expected negative return (setting mu to -30%):
voltrade (50,50,0.05,-0.3,0.5,0.4,1000,1000);
The result:

As we can readily see, the expected portfolio return is positive even when the stock price declines within the period to maturity. Again, this is done with no initial investment at all (well, except the transaction costs).
Now, what if we set the expected stock return to 0:
voltrade (50,50,0.05,0.0,0.5,0.4,1000,1000);

Well, can't really see anything here but as the expected stock return follows a generalized Wiener process, there is always a probability of large change in price even though mu is set to zero. We could rerun the simulation over and over again and once in a while (lots of while, depends on volatility) the stock will change drastically, either up or down, and the option price will increase correspondingly (in real life, this is the right time to close the portfolio). This is more easily said than done in real life though (can't redo). Note, the simulation above doesn't really involve changes in volatility parameter. Hence, by incorporating an increase in volatility in between the time steps to maturity, the option price will generally increase, even though we set mu to zero. Hence, this strategy will perform well when the current implied volatility is low, and the investor expects that the volatility will increase considerably in the near future. The consideration for sensitivity of the option price with respect to volatility is set by the option greek parameter vega.

Update:
The above explanation and implementation are flawed, refer to here.

Sunday, November 18, 2007

Remote Desktop via Xming

Xming is a port of the X Window System to the Microsoft Windows operating system. Basically, it runs an X Server on your Windows machine, and client application can be run on the remote Linux machine (LM) and displayed on your Windows machine (WM).

To use it, you'll have to install Xming and putty on WM. Now run Xming X Server on localhost:0.0. Then, use Putty to connect to LM, and don't forget to enable X11 forwarding as shown below.


Most probably, X11 forwarding is not enabled on your LM ssh daemon. So change /etc/ssh/sshd_config to include:


X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes
And, restart sshd
/etc/rc.d/rc.sshd restart
Now, you'll need to re-ssh to LM using Putty. Check the event log ensure that "X11 forwarding enabled" is there and on the terminal type echo $DISPLAY whether it's been set by Putty.

Well, that be it. I ran konqueror, and the result is as shown below.

Saturday, November 17, 2007

Remote Desktop to your home Linux PC via tightVNC

TightVNC is a free remote control software package derived from the popular VNC software. Download and install in the Linux PC here. It works in a client server manner where the server is the Linux home PC and the client is the PC from where you want to connect to the server to get the desktop of it. It's insecure as it doesn't contain any encryption apart from the initial user acknowledgment password. Hence, to make it secure we have to use use SSH tunneling. More info here and here. If the home server is firewalled or connected to the outside world via a router, don't forget to enable port forwarding.

In the snapshot above, all connection to port 22 on the router is forwarded to the IP address of the server.
To connect from the client PC, we have to connect to the external IP address of the router. Check your router IP here. The next step is to download putty and vncviewer client from here and here. These are the executables that you want to run from the client PC, and no installation is needed at all. Run Putty with tunneling, and forward client port e.g. 5901 to server port. Next, run the vncviewer client. Sample run would be like below:

Note: ssh tunneling makes the implementation rather slow, as it requires encryption and decryption, thereby bandwidth conservation is required. For example in the server I would run with the command : vncserver -geometry 800x600 depth 16 :01
and ~/.vnc/xstartup would look like below (it's the default actually, note the twm window manager is chosen):
xrdb $HOME/.Xresources
xsetroot -solid grey
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &

Sunday, November 11, 2007

Random number challenge

I read a blog post by Ariya Hidayat on a challenge to generate discrete integer random number 1..7 given a function that is able to generate random numbers (discrete integer) 1..5. This is my answer from the computer engineering point of view:

int r,x;
do{
do{
r = rand5();

if (r > 2 && r <= 4) x = rand5() + 5;

else if (r <= 2) x = rand5();

}while (r > 4);
}while (x > 7);
return x;

This answer is actually a bad one, at least compared to Ariya's answer in the sense that the probability that a call to this function will generate the respective random number directly without involving the loop is really low. Huh ?!?!? Okay, basically let me say that the inner loop will generate hit 80 % of the time. Why? Because there are 5 possibilities, and 4 of them are used to generate discrete integer random number 1..10 while 1 is deliberately ignored. Meanwhile, the outer loop will obviously generate 70 % hit. So, in the end the code will be effective 56 % (as in 80 % x 70 %, right ?) of the time without involving any loop. Actually, I think Ariya's answers are some of the most efficient implementations possible, especially the first one, hehe. Had I seen the challenge earlier, I wouldn't have been able to generate an implementation of the same quality, it would be so so like the above :(.
Now, let's take a glimpse from the mathematical point of view. If you've taken at least one math subject in your undergrad, I believe you should have heard of this term called central limit theorem. Huh?!? what's that?!? hehe, read here. I only know the basic. Basically, if you sum up m random numbers of the same property(mean, variance, etc) you'll end up with a random number with the property of normal distribution with mean m times the original mean and with variance m times the the original mean. So now, you have the ability to generate random number 1..5, and from this you're able to get some normally distributed random numbers, what's next? First, let's see the code to generate rand5 (implemented in matlab for laziness reason).

function r = rand5(m,n)
r = floor(5 * rand(m,n) + 1);
This code will generate a matrix of m row and n column of discrete random numbers. Let's see the code to generate rand7.

function r = rand7(n)
m = 1000;
sig = sqrt(2);
mu = 3;
temp = sum(rand5(m,n));
zn = (temp - m * mu)/(sig * sqrt(m));
x=0.5*erfc(-zn/sqrt(2));
r = floor(mod(x*70,7))+1;

The code above will generate a vector of n discrete random numbers 1..7.
So, how it works? The sig variable is obviously the standard deviation (sqrt of variance) of the random number generated by rand5, while mu is the mean of it which is 3 (correct? (1 + 2 + 3 + 4 + 5)/5 = ?). Now we call the rand5 to generate m by n matrix of it and sum it column by column, hence we get a vector of m number of rows and put it to variable temp. I put m to 1000 here(I know it s*cks and too large and not practical, but my purpose here is to learn something rite :p) such that it is large enough to generate a normal distribution with mean m * mu and standard deviation sig * sqrt(m). The next step is to convert it to standard normal distribution random number zn with mean 0 and standard deviation 1. Finally, convert it to uniform distribution by virtue of standard normal distribution cumulative distribution function(cdf). Again, another uncommon name. Well, basically in the program sense, cdf(x) gives the probabilty (from 0 to 1 obviously) that a random number generator will generate random number less or equal than x with some distribution (normal here) property. So here we get a uniform distribution which seems continous from 0 to 1 but actually it's discrete (and depends on m{that's why the larger the better, as I've told you}). The next step is trivial and obvious.
Now, by calling rand7 function so many times, we are able to generate so many random numbers that we could get a histogram of them. First, let's see the histogram of zn.


As we can see, it is indeed normally distributed with mean 0 (obvious) and variance 1 (unless you're a statistic professor you can't readily see it).
Now, let's check the histogram of rand7.

As we can, not so obviously, see, it is indeed uniformly distributed with discrete integer state 1..7. Cool huh !?! Well, that's about it.
Anyway, this method will make you look knowledgeable and yet at the same time stupid and dumb (because of redundancy and not much logic in it) in front of your interviewer and in the end it's not fun at all as you couldn't "tease" your interviewer with it and it's actually not "harmless" if you don't code in matlab as it requires some matrix and vector optimization to speed up your code in such large numbers of array manipulation.

Sunday, October 28, 2007

Laptop HDD bug

There have been issues on broken HDD on laptop with the recent Ubuntu release installed. Basically it's related to APM feature (which might be specific to the BIOS) whereby the head is retracted when it is on idle state. It is solved by setting off the APM: hdparm -B 255 /dev/hda1. More infos:
http://paul.luon.net/journal/hacking/BrokenHDDs.html
https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/59695
http://ubuntudemon.wordpress.com/2007/10/26/laptop-hardrive-killer-bug/
I've created a script using Python to check the load cycle count of your hdd and whether it has increased by 90 on a daily basis. There is not much comment, apart from the beginning lines and I don't intend to add any more comments on it.
To use it, you'll have to run it at least twice. The first time, it will create a timestamp and save the initial load cycle count of your hard drives. The next time, preferably after several hours (or days), it'll check on whether the load cycle has increased by a significant number (90 cycles on a daily basis) and let you know if it has. That simple, cool (or useless) huh !
# The script is used to check whether harddrive load cycle count has increased by 90 cycles
# on a daily basis (24 hour)
# it doesn't run as a daemon
# to run : python hdcheck.py
# it will create 2 files:
# -timestamp.dat is used to check the delta time since the first run
# -lcc.dat is used to check the increase in load cycle count
# The next time you run it, it will check the current load cycle count and do the calculation
# To start anew delete those 2 files, and rerun
# GAH_2007

import re
import os
import time
import datetime
import cPickle

put, get = os.popen4("df")
dev = []
current_lcc = {}
for dfout in get.readlines():
p = re.compile("/dev/\w\w\w[1-9]")
m = p.match(dfout)
if m:
dev.append(m.group())
for ent in dev:
put, get = os.popen4 ("smartctl -a "+ent+ "| grep Load_Cycle_Count")
for smartctlout in get.readlines():
p = re.compile("\d*\n")
m = p.findall(smartctlout)
p = re.compile("\d*")
n = p.match(m[0])
if n:
current_lcc[ent] = n.group()
time = time.mktime(datetime.datetime.now().timetuple())/3600.0
delta_time = 0
try:
f = open ("timestamp.dat", "r")
timestamp = cPickle.load(f)
f.close()
delta_time = time - timestamp
except:
timestamp = 0
if (timestamp):
print "Time since the first run is", delta_time, "hour"
else:
try:
f = open ("timestamp.dat", "w")
cPickle.dump(time, f)
f.close()
except:
print "unable to save timestamp"

try:
f = open ("lcc.dat", "r")
previous_lcc = cPickle.load(f)
f.close()
lcc_increase = {}
except:
previous_lcc = 0

if (previous_lcc):
for dev in current_lcc.keys():
lcc_increase[dev] = eval(current_lcc[dev]) - eval(previous_lcc[dev])
else:
try:
f = open ("lcc.dat", "w")
cPickle.dump(current_lcc, f)
f.close()
except:
print "unable to save current load cycle count data"

if (timestamp and previous_lcc):
for dev in current_lcc.keys():
lcc_increase_daily = lcc_increase[dev] * 24.0 / delta_time
print dev + " load cycle count increases by "+ str(lcc_increase[dev])+ " cycles("+str(lcc_increase_daily)+" on daily basis)"
if (lcc_increase_daily > 90):
print " Load cycle count increase by more than 90 !!"

Friday, October 19, 2007

Updating slackware, tips and tricks

http://slackware.wordpress.com/2006/09/26/slackware-changelog-september-26th/
Tips and tricks (actually, more of like some useful information):
  • Since I'm using tcsh as the default shell, just a reminder, instead of modifying /etc/profile, I should modify /etc/csh.login. Then from the shell, run "tcsh -l".
  • Azureus is a Java based Bittorrent client. Have been using it for a while as I was unable to install the Bittorrent client. Too much dependencies, I just couldn't resolve the wxPython one (not geeky enough, I guess :P). That's probably one of the problems faced by the user when they try to install Python based program.
  • XFCE is a lightweight desktop environment. Some screenshots. I find it pretty neat and, indeed, light.
  • And this is really funny.

Monday, October 15, 2007

Prex - Real Time Embedded OS

Designed based on micro kernel architecture, Prex is targeted for embedded system application. It also follows the POSIX standard, so many applications can be easily ported to the os environment.
I decided to try it on qemu. The latest stable version is 0.3.0 (floppy image), while the current version is 0.5.0 (source).
Now, build the 0.5.0 source:
tar zxvf prex-0.5.0.tar.gz
cd prex-0.5.0/
./configure --target=i386-pc
make
It will generate an image file 'prexos' in the current directory. The image file is not bootable by qemu. In order to run it via qemu, we have to make use the available floppy image, and replace it with the latest version. Basically, the image file consists of bootsector.bin which is located in the first sector and the prexos image in the root directory. Meaning, we have to replace the prexos image from the latest stable 0.3.0 with the recently compiled 0.5.0.
Now, after extracting prex-0.3.0.i386-pc.img.gz, we get prex-0.3.0.i386-pc.img. As a root, we have to modify /etc/mtools.conf, such that we emulate the image as the DOS a drive:
kedit /etc/mtools.conf &
Replace
drive a: file="/dev/fd0" exclusive
with
drive a: file="[directory of the floppy image]/prex-0.3.0.i386-pc.img" exclusive
Now we replace the older 0.3.0 image with the new one using the mcopy command :
cd prex-0.5.0/
mcopy prexos a:

Long file name "prexos" already exists.
a)utorename A)utorename-all r)ename R)ename-all o)verwrite O)verwrite-all
s)kip S)kip-all q)uit (aArRoOsSq):
type o (overwrite).
Now, we're ready to boot the image with the latest kernel version.
qemu -fda prex-0.3.0.i386-pc.img
Here's a screenshot:

Andy Tanenbaum, Presentation and Interview

http://de.sevenload.com/videos/DqzIRi2/Andrew-Tanenbaum-Design-of-microkernel-OS
http://www.computerworld.com.au/index.php/id;925218285;fp;16;fpid;1
http://www.youtube.com/watch?v=gFUvDznGwHI

Sunday, October 14, 2007

Connecting to wireless network in NUS from your thinkpad

I have mentioned about wireless for T40 in linux here. Unfortunately, it doesn't inherently support WPA protocol. So, we have to install wpa_supplicant which support WPA and WPA2 (IEEE802.11i). The installation is available here and here. Basically, after building the source, which creates wpa_supplicant and wpa_cli with 'make' command, the binaries can be installed to /usr/local/bin.
Example commands:
make
cp wpa_cli wpa_supplicant /usr/local/bin
Then, we need to make a configuration file, /etc/wpa_supplicant.conf. The entry of the file as provided by http://opensource.nus.edu.sg/wiki/index.php/Connecting_to_PEAP_in_NUS and here:
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="NUS"
scan_ssid=1
key_mgmt=IEEE8021X
eap=PEAP
ca_cert=”/etc/cert/ase1.pem”
phase2=”auth=MSCHAPV2”
}
The content of ase1.pem is as such :
-----BEGIN CERTIFICATE-----
MIIDJzCCApCgAwIBAgIBATANBgkqhkiG9w0BAQQFADCBzjELMAkGA1UEBhMCWkEx
FTATBgNVBAgTDFdlc3Rlcm4gQ2FwZTESMBAGA1UEBxMJQ2FwZSBUb3duMR0wGwYD
VQQKExRUaGF3dGUgQ29uc3VsdGluZyBjYzEoMCYGA1UECxMfQ2VydGlmaWNhdGlv
biBTZXJ2aWNlcyBEaXZpc2lvbjEhMB8GA1UEAxMYVGhhd3RlIFByZW1pdW0gU2Vy
dmVyIENBMSgwJgYJKoZIhvcNAQkBFhlwcmVtaXVtLXNlcnZlckB0aGF3dGUuY29t
MB4XDTk2MDgwMTAwMDAwMFoXDTIwMTIzMTIzNTk1OVowgc4xCzAJBgNVBAYTAlpB
MRUwEwYDVQQIEwxXZXN0ZXJuIENhcGUxEjAQBgNVBAcTCUNhcGUgVG93bjEdMBsG
A1UEChMUVGhhd3RlIENvbnN1bHRpbmcgY2MxKDAmBgNVBAsTH0NlcnRpZmljYXRp
b24gU2VydmljZXMgRGl2aXNpb24xITAfBgNVBAMTGFRoYXd0ZSBQcmVtaXVtIFNl
cnZlciBDQTEoMCYGCSqGSIb3DQEJARYZcHJlbWl1bS1zZXJ2ZXJAdGhhd3RlLmNv
bTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0jY2aovXwlue2oFBYo847kkE
VdbQ7xwblRZH7xhINTpS9CtqBo87L+pW46+GjZ4X9560ZXUCTe/LCaIhUdib0GfQ
ug2SBhRz1JPLlyoAnFxODLz6FVL88kRu2hFKbgifLy3j+ao6hnO2RlNYyIkFvYMR
uHM/qgeN9EJN50CdHDcCAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG
9w0BAQQFAAOBgQAmSCwWwlj66BZ0DKqqX1Q/8tfJeGBeXm43YyJ3Nn6yF8Q0ufUI
hfzJATj/Tb7yFkJD57taRvvBxhEf8UqwKEbJw8RCfbz6q1lu1bdRiBHjpIUZa4JM
pAwSremkrj/xw0llmozFyD4lt5SZu5IycQfwhl7tUCemDaYj+bvLpgcUQg==
-----END CERTIFICATE-----
Then, run the wpa_supplicant using :
wpa_supplicant -i eth1 -c /etc/wpa_supplicant.conf -B
Might want to try using -Dwext or -Dipw (deprecated)
Start wpa_gui:
wpa_gui &
Obtain ip address using:
dhclient eth1
To connect to NUSOPEN (NUS open network), set the essid to NUSOPEN:
iwconfig eth1 essid "NUSOPEN"
then obtain the ip address using dhclient as before (or dhcpcd eth1).

More info are available here:
http://www.thinkwiki.org/wiki/Wpa_supplicant
http://ipw2100.sourceforge.net/
http://en.wikipedia.org/wiki/Wpa_supplicant
http://opensource.nus.edu.sg/wiki/index.php/Main_Page
http://linuxnus.org/

Thursday, October 11, 2007

Suspending your IBM Thinkpad T40

For this I had to upgrade the kernel to the latest, 2.6.23, since my previous kernel doesn't inherently support suspend feature unless I patch it with tuxonice. To check whether your kernel version support suspend:
cd /usr/src/linux
make xconfig
Under "Power management options (ACPI, APM)" tab, check whether "suspend to RAM and standby" exists. If it doesn't, I suppose suspend is not supported by your kernel.
After downloading the kernel and compiling it, by right, suspend should work.
echo -n "mem" > /sys/power/state
Press Fn button on your thinkpad to resume.
During the compilation process, typically we would want to do make oldconfig, and we should enable all the suspend related features whenever asked. Also we might want to enable ibm_acpi
I also needed to install initrd in order to mount root partition using ext3 modules:
cd /boot
mkinitrd -c -k 2.6.23-smp -m ext3 -f ext3 -r /dev/hda2
It will create /boot/initrd.tgz file, that we'll want to load it along with the kernel at boot time. Now edit the /etc/lilo.conf :
kedit /etc/lilo.conf
The linux part should look like this :
image = /boot/vmlinuz
initrd = /boot/initrd.gz
root = /dev/hda2
label = newLinux
read-only
image = /boot/vmlinuz-huge-smp-2.6.21.5-smp
root = /dev/hda2
label = oldLinux
read-only
Then, run lilo and reboot to check whether it works.
In order to automate the suspend process whenever we close laptop lid, we have to configure acpid. Excerpted from the link:
Basically, acpid just executes scripts residing in /etc/acpi/actions. Which script to launch at which event is configured in several files in /etc/acpi/events. All actions are documented in /var/log/acpid.
The event script needs to be created within /etc/acpi/events and can have any name you like. In this case we call it lid because it will trigger the lid event. Do # vi /etc/acpi/events/lid and make it look like this:
event=button/lid
action=/etc/acpi/actions/sleep.sh %e

The "event" line is a regular expression specifying the events we're interested in. You can determine what the event strings are from looking at /var/log/acpid after trying to suspend, close the lid, etc. . You can find information about the event strings ibm-acpi generates for certain keys at the Special Keys HOWTO.

The "action" line is the command to be executed when these events are dispatched. In this example we call the sleep.sh script residing in /etc/acpi/actions and pass the event description text using the %e placeholder.
Our example /etc/acpi/actions/sleep.sh script looks as follows:
#!/bin/sh

# if launched through a lid event and lid is open, do nothing
echo "$1" | grep "button/lid" && grep -q open /proc/acpi/button/lid/LID/state && exit 0

# remove USB 1.1 driver
rmmod uhci_hcd

# sync filesystem and clock
sync
/sbin/hwclock --systohc

# switch to console
FGCONSOLE=`fgconsole`
chvt 6
/usr/sbin/radeontool light off

# go to sleep
sleep 5 && echo -n "mem" > /sys/power/state

# readjust the clock (it might be off a bit after suspend)
/sbin/hwclock --adjust
/sbin/hwclock --hctosys

# reload USB 1.1 driver
modprobe uhci_hcd

# turn on the backlight and switch back to X
radeontool light on
chvt $FGCONSOLE
Then reboot, to restart the acpi daemon.

More information is available at :
http://www.thinkwiki.org/wiki/How_to_configure_acpid
http://ibm-acpi.sourceforge.net/README
http://www.thinkwiki.org/wiki/How_to_make_ACPI_work
http://www.thinkwiki.org/wiki/ThinkWiki
http://ibm-acpi.sourceforge.net/
http://www.linux.com/articles/54610

Slackware experience

To this day, I can still claim that I'm no expert in these areas, still a noob in his early stage. Nevertheless, here's my story.
Well, to begin with, my experience with Linux started when I installed Red Hat 6 in 1999. Back then, it was hardly useful for day-to-day use, and further, having no internet connection at home made the experience simply lackluster. During my undergraduate days, I had several encounters that in one way or another further developed my interest in Linux. I tried Gentoo on my PC which took me 2 days to install it. If I remember it correctly, the installer had to download every single source from the net, and then compiling each to a hardware specific package which made the installation process, well, very time consuming. Some other distros that I had tried are SUSE, Fedora, Debian, Slax, and Knoppix. I also used Solaris during industrial attachment and in my recent job. I was merely using it to run some ic design applications (Cadence, Synopsys, etc) and no more than that.
Here's the Slackware story. During the inter-sem project, I was doing a robotic project on environmental mapping. The idea of environmental mapping (SLAM) is simply to use the robot, which is RWI Magellan Pro in this case, to wander around the environment and then to make a map based on several features captured from the environment and on the same time to localize itself in the map. The RWI Magellan Pro had a pretty old hardware and Red Hat 6.2 installed in it with its own controller system, i.e. rFlex controller. Basically, we could access the robot devices and sensors through the library provided by rFlex controller. I spent couple of weeks to understand the system and also on SLAM. Then, I found this Player/Stage project which basically provides a simple interface to devices and sensors in the robot. The cool thing is, it acts as a client and server system that communicates over the IP network. When Player is running on the robot, the client control program could be run from other computer (a very fast system that could process algorithms in real time), and connects to it via a standard TCP socket. Another good thing is that Player/Stage could be run on many robotics system, thus code developed in one system can be run on others with very few modifications.
Problem is, Player/Stage needs several libraries that are not provided by Red Hat 6.2, and for some reasons which I can't really recall, I had to install the newer kernel for that. Anyway, I decided to install a newer version of Linux, and I chose Slackware 10.0 for this after trying several other distros like Red Hat 9, Debian, etc. I can't really remember why I ended up with Slackware. Based on the project report, Slackware was chosen due to its small size (the hard disk in Magellan Pro is kinda small) and robustness compared to other system, and more importantly the kernel version 2.4.26 which is compatible with Player/Stage. I think, I also had some problems installing Player/Stage on Red Hat 9. FYI, Magellan Pro doesn't have CD-ROM drive in it, so the installation was done via NFS. Anyway, for obvious reason, I tried to install Player/Stage on PC first in order to determine which Linux distribution to use.
The Slackware installation, as I remember, didn't go really smoothly, but anyway in the end the system worked as intended. Then, I installed (in no particular order): Player, Stage, Pmap, Lodo. Some time was then spent to install Sick LMS laser sensor which created much hassle, and by this time the 6-week project was near to its end and yet I haven't implemented any single code for the SLAM.
Apparently, Player/Stage has this Pmap package that is used for laser-based mapping, which made the system, or the project for this matter, complete with a few modification on the Pmap source code and calibration on the odometer. Also, I managed to transfer Matlab code on Kalman filter implementation of SLAM to Java that didn't really work on the robot (it somehow worked on Stage). According to my supervisor, the idea of Kalman filter is understandable to some extent however the implementation is rather tedious for my level. He also convinced me that I wouldn't be able to implement the algorithm on the robot at such stage (undergraduate) with very little time left.
Anyway, my supervisor was totally satisfied with my work (since previously the robot had lied there uselessly on the lab) and instead, decided to change the project name to fit the current work. He then offered me to continue the project for my final year project.
To his surprise, I decided not to continue the project as I thought I have done what was really need to be done. Anyway, in my humble opinion, the field of robotics and AI in Singapore is really way behind than that of other countries like Japan, Europe, or USA. While those local polytechnic students are still trying to find out how to win the robot soccer competition, our fellows in Japan have created a system that mimics human being, responds and interacts to us.
So, I then moved to IC design and electronics industry, which was really interesting back then, but no so now, hehe.
Well, to my understanding, I just have some mere surface knowledge on each field, that enables me to work in each industry but not enough for me to start my own company. IC design was really interesting, but I ended up doing analog design which was more to intuition than logic. I managed to produce two local conference papers, not of quality one though.
I guess, I really have to go back to the basic, which is programming, that has excite me since back then in junior high. Anyway, recently I found this operating system, Minix, which actually inspired the Linux to the its existence now. While Linux kernel is based on monolithic kernel, the newer version of Minix (Minix 3) is based on micro kernel, which is more elegant and reliable. I decided to learn more on it, and just borrowed a book from the library: "Operating Systems: Design and Implementation". Hopefully, I could finish it by the end of the year.
Back to Slackware, I'm still using it up to now on my Thinkpad T40 laptop. Hopefully, it thrives. Well, if it is not, then I hope Minix will thrive and become the most popular OS, which is rather impossible here due to the concept of inertia, as explained by Prof. Tanenbaum.

Monday, October 8, 2007

NTFS on Linux

To read and to write to NTFS file system in Linux is possible via ntfsmount driver. After installing FUSE and ntfsprogs, create these symbolic links:
ln -s /usr/local/lib/libntfs.so.10 libntfs.so.10
ln -s /usr/local/lib/libfuse.so.2 libfuse.so.2
Then in /etc/fstab, add:
/dev/hda1 /mnt/windows fuse.ntfs rw 1 0 locale=be_BY.UTF-8 0 0

Friday, October 5, 2007

Changing window manager in KDE

By setting environment variables KDEWM to the specific window manager, we could use that window manager under KDE. Example here is to set the window manager to blackbox
setenv KDEWM blackbox

Thursday, October 4, 2007

Slackware 12.0 changelogs

The changelogs of Slackware 12.0 (stable) is available here. Packages are available here.

Tuesday, October 2, 2007

Mplayer and Sopcast in Slackware 12.0

From the website, SopCast is a simple, free way to broadcast video and audio or watch the video and listen to radio on the Internet by adopting P2P technology. It needs media player such as vlc or mplayer on top of it. Basically, from my understanding, it only provides streams of data in a certain format (i.e. wmv3) that is readable, and thus viewable, by the media players. Since vlc doesn't support wmv3 format, we'll have to rely on mplayer for this particular case.
I downloaded the Slackware package for mplayer from here.
Then, I downloaded these 2 files for SopCast:
http://download.sopcast.com/download/sp-auth.tgz
http://www.sopcast.com/download/libstdcpp5.tgz
From the readme file :
SopCast client version 1.0.2 library dependency
If you don't have stdc++ 5 in your system, please download the libstdcpp5.tgz from
www.sopcast.com, and copy the
libstdc++.so.5
libstdc++.so.5.0.1
to /usr/lib/

The copy command must be:
cp -a libstdc++.so.5* /usr/lib
With '-a' parameter, and you must login as root.

1. sp-sc-auth

A simple example of sp-sc command line.
./sp-sc-auth sop://broker.sopcast.com:3912/6098 3908 8908 > /dev/null &
Start to transfer channel 6098, and you can play it on 8908 with VLC or mplayer
by open the url: http://localhost:8908/tv.asf
Then, from konsole:
mplayer -zoom http://localhost:8908/tv.asf
What's interesting from the Linux version of vlc is that here we could stream several channels at the same time, which is not possible in that of Windows XP's.
From konsole:
Start streaming channel 1(Channel ID:6001, Port: 8908)
./sp-sc-auth sop://broker.sopcast.com:3912/6001 3908 8908 > /dev/null &
Show the channel at mplayer:
mplayer http://localhost:8908/tv.asf
Start streaming channel 2:(Channel ID:6002, Port 8909)
./sp-sc-auth sop://broker.sopcast.com:3912/6002 3908 8909 > /dev/null &
Show the channel at mplayer: (Note, now the TCP port is 8909)
mplayer http://localhost:8909/tv.asf
etc

Python IDLE in Linux

A bash script to start Python IDLE in Linux :
#!/bin/bash
cd /usr/lib/python2.5/idlelib/
python PyShell.py &
cd ~

slackbuilds.org

I've found this website : www.slackbuilds.org that provides packages for Slackware, to be compiled with the SlackBuild scripts. Usage of the slackbuild script can be found here. The resulting Slackware official package is then to be installed with installpkg command. Typically, the resulting package will be in /tmp.

Installation of openoffice.org
First download the slackbuild script here.
Untar it.
Download the source from here.
Copy the source the the slackbuild directory:
cp OOo_2.3.0_LinuxIntel_install_en-US.tar.gz openoffice.org
Make the script executable:
chmod +x openoffice.org.SlackBuild
./openoffice.org.SlackBuild
When done creating the package, install it with installpkg:
cd /tmp
installpkg openoffice.org-2.3.0_en_US-i586-1_SBo.tgz

Monday, October 1, 2007

Important Stuffs in Setting Up Your Slackware

USB Flash Drive
modprobe usb-storage
cd /mnt
mkdir usbflash
nano /etc/fstab
add: /dev/sda1 /mnt/usbflash vfat,ntfs noauto,owner,user,umask=0 0 0
The sda1 represents the device name that the kernel gives the USB flash when it gets plugged in.
mount /mnt/usbflash
Slocate: It provides a secure way to index and quickly search for all files on
your system regardless of ownership.
slocate -u
slocate
Wireless LAN:
Based on /sbin/lspci, the driver for the system (IBM Thinkpad T40) is Intel Corporation PRO/Wireless LAN 2100 3B Mini PCI Adapter. After searching the net, I've got these results: http://ipw2100.sourceforge.net/, http://jrblevin.freeshell.org/linux/ipw2100/, http://ipw2100.sourceforge.net/. According to the installation manual , starting with Linux kernel version 2.6.14, an old stable version of the Intel PRO/Wireless 2100 Network Connection driver is provided in the kernel. All we need to do is to download the firmware from http://ipw2100.sf.net/firmware.php , untar it, and copy all the files to /lib/firmware. The specific files are :
ipw2100-(version number).fw Boot strap image
ipw2100-(version number)-i.fw Used in IBSS mode
ipw2100-(version number)-p.fw Used in monitor mode
Load the module with:
modprobe ipw2100
check the output of dmesg to see if it is successful:
dmesg | tail
Slackpkg: is a tool to install or upgrade Slackware official packages from a mirror.


Firefox plugins

Here are the necessary plugins as add-ons on firefox (https://addons.mozilla.org/en-US/firefox/browse/type:7)
1. Adobe Flash Player
Download the tarball
tar zxvf install_flash_player_9_linux.tar.gz
cd install_flash_player_9_linux/
cp libflashplayer.so ~/usr/lib/mozilla/plugins/
cp flashplayer.xpt ~/usr/lib/firefox-2.0.0.4/components/
2. Adobe Reader
Download the tarball
tar zxvf AdobeReader_enu-7.0.9-1.i386.tar.gz
cd AdobeReader
./INSTALL
all the files are installed at /usr/local/Adobe/Acrobat7.0/
acroread
expr: syntax error
message shows, to work it out :
nano /usr/local/Adobe/Acrobat7.0/
comment the line : check_gtk_ver_and_set_lib_path "$MIN_GTK_VERSION"
i.e. # check_gtk_ver_and_set_lib_path "$MIN_GTK_VERSION"
3. JRE
The Slackware package can be found from the slackware stable repository
http://plugindoc.mozdev.org/faqs/java.html#Linux
ln -s /usr/lib/jre1.6.0_02/plugin/i386/ns7/libjavaplugin_oji.so /usr/lib/mozilla/libjavaplugin_oji.so



Slackware 12.0 installation

These are the steps encountered during the installation of Slackware 12.0 in my IBM T40 system.
1. When booting from CD1, press enter after boot: appears.
2. Login as root.
3. cfdisk, this will create the partition layout of the hardisk. Set it as below:
Name Flags Type Fs type Size
hda1 Boot Pri NTFS 29417.48
hda2 Pri Linux 6967.30
hda3 Pri Linux swap 495.446
4. Type setup.
5. Addswap
6. Check swap partition for bad blocks -> No
7. Swap space configured -> OK
8. Select Linux installation partition: /dev/hda2 Linux
9. Format partition /dev/hda2 -> Yes
10. Select filesystem for /dev/hda2 : ext3
11. FAT or NTFS partitions detected, add to /etc/fstab -> Tes
12. Select partition to add to /etc/fstab: /dev/hda1
13. Pick mount point for /dev/hda1 : /mnt/windows
14. Media selection : slackware CD or DVD
15. Package series selection
16. Select prompting mode : full
17. Make USB flash boot : skip
18. Modem configuration : /dev/ttys3
19. Installation of LILO : simple
1024x768x256
LILO destination MBR
20. Network config: DHCP
21. Confirm starting services to run
22. Hardware clock set to UTC : no
23. Timezone config : Singapore
24. Default window manager : KDE
25. Set root passwrd: **
26. We're done with the setup, reboot
27. startx, to start the x environment

Adding user is performed under KDE environment, KUser:
-Login shell: /bin/tcsh

From konsole: nano /etc/sudoers, then add the line :
username ALL=(ALL) NOPASSWD: ALL

From konsole: /sbin/lspice:
00:00.0 Host bridge: Intel Corporation 82855PM Processor to I/O Controller (rev 03)
00:01.0 PCI bridge: Intel Corporation 82855PM Processor to AGP Controller (rev 03)
00:1d.0 USB Controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #1 (rev 01)
00:1d.1 USB Controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #2 (rev 01)
00:1d.2 USB Controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #3 (rev 01)
00:1d.7 USB Controller: Intel Corporation 82801DB/DBM (ICH4/ICH4-M) USB2 EHCI Controller (rev 01)
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev 81)
00:1f.0 ISA bridge: Intel Corporation 82801DBM (ICH4-M) LPC Interface Bridge (rev 01)
00:1f.1 IDE interface: Intel Corporation 82801DBM (ICH4-M) IDE Controller (rev 01)
00:1f.3 SMBus: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) SMBus Controller (rev 01)
00:1f.5 Multimedia audio controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) AC'97 Audio Controller (rev 01)
00:1f.6 Modem: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) AC'97 Modem Controller (rev 01)
01:00.0 VGA compatible controller: ATI Technologies Inc Radeon Mobility M7 LW [Radeon Mobility 7500]
02:00.0 CardBus bridge: Texas Instruments PCI1520 PC card Cardbus Controller (rev 01)
02:00.1 CardBus bridge: Texas Instruments PCI1520 PC card Cardbus Controller (rev 01)
02:02.0 Network controller: Intel Corporation PRO/Wireless LAN 2100 3B Mini PCI Adapter (rev 04)
02:08.0 Ethernet controller: Intel Corporation 82801DB PRO/100 VE (MOB) Ethernet Controller (rev 81)

Friday, September 28, 2007

Slackware 12.0



The newest Slackware version 12.0 could be downloaded from ftp://swtsrv.informatik.uni-mannheim.de/pub/linux/distributions/slackware/slackware-12.0-iso/
It comes with the latest kernel version 2.6.
http://www.slackware.com/announce/12.0.php for more information.
The 7 Most Influential GNU/Linux Distributions

Slackware has been my all time favorite distro. In my opinion, it's simple yet elegant. I can't say much about it though, as I have only tried a few of other distros (Fedora, SUSE, Gentoo).

Thursday, September 27, 2007

Options pricing based on Monte Carlo methods

Here is a python code to analyze options price based on Monte Carlo simulations.
From wikipedia: Monte Carlo methods are useful in many areas of computational mathematics, where a lucky choice can find the correct result. Interesting, huh !!
http://en.wikipedia.org/wiki/Monte_Carlo_methods_in_finance
All the formula in the code are taken from FE5102 notes, chapter 5 on Statistical Methods.

import random
import math

price = 33
exercise = 30
expiry = 6.0
vol = 9.0/100
rate = 5.0/100
simNum = 5000000

mean = math.log (price) + (rate - vol * vol / 2) * expiry / 12.0
stddev = math.sqrt(vol * vol * expiry / 12.0)

sum = 0
for i in range (simNum):
ft = math.exp(random.normalvariate(mean,stddev)) - exercise
if (ft <= 0): ft = 0 sum = sum + ft average = sum / simNum optionsPrice = average * math.exp(-rate*expiry/12.0) print "The options price based on " + str(simNum) + " observations is " + str(optionsPrice)