binom.pmf only returning zero - python

code:
def expected_profit(n):
total = 0
X = np.arange(0,n+1)
p = np.arange(0,n+1)
profit = np.arange(0,n+1)
for i in list(range(1,n+1)):
print("X_i:", X[i])
p[i] = binom.pmf(X[i],n,19/20)
print(p[i])
if X[i] > 100:
profit[i] = 50*n-60*(X[i]-100)
else:
profit[i] = 50*n
total += profit[i]*p[i]
return total
expected_profit(10)
>>>0
For some reason, after each iteration, p[i] is equal to zero. Yet when I manually type out (for example) binom.pmf(10,10,19/20) I get a non zero answer. What is the problem here?
This seems to happen with any call to binom.pmf within the function call.

With p = np.arange(0,n+1) you initialize p with an integer array 0,...,n. That makes that binom.pmf(...) is converted to an integer when assigned to p[i]. The solution is to make p an array of floats. np.zeros() by default creates an array of floats. The same problem holds for profit.
Fitting this into the code would look like:
from scipy.stats import binom
import numpy as np
def expected_profit(n):
n = 10
total = 0
X = np.arange(0, n + 1)
p = np.zeros(n + 1, dtype=float)
profit = np.zeros(n + 1, dtype=float)
for i in range(1, n + 1):
p[i] = binom.pmf(X[i], n, 19/20)
if X[i] > 100:
profit[i] = 50 * n - 60 * (X[i] - 100)
else:
profit[i] = 50 * n
total += profit[i] * p[i]
expected_profit(10)

Related

Why do I get the message "TypeError: 'complex' object is not subscriptable" when trying to use a complex number in a np.sum?

Am trying to write code for an equation that includes complex number, and put it into a function for simpsons rule.
import numpy as np
import cmath as cmp
import matplotlib.pyplot as plt
wavelength = 0.000001 #meters
Apw = 0.00002 # meters Apw taken as apeture width
z = 0.02 # meters
N = 100
permittivity = 0.00000000000885418783
c = 299792458 # m/s
k = (2*cmp.pi)/wavelength
j = 0 + 1j
n = 100
x = np.linspace(-0.005, 0.005, n, 1.1)
def simps (N, k, Apw, x):
S = 0
h = Apw / N
for i in range(0, N + 1):
for xprime in range(0, N+1):
xprime = Apw*xprime/N
f = cmp.exp(((j*k)/(2*z))*(x-xprime)**2)
if (i != 0) and (i != n):
f *= (2 + (2 * (i % 2)))
S = h/3 * np.sum(f[0:-1:2] + 4*f[1::2] + f[2::2])
return S
x = np.linspace(-0.005, 0.005, n, 1.1)
I = np.zeros([n])
for i in range(0,n):
E_0 = simps(N, k, Apw, x[i])[0]
I[i] = permittivity*c*(E_0 * cmp.conj(E_0)).real
Where j is a complex number j = 0 + 1j
I don't really know if what I am doing is anywhere near correct, the lines that are causing the error is line 46 and 36
In your code, f is a complex number. As the error message says, you can subscript (use square braces on) a complex number. For this reason, the expressions f[0:-1:2], f[1::2] and f[2::2] are illegal operations that are producing this error. It seems that maybe you think that f is a list of complex values, rather than a single value?
If you do know that f is a single complex value, then the only operations on it that I can think of are to extract the real and imaginary parts with f.real and f.imag.

ValueError with solve_ivp because of RuntimeWarning: invalid value encountered in power

I'm developing an evolutionary model, of which a shortened (and therefore biologically senseless) version looks like this:
n = 300 # population size
no_gens = 600 # total number of generations
R = 1000 # relative timescale of environmental variation
P = 0.9 # environmental predictability
def environment(t):
E = (np.sin(2 * np.pi * t / R) + 1) / 2
mu_env = 0.5 * (1 - P) + P * E
sigma_env = 0.5 * (1 - P) / 3
C = np.random.normal(mu_env, sigma_env)
return E, C
# initial population
def create_pop(n):
pop = np.zeros(n, dtype=[('E','<f8'), ('C','<f8'), ('hill','<f8'), ('phenotype','<f8'), ('fitness','<f8'),
('G','<f8'), ('basal','<f8'), ('b', '<f8'), ('alpha','<f8')])
pop['G'] = np.random.random(n)
pop['b'] = np.random.random(n)
pop['hill'] = 1
return pop
def phenotype(pop):
pop['basal'] = pop['G'] + pop['b'] * pop['C']
def phenotype_ode(t_ode, x):
dxdt = pop['basal'] - x + pop['alpha'] * (1 / (1 + x ** (-pop['hill'])))
return dxdt
t_end = 1e02
sol_lower = solve_ivp(phenotype_ode, [0, t_end], np.zeros(n), method='BDF')
lower_steady_states = sol_lower.y[:, -1]
return lower_steady_states
def mutation(pop):
if np.random.random() <= 0.001:
pop['G'] += np.random.normal(0, 0.05, n)
pop['G'][np.flatnonzero(pop['G'] < 0)] = 0
pop['G'][np.flatnonzero(pop['G'] > 1)] = 1
if np.random.random() <= 0.001:
pop['b'] += np.random.normal(0, 0.05, n)
pop['b'][np.flatnonzero(pop['b'] < 0)] = 0
pop['b'][np.flatnonzero(pop['b'] > 1)] = 1
if np.random.random() <= 0.001:
pop['hill'] += np.random.normal(0, 0.05, n)
pop['hill'][np.flatnonzero(pop['hill'] < 1)] = 1
if np.random.random() <= 0.001:
pop['alpha'] += np.random.normal(0, 0.05, n)
pop['alpha'][np.flatnonzero(pop['alpha'] < 0)] = 0
def reproduction(pop):
mutation(pop)
pop['phenotype'] = phenotype(pop)
return pop
# iteration
pop = create_pop(n)
for t in progressbar.progressbar(range(no_gens+1)):
enviro = environment(t)
pop['E'] = enviro[0]
pop['C'] = enviro[1]
pop = reproduction(pop)
With a sufficiently large number of generations, there may occur a
RuntimeWarning: invalid value encountered in power, finally leading to
ValueError: array must not contain infs or NaNs (if I'm reading the error message correctly).
Having read Numpy error: invalid value encountered in power, I was assuming a data type issue. The dtype of pop['hill'] is '<f8'. If I'm not mistaken, np.dtype('f8') is equivalent to np.int64 (see, for example, https://jakevdp.github.io/PythonDataScienceHandbook/02.09-structured-data-numpy.html), i.e., exactly the data type for which all went well in the above SO link. Does the issue then have to do with the "little endian" (<)?
Or am I completely barking up the wrong tree here??
P.S. Of course, I can post the complete error message (>30 lines), if this helps!

how to append local variable from function to array/list in python?

How to append local variable from inside function to array/list in python?
below is my code.
I want to append corr variable to an empty array.
suppose T=[] .
its not appending and going in infinite loop.
how can I do this?
# Python Program to find correlation coefficient.
import math
# function that returns correlation coefficient.
def correlationCoefficient(X, Y, n) :
sum_X = 0
sum_Y = 0
sum_XY = 0
squareSum_X = 0
squareSum_Y = 0
i = 0
while i < n :
# sum of elements of array X.
sum_X = sum_X + X[i]
# sum of elements of array Y.
sum_Y = sum_Y + Y[i]
# sum of X[i] * Y[i].
sum_XY = sum_XY + X[i] * Y[i]
# sum of square of array elements.
squareSum_X = squareSum_X + X[i] * X[i]
squareSum_Y = squareSum_Y + Y[i] * Y[i]
z = ((float)(math.sqrt((n * squareSum_X -sum_X * sum_X)* (n * squareSum_Y -sum_Y * sum_Y))))
y = ((float)(n * sum_XY - sum_X * sum_Y))
i = i + 1
if z == 0:
corr = 0
else:
# use formula for calculating correlation coefficient.
corr=abs(y/z)
while corr<1:
T=[]
T.append(corr)
print("T",T)
return corr
# Driver function
A = [0,7.6,7.7,6.4,6.25,6.4,6.4,5.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8.3,6.4,3.2,3.2,3.25,3.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5.35,5,4.85,5.65,5.4,5.35,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
B = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86]
X = [0]*5
Y = [0]*5
# the size of array.
n=5
# Function call to correlationCoefficient.
k=0
while k <= len(A):
i = k
m = 0
while i <= k+4:
X[m] = A[i]
#print("A[i]",A[i])
Y[m] = B[i]
#print("B[i]",B[i])
i = i + 1
m = m + 1
#correlationCoefficient(X, Y, 5)
print ((correlationCoefficient(X, Y, 5)))
k = k + 1
The relevant bit seems to be here:
corr=abs(y/z)
while corr<1:
T=[]
T.append(corr)
print("T",T)
return corr
You're blanking out the T array each time that while loop runs, and it will run forever if corr<1, since you never change the value of corr.
Move T=[] outside of the while i<n loop if you'd like it to stick around, and modify corr (or use an if instead) to avoid the infinite loop.

Generalized Distance Transform in Python

I'm currently trying to implement the GDT described by Felzenszwalb and Huttenlocher (http://www.cs.cornell.edu/~dph/papers/dt.pdf) inside of Python for an image processing algorithm. However I used the algorithm described in the paper they published a few years back but got faulty results. I found a C# implementation here: https://dsp.stackexchange.com/questions/227/fastest-available-algorithm-for-distance-transform/29727?noredirect=1#comment55866_29727
And converted it to Python (which is pretty much the same I had before).
This is my code:
def of_column(dataInput):
output = zeros(dataInput.shape)
n = len(dataInput)
k = 0
v = zeros((n,))
z = zeros((n + 1,))
v[0] = 0
z[0] = -inf
z[1] = +inf
s = 0
for q in range(1, n):
while True:
s = (((dataInput[q] + q * q) - (dataInput[v[k]] + v[k] * v[k])) / (2.0 * q - 2.0 * v[k]))
if s <= z[k]:
k -= 1
else:
break
k += 1
v[k] = q
z[k] = s
z[k + 1] = +inf
k = 0
for q in range(n):
while z[k + 1] < q:
k += 1
output[q] = ((q - v[k]) * (q - v[k]) + dataInput[v[k]])
return output
I still can't find my error. When giving the algorithm a binary (boolean) numpy array it just returns the array itself not the Distance Transform. Why is this not working in Python?
I got it working after hours and hours. The answer given in the link above implementing the code in C# suggests putting up the "white" areas to a very large number. My dataInput array was a boolean array (0, 1). I replaced all 1s with 2^32 and it works just fine. The higher the number the more blurry it gets. The lower the more similar to the source it gets.
I would like to add the function for 2D that works with the 1D function described previously:
###############################################################################
# distance transform of 1d function using squared distance
###############################################################################
def dt_1d(dataInput, n):
output = np.zeros(dataInput.shape)
k = 0
v = np.zeros((n,))
z = np.zeros((n + 1,))
v[0] = 0
z[0] = -np.inf
z[1] = +np.inf
for q in range(1, n):
s = (((dataInput[q] + q * q) - (dataInput[v[k]] + v[k] * v[k])) / (2.0 * q - 2.0 * v[k]))
while s <= z[k]:
k -= 1
s = (((dataInput[q] + q * q) - (dataInput[v[k]] + v[k] * v[k])) / (2.0 * q - 2.0 * v[k]))
k += 1
v[k] = q
z[k] = s
z[k + 1] = +np.inf
k = 0
for q in range(n):
while z[k + 1] < q:
k += 1
value = ((q - v[k]) * (q - v[k]) + dataInput[v[k]])
if value > 255: value = 255
if value < 0: value = 0
output[q] = value
print output
return output
###############################################################################
# distance transform of 2d function using squared distance
###############################################################################
def dt_2d(dataInput):
height, width = dataInput.shape
f = np.zeros(max(height, width))
# transform along columns
for x in range(width):
f = dataInput[:,x]
dataInput[:,x] = dt_1d(f, height)
# transform along rows
for y in range(height):
f = dataInput[y,:]
dataInput[y,:] = dt_1d(f, width)
return dataInput
I hope it helps.

Python implementation of gradient descent (Machine Learning)

I have tried to implement gradient descent here in python but the cost J just seems to be increasing irrespective of lambda ans alpha value, i am unable to figure out what the issue over here is. It'll be great if someone can help me out with this. The input is a matrix Y and R with same dimensions. Y is a matrix of movies x users and R is just to say if a user has rated a movie.
#Recommender system ML
import numpy
import scipy.io
def gradientDescent(y,r):
(nm,nu) = numpy.shape(y)
x = numpy.mat(numpy.random.randn(nm,10))
theta = numpy.mat(numpy.random.randn(nu,10))
for i in range(1,10):
(x,theta) = costFunc(x,theta,y,r)
def costFunc(x,theta,y,r):
X_tmp = numpy.power(x , 2)
Theta_tmp = numpy.power(theta , 2)
lmbda = 0.1
reg = ((lmbda/2) * numpy.sum(Theta_tmp))+ ((lmbda/2)*numpy.sum(X_tmp))
ans = numpy.multiply(numpy.power(((theta * x.T).T - y),2) , r)
res = (0.5 * numpy.sum(ans))+reg
print "J:",res
print "reg:",reg
(nm,nu) = numpy.shape(y)
X_grad = numpy.mat(numpy.zeros((nm,10)));
Theta_grad = numpy.mat(numpy.zeros((nu,10)));
alpha = 0.1
# [m f] = size(X);
(m,f) = numpy.shape(x);
for i in range(0,m):
for k in range(0,f):
tmp = 0
# X_grad(i,k) += (((theta * x'(:,i)) - y(i,:)').*r(i,:)')' * theta(:,k);
tmp += ((numpy.multiply(((theta * x.T[:,i]) - y[i,:].T),r[i,:].T)).T) * theta[:,k];
tmp += (lmbda*x[i,k]);
X_grad[i,k] -= (alpha*tmp)
# X_grad(i,k) += (lambda*X(i,k));
# [m f] = size(Theta);
(m,f) = numpy.shape(theta);
for i in range(0,m):
for k in range(0,f):
tmp = 0
# Theta_grad(i,k) += (((theta(i,:) * x') - y(:,i)').*r(:,i)') * x(:,k);
tmp += (numpy.multiply(((theta[i,:] * x.T) - y[:,i].T),r[:,i].T)) * x[:,k];
tmp += (lmbda*theta[i,k]);
Theta_grad[i,k] -= (alpha*tmp)
# Theta_grad(i,k) += (lambda*Theta(i,k));
return(X_grad,Theta_grad)
def main():
mat1 = scipy.io.loadmat("C:\Users\ROHIT\Machine Learning\Coursera\mlclass-ex8\ex8_movies.mat")
Y = mat1['Y']
R = mat1['R']
r = numpy.mat(R)
y = numpy.mat(Y)
gradientDescent(y,r)
#if __init__ == '__main__':
main()
I did not check the whole code logic, but assuming it is correct, your costfunc is supposed to return gradient of the cost function, and in these lines:
for i in range(1,10):
(x,theta) = costFunc(x,theta,y,r)
you are overwriting the last values of x and theta with its gradient, while gradient is the measure of change, so you should move in the opposite direction (substract the gradient instead of overwriting the values):
for i in range(1,10):
(x,theta) -= costFunc(x,theta,y,r)
But it seems that you already assign the minus sign to the gradient in your costfunc so you should add this value instead
for i in range(1,10):
(x,theta) += costFunc(x,theta,y,r)

Categories

Resources