0

I want to get simulation observation with kernel Density, but I have the Following error type: TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

how to resolve it? This is the code that I use:

    from matplotlib.pyplot import *
    from math import *
    from array import *
    import numpy as np 
    from numpy.random import *
    from scipy.misc import *
    from scipy.stats import *
    from scipy import *
    from random import *


    N=1000
    n=30
    lamb=0.5
    X=lamb*tan(pi*(np.reshape(rand(n,1),n)-0.5)) #loi de Cauchy  
    x=1
    alpha=0.45

    def k_gaussien(x):
        sigma=1
        if(sigma<=0):
            return((1/(sigma*sqrt(2*pi)))*exp(-(x**2/(2*sigma**2))))

    def h(n,alpha):
        for i in range(1,n):   
            return (i**(1-alpha))

    def f_PR(x,X,alpha):
        global F;
        F = ones((n,1))
        h_n = h(n,alpha)
        for k in range(2,n):
            for i in  range(1,k):
                F[k] = F[k-1] + k_gaussien((x-X[i])*i**alpha)
            F[k] = F[k-1] * h_n
        return F

    # Almost sure convergence f_n(x)--> f(x) ps
    figure(figsize=(20,10))
    fPR=f_PR(x,X,alpha)
    T=linspace(1,n,n);
    plot(cumsum(fPR)/T)
    plot(T,(1/pi)*(lamb/(lamb**2 + y**2))*linspace(1,1,N),"r--",lw=3)#with Cauchy density
    grid(True)
    title("convergence presque sure",fontsize=20,color="blue")

    #Convergence in mean N(0,e2f(x))
    hist(fPR,bins=linspace(-10,10,50),normed=True)
    y=linspace(-10,10,100);
    v=(1/pi)*(lamb/(lamb**2 + y**2))
    plot(y,(1/sqrt(2*pi)*v)*exp ((-(y*y)/(2*v**2)))*linspace(1,1,n),'r--') #with cauchy density
    title("convergence asymptotique", fontsize=20,color="blue")

TypeError Traceback (most recent call last)

<ipython-input-76-13bc86608417> in <module>()
         38 # Almost sure convergence f_n(x)--> f(x) ps
         39 figure(figsize=(20,10))
    ---> 40 fPR=f_PR(x,X,alpha)
         41 T=linspace(1,n,n);
         42 plot(cumsum(fPR)/T)

    <ipython-input-76-13bc86608417> in f_PR(x, X, alpha)
         32     for k in range(2,n):
         33         for i in  range(1,k):
    ---> 34             F[k] = F[k-1] + k_gaussien((x-X[i])*i**alpha)
         35         F[k] = F[k-1] * h_n
         36     return F

TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

1
  • Hi, welcome to Stackoverflow. Please post the complete stacktrace. Commented Nov 11, 2018 at 11:02

1 Answer 1

0

It's in your definition:

def k_gaussien(x):
    sigma=1
    if(sigma<=0):
        return((1/(sigma*sqrt(2*pi)))*exp(-(x**2/(2*sigma**2))))

You hardcoded sigma=1, but your function only returns something if sigma<=0 which will never be the case. So k_gaussien((x-X[i])*i**alpha) will return None. Therefore F[k] = F[k-1] + k_gaussien((x-X[i])*i**alpha) tries to sum float and None types, which does not work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.