NaN Quantitavity – Quant Trading, Statistical Learning, Coding and Brainstorming

2013-03-20, 10:04 AM, Wednesday

How many times are expected to try all songs in your itune library?

Filed under: R, Statistics — weekendsunny @ 10:04 AM

Today, when I was very boring with all my songs in iphone libary, I suddenly thought about one quick but useful math puzzle: assuming the iphone library has 100 songs and you use random shuffle (with re-sample) for next song, how many times are expected to listen all these songs?

This is very similar as the classical Coupon Collection problem. Here is a quick dirty R simulations, with two plots in regular scale and log scale:

N.songs = 100
temp <- function(N.songs){
return ( sum(N.songs/(1:N.songs)) )
}

x <- 1:N.songs
y <- sapply(x,temp)

par(mfrow=c(1,2))
plot(x,y,type=’l’,col=3, lty=2, lwd=2,log =’x’, xlab=’Number of New Songs’, ylab = “# Trials Needed”)
plot(x,y,type=’l’,col=3, lty=2, lwd=2, xlab=’Number of New Songs’, ylab = “# Trials Needed”)

songs

2013-03-16, 2:19 PM, Saturday

Python First Try

Filed under: Python — weekendsunny @ 2:19 PM

import pandas as pd
import pandas.io.sql as pd_sql
import sqlite3 as sql
import os

path = “C:\\test”
os.chdir(path)

DF = pd.read_csv(‘spy2011.csv’)
DF.head(10)
con = sql.connect(‘data.db’)
pd_sql.write_frame(DF, “tbl”, con)
con.commit()

df = pd.DataFrame({‘label’: [‘A’, ‘B’, ‘C’] + [‘B’] * 2 + [‘A’] * 3, ‘value’: [4, 3, 6, 3, 1, 2, 4, 4]})
df2 = df.sort([‘label’, ‘value’])
df3 = df2.drop_duplicates([‘label’])

2013-03-13, 10:31 PM, Wednesday

Random Permutation 2 – Random sample m out of n

Filed under: C++, Java — weekendsunny @ 10:31 PM
int[] pickMRandomly(int[] array, int m) {
    int[] subset = new int[m];
    for (int j = 0; j < m; j++) {
        int index = random(j, n); // random number between j and n
        int temp = array[index];
        array[index] = array[j];
        array[j] = temp;
        subset[j] = temp;
    }
    return subset;
}

Random Permutation 1 – shuffle

Filed under: C++, Java — weekendsunny @ 10:29 PM
public static void shuffleArray(int[] array) {
    Random generator = new Random();
    for (int j = 0; j < array.length; j++) {
    int index = generator.nextInt(array.length-j)+j; // random number between j and n
    int temp = array[index];
    array[index] = array[j];
    array[j] = temp;
    }
}

Create a free website or blog at WordPress.com.

Design a site like this with WordPress.com
Get started