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'
python simulation
add a comment |
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'
python simulation
Hi, welcome to Stackoverflow. Please post the complete stacktrace.
– quant
Nov 11 at 11:02
add a comment |
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'
python simulation
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
python simulation
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 13:31
user8408080
982139
982139
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Hi, welcome to Stackoverflow. Please post the complete stacktrace.
– quant
Nov 11 at 11:02