Created
February 6, 2014 07:13
-
-
Save yosimox/8839594 to your computer and use it in GitHub Desktop.
ランダムウォークによる広告アトリビューション予測モデル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'pp' | |
class RandomWalk | |
def initialize(header, data, input) | |
@header = header | |
@data = data | |
@input = input | |
@loopmax = 10 | |
@cv_index = header.index("cv") | |
@ridatsu_index = header.index("ridatsu") | |
end | |
def start | |
@data = change_to_float(@data) | |
@share_matrix = tr_share(@data) | |
simulation() | |
end | |
def simulation | |
calc_data = @input | |
cv_data = [] | |
ridatasu_data = [] | |
0.upto(@loopmax) do |i| | |
res = step(calc_data) | |
cv_data << res[@cv_index] | |
ridatasu_data << res[@ridatsu_index] | |
calc_data = res[0...@cv_index] | |
#CV数が1件以下になったら、ループ終了 | |
break if res[@cv_index] < 1 | |
#pp cv_data | |
#pp ridatasu_data | |
#pp calc_data | |
end | |
pp cv_data | |
puts cv_data.sum() | |
end | |
def step(calc_data) | |
res = [] | |
@share_matrix.each_with_index do |row,i| | |
#pp row.inject(0){|a,b| a += b * 1000} | |
arr = calc_data.map.with_index{|y, j| | |
row[j] * y | |
} | |
res << arr.sum | |
end | |
return res | |
end | |
####### Pre Processing ########### | |
#change to integer from file data | |
def change_to_float(arr) | |
res_arr = [] | |
arr.each do |x| | |
res_arr << x.map{|y| y.to_f} | |
end | |
return res_arr | |
end | |
#make share matrix transposed | |
def tr_share(data) | |
arr = [] | |
matrix = data[0...4] | |
matrix.each do |x| | |
if (x.sum != 0) | |
arr << x.map{|y| y / x.sum} | |
end | |
end | |
return arr.transpose | |
end | |
end | |
#Array拡張 | |
class Array | |
def sum | |
s = 0.0 | |
n = 0 | |
self.each do |v| | |
next if v.nil? | |
s += v.to_f | |
n += 1 | |
end | |
return s | |
end | |
end | |
header = ["ad","sem","org","mail","cv","ridatsu"] | |
data = [ | |
[2,64,300,31,60,675], | |
[2,48,74,61,10,219], | |
[300,19,4,4,83,971], | |
[32,51,94,3,0,265] | |
] | |
#入力データ | |
#input = [1000,1000,0,0] | |
input = [2000,0,0,0] | |
r = RandomWalk.new(header, data, input) | |
r.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment