0

I want to find the parameters of a Weibull distribution by minimizing the parameters using Kullbak-Leibler method. I found a code here which did the same thing. I replaced the Normal distributions in the original code by the Weibull distributions. I do not know why I get “Nan” parameters and “Nan” Kullback-Leibler divergence value. Can anyone please help?

import numpy as np
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import seaborn as sns
sns.set()
from scipy.stats import weibull_min

learning_rate = 0.001
epochs = 100

x = np.arange(0, 2000,0.001)
p_pdf=weibull_min.pdf(x, 1.055,0, 468).reshape(1, -1)
p = tf.placeholder(tf.float64, shape=p_pdf.shape)

alpha = tf.Variable(np.zeros(1))
beta = tf.Variable(np.eye(1))

weibull=(beta / alpha) * ((x / alpha)**(beta - 1)) * tf.exp(-((x / alpha)**beta))
q = weibull
kl_divergence = tf.reduce_sum(tf.where(p == 0, tf.zeros(p_pdf.shape, tf.float64), p * tf.log(p / q)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(kl_divergence)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    history = []    
    alphas = []
    betas = []
    
    
    for i in range(epochs):
        sess.run(optimizer, { p: p_pdf })
        
        if i % 10 == 0:
            history.append(sess.run(kl_divergence, { p: p_pdf }))
            alphas.append(sess.run(alpha)[0])
            betas.append(sess.run(beta)[0][0])
            
    for a, b in zip(alphas, betas):

        q_pdf =weibull_min.pdf(x, b,0,a)
        plt.plot(x, q_pdf.reshape(-1, 1), c='red')

plt.title('KL(P||Q) = %1.3f' % history[-1])
plt.plot(x, p_pdf.reshape(-1, 1), linewidth=3)
plt.show()  
plt.plot(history)
plt.show()   
sess.close()
Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
NeSha
  • 1
  • 1

1 Answers1

1

Try initialising your alphas to not be 0. Perhaps initialise to np.ones(1) instead.

If you use an alpha of zero you will get a nan with scipy.

from scipy.stats import weibull_min

weibull_min.pdf(100, 0, 0, 2.), weibull_min.pdf(100, 1, 0, 2.)
(nan, 9.643749239819589e-23)