Problem with simulation kernel density with python









up vote
0
down vote

favorite












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'










share|improve this question























  • Hi, welcome to Stackoverflow. Please post the complete stacktrace.
    – quant
    Nov 11 at 11:02














up vote
0
down vote

favorite












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'










share|improve this question























  • Hi, welcome to Stackoverflow. Please post the complete stacktrace.
    – quant
    Nov 11 at 11:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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'










share|improve this question















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'







python simulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:32

























asked Nov 11 at 9:49









Sa Majesté

83




83











  • Hi, welcome to Stackoverflow. Please post the complete stacktrace.
    – quant
    Nov 11 at 11:02
















  • Hi, welcome to Stackoverflow. Please post the complete stacktrace.
    – quant
    Nov 11 at 11:02















Hi, welcome to Stackoverflow. Please post the complete stacktrace.
– quant
Nov 11 at 11:02




Hi, welcome to Stackoverflow. Please post the complete stacktrace.
– quant
Nov 11 at 11:02












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247521%2fproblem-with-simulation-kernel-density-with-python%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    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.






    share|improve this answer
























      up vote
      0
      down vote



      accepted










      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.






      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 13:31









        user8408080

        982139




        982139



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247521%2fproblem-with-simulation-kernel-density-with-python%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党