import tensorflow as tf


def cw_1d(X, y=None):

    def N0(mean, variance):
        return 1.0/(tf.sqrt(2.0 * m.pi * variance)) * tf.exp((-(mean**2))/(2*variance))

    N = tf.cast(tf.shape(X)[0], tf.float32)
    if y is None:
        y = silverman_rule_of_thumb(N)

    A = tf.subtract(tf.expand_dims(X, 0), tf.expand_dims(X, 1))
    return (1.0/(N*N)) * tf.reduce_sum(N0(A, 2*y)) + N0(0.0, 2.0 + 2*y) - (2/N) * tf.reduce_sum(N0(X, 1.0 + 2*y))
