# Category Archives: Uncategorized

## Monty Hall problem and frog riddle

Posted on

Probability topic is the fundamental concept of the statistics. And machine learning is closely related to statistics. That is why, understand the probability very important if you are doing research, statistics, and machine learning.

Monty Hall is a very interesting problem. It says, if you are given 3 doors to choose. One of them contains a car (which you want), the other two are goats (which you don’t want). After you made your choice, before opening the door, the host will open the door that you didn’t choose yet contains the goat (he knows which door has the goat). Now, if you are given an opportunity to change your choice to another door (which you didn’t choose earlier), are you going to change?

In the first glance, you will feel that whatever you choose, the probability is always 1/3. However, the conditional probability tells you that, if you always make the switch after the host opened the door that has a goat, your probability to win the car will increase to 2/3. What??

In order to prove this, I wrote a Python script.

#!/usr/bin/env python
# This is simulating Monty Hall Paradox

import random

def monty(switch):
# random shuffle
doors = [0, 0, 1]  # one of the door contains the car
random.shuffle(doors)

openDoor = None

# choose the first door (not open)
# if the first door is 1, randomly open the other
if doors[0] == 1:
# open the door
openDoor = random.randint(1, 2)
else:  # open the door that contains goat
if doors[1] == 1:
openDoor = 2
else:
openDoor = 1

# now open the last door
if not switch:
return doors[0]
else:
if openDoor == 2:
return doors[1]
else:
return doors[2]

def main():
total = 10000
car = 0
for i in range(total):
car += monty(True)

print("Always switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total))

car = 0
for i in range(total):
car += monty(False)

print("No switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total))

main()


Run the code, you will always get the probabilty close to 0.6667 if you always switch the door.

Always switch the door. Total: 10000, car: 6625. P = 0.6625
No switch the door. Total: 10000, car: 3309. P = 0.3309


### Frog riddle

It also mentions about the conditional probability. Interestingly, quite a lot of comments mentioned that the author is wrong.

In order to prove that the author is correct, I wrote another Python script.

#!/usr/bin/env python

import random

# Frog 0 for female, 1 for male

def create_frog():
return random.randint(0, 1)

def has_croak(pairs):  # also male
return 1 in pairs

def has_female(frogs):
return 0 in frogs

def choose_without_croak(choose_two):
frogs = [create_frog() for i in range(3)]
# first frog at the right side
# second and third at the left side

if choose_two:
return has_female(frogs[1:])  # choose two frogs

return has_female(frogs[0:1])

def main():
total = 10000
correct = 0
for i in range(total):
correct += choose_without_croak(True)
print('Just choose two frogs. Total: {}, correct: {}. P = {}'.format(total, correct, correct / total))

correct = 0
for i in range(total):
correct += choose_without_croak(False)
print('Just choose one frog. Total: {}, correct: {}. P = {}'.format(total, correct, correct / total))

# The exact question is,
# "What is the probability of the frogs in the pair has female,
# given that one of them is male?"
def exact_calculation():
total = 10000
croak = 0
correct = 0
for i in range(total):
frogs = [create_frog() for i in range(3)]
if has_croak(frogs[1:]):
croak += 1
if has_female(frogs[1:]):
correct += 1
print('Total croak: {}, correct: {}. P = {}'.format(croak, correct, correct / croak))

main()
exact_calculation()


Running the script, you will get

Just choose two frogs. Total: 10000, correct: 7498. P = 0.7498
Just choose one frog. Total: 10000, correct: 4974. P = 0.4974
Total croak: 7474, correct: 4998. P = 0.6687182231736687


Based on the result, if you choose two frogs, the probability of survive is close to 0.75. If you choose one frog, the probability is 0.5.

Now, the tricky part is the probability 0.67 mentioned in the video. The question should be “What is the probability of the frogs in the pair has female, given that one of them is male?”

So, based on the question, my similuation needs to get the total count of the male (that has croak), and within these pairs, count the female frogs.

To convert this into mathematical expression,

$P(\text{female frog}) = 0.5$

$P(\text{at least one male frog}) = 0.75$

$P(\text{female frog} | \text{at least one male frog}) = \frac{0.5}{0.75} = 0.6667$

Then, based on the simulation and calculation, you will get the 0.6667.

Posted on

## Prelude

Last weekend was a long story, that I my Xiaomi Mi 3 is screwed! It happened like this.

I was in the car, using the phone to surf net and go through the social networks. Then suddenly, it popped up a notification saying that my phone is too hot, asked me to cool down, with a system button “Cool Down”. I cannot guarantee that this is not a virus, but I believe that my phone doesn’t have virus. (I can hardly find the Cool Down feature online, this is the only one.) So, I pressed the Cool Down button. With this press, sorry my phone. You got a stroke, and never overcome. Bye-bye!

## Recovering

Because of that one press, my phone fell into the bootloop. When I went home, I searched for some possible solutions online. And found that, the possible solution is to flash the stock ROM. I downloaded everything I needed. Moreover, the most important thing is to get all my data especially photos, which I didn’t make any backup recently.

So, I followed the steps in this wonderful page for how to perform a flash. WARNING: This will flash everything, meaning, you will loss your data, including everything in the internal storage, such as photos. So, before I flash the stock ROM, I followed this forum to learn how to restore the data.

Summary:

1. Get all the necessary drivers and software. Actually can be found in the official site. Important software are: MiFlash. It also requires drivers which can be found here. So that you can use “fastboot” command (and also “adb” command) and detect the phone when USB connected.
2. Get the stock ROM and recovery ROM. I used TWRP.
3. Boot into fastboot mode (read the instruction from the “wonderful page” mentioned above).
4. Use command-line “fastboot” to flash the recovery ROM TWRP. And boot into the recovery mode.
5. Once successfully boot, your computer can detect the phone (as MTP), then you can access the internal storage and copy all the files.

The luckiness within the unluckiness (不幸中的大幸), I managed to boot into recovery mode. Immediately copied everything from the phone to the computer. After this, I tried to flash the stock ROM. But failed. Tried various times, but none of them work. Even wanted to boot into the TWRP recovery mode was very difficult. But luckily, I got all the photos from the phone before these.

Searched for the solution again, I found this forum. Probably my battery got serious problem.

Yet, because my phone was bought through the mobile phone shop, not from Xiaomi official website. The phone is actually from Taiwan. And there is no nearest Xiaomi service centre at my area (and never will be). So, I can only say “bye-bye” to my phone. It is the time to “let it go”.

## Epilogue

Most important thing is data. As long as you have the data, put it in any phone, then that is your phone. Just like your soul and spirit resides in any body, make it you yourself!

## Arch Linux update issue

Posted on

Today, when I updated Arch Linux as usual, then suddenly it brought me to the TTY, which didn’t allow me to change to other TTY (due to NVIDIA graphic card issue which has been attached with my laptop for quite a long time.)

Because I could do nothing, and not sure whether the upgrade was completed, so I made a hard reset.

Then I boot with fallback initrmfs just in case has any error. Luckily, there was no problem to boot.

Then I did a quick check on the database. And found that it was locked. Meaning, I had restarted without finishing the update. This is serious if it happened when upgrading the kernel.

Reading the /var/log/pacman.log, I found that some packages were not updated. Then I just simply did a pacman command, and I got these message,

ldconfig: File /usr/lib/libqca.so is empty, not checked.
ldconfig: File /usr/lib/libQt5QuickTest.so is empty, not checked.
ldconfig: File /usr/lib/libQt5QuickParticles.so is empty, not checked.
...


Read the forum, the solution to re-install the packages that own these files. So I used pkgfile to search fo the owner of these files. Then, re-installed each packages found.

Then I just made sure all the packages were installed properly with

pacman -Dk #previously is testdb


## Social media

Posted on

Different social media usage:

Facebook – Family and friends status.

Google+ – Official tech news or blogs.

Weibo – China celebrities personal status.

Instagram – Celebrities’ photos.

Reddit – Community and specific topic discussion.

WordPress – Nothing but blog posts.

## Migrating Arch Linux from 32-bit to 64-bit

Posted on

These days, I decided to migrate my Arch Linux from 32-bit to 64-bit. There are several reasons that make me to make such decision.

Firstly, in Arch Linux official site, there was an announcement that Arch Linux will drop 32-bit. And in the wiki page, it mentioned that Arch Linux user should use 64-bit if the processor supports.

Secondly, new distros such as KaOS and Evolve OS only support one architecture 64-bit. That means 32-bit is considered less targeted.

Thirdly, my favourite software FreeFileSync has only pre-built 64-bit binary package, though it can be built on 32-bit from source. Moreover, Opera on Linux only active on 64-bit. The current version is 27.0, yet 32-bit is still version 12.16. That means 32-bit OS users do not have the chance to use the latest Opera.

Next, Docker which is officially supported by Arch Linux, is 64-bit only. However, 32-bit can be installed through AUR. I tried Docker 32-bit and pull an Ubuntu image from Docker repository, at the end the image is 64-bit. As a result, the Ubuntu image does not work on my Docker container. And most of the official images on Docker repository are 64-bit, including Fedora. That is why when I was testing Docker on my 32-bit Linux, I had to use “debootstrap” to get the 32-bit Ubuntu then imported to Docker.

With these reasons and the trend, sooner or later, 32-bit software will less likely be supported. And migrating to 64-bit sooner or later must happen.

### The way to 64-bit

Arch Linux wiki page offers some methods to do the migration between two architecture, 32-bit to 64-bit or vice versa. I did not use the methods as mentioned, due to the limitation of disk space in partitions. So, I have to use other method.

Since I have two laptops: personal and working. The working laptop is my primary laptop, which is always updated to the latest packages. On the other hand, the personal laptop is old but with good graphic card, Nvidia. In order to do migration, I tried to migrate the personal laptop first, to check whether my method works or not. So that when migrating the working laptop, everything should go smooth.

Firstly, I downloaded the latest Arch Linux installation live media, then created a 64-bit virtual machine using VirtualBox. Then installed Arch Linux 64-bit on the virtual machine. Besides that, I also installed all the packages which are explicitly installed on my working laptop.

Then the next thing I did was retrieving the /var, /opt, and /usr directories from the virtual machine. The reason was that these directories contain binary files to run the OS, and I wanted to preserve all the configuration in /etc, so that less configuration to do after installation. On the other hand, /bin, /lib, /lib64 are all symbolic links only. So, I ignore them, I can make the symbolic links myself.

Then, I used SystemRescueCD to delete all the files in /var, /opt, and /usr and replaced with the files retrieved from virtual machine. Then reboot using Arch Linux installation live media, mount the partition, arch-chroot, and mkinitcpio to generate the initramfs. Then reboot, but failed to load lxdm. After several tries, I concluded that my hypothesis failed. As a result, I decided to re-install everything, but backup /etc so that can refer it later.

Arch Linux is good, but there is a drawback, that is, installation requires Internet connection. Because to install, there is a script called “pacstrap” which will download the necessary packages and install to the partition. The “base” packages are around 150 MB. That means, if we have limited Internet quota, slow Internet, or no Internet, then there is a serious problem. So far, I have not look for any better solution for this.

However, all the other packages such as LibreOffice, Xfce, Firefox, and others, I had downloaded through VirtualBox. I just copied these cache to the /var directory, then installed the packages that I need. As they were cached, most of the packages needed not to be downloaded again. This reduces a lot of time and these packages can be used in the next OS installation.

Finally, /home directory preserved and it works fine. Just the /etc configuration has to be done manually, such as Apache and PHP configuration.

So, using the same method, I successfully installed Arch Linux 64-bit on my working laptop. Yet, there are some more configurations needed.

## YouTube automatic captions to .srt subtitle format

Posted on

However, automatic captions is different. It is created by YouTube based on speech recognition, thus the captions are not very accurate. But I personally feel that it may be a little useful. Therefore, I have done some scripting to solve the problem semi-manually. Semi-manual is because the preparation of the subtitle have to do it manually. I do not spend time to write a userscript to solve it.

In order to convert the automatic captions into the .srt,

1. Go to the YouTube page of our interested video.
2. Click the “Transcript” icon which is besides “About”, “Share”, and “Add to”. This will show a frame of English (automatic captions). Now we are going to copy all the text in the frame.
3. Using web browser’s Inspector by right-click. Then choose the parent HTML element of these captions, then right-click the element to “Copy Inner HTML” and save to a plain text file as HTML file format.
4. Open the HTML file format with the web browser, this will show the time and subtitles for every two lines. Copy these text to another plain text file.
5. Finally, use the Perl script below to convert the plain text file.
#!/usr/bin/perl
# Download the auto generated caption (English) from the internet, convert to the text.
# Then this script is to convert the text into the .srt format
use strict;
use warnings;

my $file =$ARGV[0];

my @time,my @subtitles;

open(FILE,$file); while(<FILE>) { my$line = $_;$line =~ s/^\s+|\s+$//g; if($line =~ /^(\d+:\d+)/) { #Updated (thanks to Daniel)
push @time,$1; } elsif($line =~ /(.+)/) {
if(length($1)) { push @subtitles,$1;
}
}
}
close(FILE);

for(my $i=0;$i<@subtitles-1;$i++) { print "00:$time[$i],000 --> 00:$time[$i+1],000\n"; print$subtitles[$i],"\n\n"; } my$next = $time[@subtitles-1]; if($next =~ /((\d+):(\d+))/) {
my $temp =$3+5;
$next = "$2:$temp"; } print "00:$time[@subtitles-1],000 --> 00:$next,000\n"; print$subtitles[@subtitles-1],"\n";


The steps 1-4 are done manually. It is possible to convert the above steps using JavaScript (userscript). But it is too time consuming for me.

## 2010 in review

Posted on

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

The Blog-Health-o-Meter™ reads This blog is on fire!.

## Crunchy numbers

A Boeing 747-400 passenger jet can hold 416 passengers. This blog was viewed about 2,100 times in 2010. That’s about 5 full 747s.

In 2010, there were 55 new posts, growing the total archive of this blog to 216 posts. There were 10 pictures uploaded, taking up a total of 735kb. That’s about a picture per month.

The busiest day of the year was August 3rd with 41 views. The most popular post that day was Installing CVS on Ubuntu 10.04.

## Where did they come from?

Some visitors came searching, mostly for ubuntu 10.04 cvs, cvs ubuntu 10.04, ubuntu 10.10 hp mini, install cvs ubuntu 10.04, and ubuntu 10.10 hp mini wireless.

## Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

Installing CVS on Ubuntu 10.04 June 2010
1 comment

2

Upgrade to Ubuntu 10.10 on HP Mini October 2010

3

Huawei E1552 on Ubuntu August 2010