In [1]:
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

%matplotlib inline

In [2]:
learning_rate = 0.001

In [3]:
data = np.genfromtxt('cust.csv', delimiter=',')

In [4]:
x_data,  tx_data, T_data = data[:, 0], data[:, 1], data[:, 2]
print x_data.shape


(2357,)

In [5]:
r = tf.Variable(tf.random_uniform(shape=[1]), dtype=tf.float32, name='r')
alpha = tf.Variable(tf.random_uniform(shape=[1]), dtype=tf.float32, name='alpha')
a = tf.Variable(tf.random_uniform(shape=[1]), dtype=tf.float32, name='a')
b = tf.Variable(tf.random_uniform(shape=[1]), dtype=tf.float32, name='b')
# r = tf.Variable([0.243], dtype=tf.float32, name='r')
# alpha = tf.Variable([4.414], dtype=tf.float32, name='alpha')
# a = tf.Variable([0.79], dtype=tf.float32, name='a')
# b = tf.Variable([2.426], dtype=tf.float32, name='b')


x = tf.placeholder(shape=[None], dtype=tf.float32, name='x')
tx = tf.placeholder(shape=[None], dtype=tf.float32, name='tx')
T = tf.placeholder(shape=[None], dtype=tf.float32, name='T')

In [6]:
sess = tf.Session()

In [7]:
lA1 = tf.lgamma(r + x) + r*tf.log(alpha) - tf.lgamma(r) - tf.lgamma(a) - tf.lgamma(b) -tf.lgamma(a + b +x)
lA2 = tf.lgamma(a) + tf.lgamma(b + x) - (r + x)*tf.log(alpha + T)
lA3 = tf.lgamma(a + 1) + tf.lgamma(b + x -1) - (r + x)*tf.log(alpha + tx)

LLA = - tf.reduce_sum(lA1 + tf.log(tf.exp(lA2) + tf.where(x > 0, tf.exp(lA3), tf.zeros_like(x))))
Lloss = tf.reduce_sum(lA1 + tf.log(tf.exp(lA2) + tf.where(x > 0, tf.exp(lA3), tf.zeros_like(x))))

In [8]:
init = tf.global_variables_initializer()
sess.run(init)
my_opt = tf.train.GradientDescentOptimizer(learning_rate)
train_step = my_opt.minimize(LLA)

In [9]:
for i in range(5000):
    ind = np.random.choice(len(x_data), size=100)
    st, loss = sess.run([train_step, LLA ], feed_dict={x:x_data[ind], tx:tx_data[ind], T:T_data[ind]})
    ll = sess.run(Lloss, feed_dict={x:x_data, tx:tx_data, T:T_data})
    k1, k2, k3, k4 = sess.run([r, alpha, a, b])
    if (i+1)%100 ==0:
        print '--iter %d-- '%(i+1), ll, k1, k2, k3, k4


--iter 100--  -9367.25 [ 0.14583161] [ 1.580387] [ 0.48181394] [ 0.98363101]
--iter 200--  -9345.39 [ 0.17790435] [ 1.89526033] [ 0.4474214] [ 1.02554822]
--iter 300--  -9338.25 [ 0.19533697] [ 2.13579202] [ 0.43017304] [ 1.03136337]
--iter 400--  -9329.55 [ 0.19555435] [ 2.31555557] [ 0.4256216] [ 1.03503537]
--iter 500--  -9352.44 [ 0.23879784] [ 2.45416951] [ 0.45906088] [ 1.00318038]
--iter 600--  -9337.34 [ 0.2326459] [ 2.59937906] [ 0.44735906] [ 1.02127194]
--iter 700--  -9330.92 [ 0.23137787] [ 2.70656514] [ 0.43118662] [ 1.03292549]
--iter 800--  -9315.68 [ 0.19005829] [ 2.8085196] [ 0.42354617] [ 1.04168892]
--iter 900--  -9313.22 [ 0.20626014] [ 2.89982319] [ 0.44312689] [ 1.03020024]
--iter 1000--  -9326.25 [ 0.17019039] [ 2.98749948] [ 0.4264186] [ 1.04495919]
--iter 1100--  -9310.71 [ 0.21373698] [ 3.06862926] [ 0.42749614] [ 1.03599238]
--iter 1200--  -9317.14 [ 0.2371749] [ 3.15094304] [ 0.43141523] [ 1.02314758]
--iter 1300--  -9313.49 [ 0.18903172] [ 3.22969842] [ 0.43062642] [ 1.03474438]
--iter 1400--  -9311.48 [ 0.19348183] [ 3.30381703] [ 0.43599328] [ 1.03924775]
--iter 1500--  -9349.88 [ 0.28603274] [ 3.36454964] [ 0.42291161] [ 1.04144883]
--iter 1600--  -9319.92 [ 0.25633535] [ 3.43564916] [ 0.4251762] [ 1.04473722]
--iter 1700--  -9310.49 [ 0.24064603] [ 3.47634006] [ 0.40978304] [ 1.05830407]
--iter 1800--  -9319.29 [ 0.25953537] [ 3.52139282] [ 0.43882227] [ 1.02795386]
--iter 1900--  -9309.48 [ 0.19875516] [ 3.56827807] [ 0.41905719] [ 1.03919828]
--iter 2000--  -9308.48 [ 0.2011531] [ 3.61226368] [ 0.42295811] [ 1.04413998]
--iter 2100--  -9312.73 [ 0.25449812] [ 3.65281582] [ 0.43291309] [ 1.04129887]
--iter 2200--  -9308.21 [ 0.24574596] [ 3.69473004] [ 0.40537748] [ 1.05945027]
--iter 2300--  -9307.04 [ 0.2059705] [ 3.72657275] [ 0.43232909] [ 1.03730333]
--iter 2400--  -9304.43 [ 0.21459806] [ 3.77185726] [ 0.42980325] [ 1.03777087]
--iter 2500--  -9313.18 [ 0.26163131] [ 3.80496669] [ 0.44235253] [ 1.02436149]
--iter 2600--  -9303.51 [ 0.23328483] [ 3.83782959] [ 0.39624694] [ 1.06666136]
--iter 2700--  -9303.43 [ 0.23786294] [ 3.87924814] [ 0.43664715] [ 1.03804207]
--iter 2800--  -9304.53 [ 0.24487503] [ 3.90380359] [ 0.41273075] [ 1.05526364]
--iter 2900--  -9312.3 [ 0.26492223] [ 3.92552567] [ 0.4291718] [ 1.04484832]
--iter 3000--  -9303.04 [ 0.23329842] [ 3.95172238] [ 0.38813078] [ 1.07066631]
--iter 3100--  -9306.99 [ 0.20904279] [ 3.97139597] [ 0.40282527] [ 1.05754507]
--iter 3200--  -9302.6 [ 0.22698854] [ 3.99188924] [ 0.43766332] [ 1.03093052]
--iter 3300--  -9302.08 [ 0.23000285] [ 4.00847149] [ 0.42313612] [ 1.04232717]
--iter 3400--  -9328.76 [ 0.18223822] [ 4.03249598] [ 0.42082006] [ 1.04291892]
--iter 3500--  -9302.45 [ 0.23473594] [ 4.0504694] [ 0.4450148] [ 1.02533484]
--iter 3600--  -9307.42 [ 0.25978929] [ 4.06660795] [ 0.43373036] [ 1.02583826]
--iter 3700--  -9302.64 [ 0.22508581] [ 4.09483194] [ 0.42464665] [ 1.04254818]
--iter 3800--  -9302.75 [ 0.22852573] [ 4.11203527] [ 0.38806486] [ 1.06426346]
--iter 3900--  -9302.28 [ 0.22734053] [ 4.12776613] [ 0.40828222] [ 1.05607522]
--iter 4000--  -9302.49 [ 0.24288028] [ 4.1355896] [ 0.39590809] [ 1.07276773]
--iter 4100--  -9303.64 [ 0.24975184] [ 4.15130949] [ 0.44481567] [ 1.02242696]
--iter 4200--  -9305.48 [ 0.25783929] [ 4.17065382] [ 0.43548003] [ 1.04397845]
--iter 4300--  -9305.86 [ 0.25938481] [ 4.19119167] [ 0.44165394] [ 1.0212208]
--iter 4400--  -9315.78 [ 0.27957699] [ 4.19599152] [ 0.39575335] [ 1.06278408]
--iter 4500--  -9305.16 [ 0.25831419] [ 4.20386553] [ 0.43695194] [ 1.03337169]
--iter 4600--  -9314.09 [ 0.27569702] [ 4.20209837] [ 0.38247874] [ 1.07518673]
--iter 4700--  -9301.96 [ 0.23843226] [ 4.21175766] [ 0.43329525] [ 1.04947829]
--iter 4800--  -9340.06 [ 0.31181169] [ 4.22191191] [ 0.44840562] [ 1.0301826]
--iter 4900--  -9303.42 [ 0.24821644] [ 4.2437191] [ 0.37799585] [ 1.07744348]
--iter 5000--  -9316.45 [ 0.28380355] [ 4.25541449] [ 0.42180914] [ 1.04452527]

In [10]:
st

In [ ]:
# lA1 = tf.lgamma(r + x) + r*tf.log(alpha) - tf.lgamma(r)
# lA2 = tf.lgamma(a + b) + tf.lgamma(b + x) - tf.lgamma(b) - tf.lgamma(a + b + x)
# lA3 = - (r + x)*tf.log(alpha + T)
# lA4 = tf.log(a) - tf.log(b + x - 1) - (r + x) * tf.log(alpha + tx)

# LLA =  -tf.reduce_sum(lA1 + lA2 +tf.log(tf.exp(lA3) +tf.where(x > 0, tf.exp(lA4), tf.zeros_like(x))))
# Lloss =  tf.reduce_sum(lA1 + lA2 +tf.log(tf.exp(lA3) +tf.where(x > 0, tf.exp(lA4), tf.zeros_like(x))))