I need to find out how to calculate the 'hypergeometric cdf':
I know how the function looks like and how it works but I have a few problems tipping the function into python:
def hypergeometricCDF(N,K,n,x):
"""
Call:
p = hypergeometricCDF(N,K,n,x)
Input argument:
N: integer
K: integer
n: integer
x: integer
Output argument:
p: float
Example:
hypergeometricCDF(120,34,12,7)
=>
0.995786
"""
f=sum(range(x+1))
p = log_binomial_coeff(N-K,n-f) + log_binomial_coeff(K,f) - log_binomial_coeff(N,n)
return(p)
The problem is, how do I integrate the sum function from i to x? I tried it with sum(range(x+1)) but it won't work.
Try this:
def hypergeometricCDF(N,K,n,x):
"""
Call:
p = hypergeometricCDF(N,K,n,x)
Input argument:
N: integer
K: integer
n: integer
x: integer
Output argument:
p: float
Example:
hypergeometricCDF(120,34,12,7)
=>
0.995786
"""
k = arange(x+1)
p = sum(exp(log_hypergeometricPMF(N,K,n,k)))
return(p)
log_hypergeometricPMF is defined on top of the file ;)
Related
I have a data frame with one column. In each row of this data frame, there is a list. For example :
df = spark.createDataFrame(
[
[[13,23]],
[[55,65]],
],
['col',]
)
Then I defined a UDF which basically adds 1 to first number in the list and add 1.5 to the second number of the list.
def calculate(mylist) :
x = mylist[0] + 1
y = mylist[1] + 1.5
return x,y
The problem is that when I apply this function to my data frame it returns the X value but it does not return the Y value.
I think it is because the Y value is not an integer.
This is the way that I do this.
import pyspark.sql.functions as F
from pyspark.sql.types import IntegerType, ArrayType
func = F.udf(lambda x: calculate(x), ArrayType(IntegerType()))
df.withColumn('vals', func('col')).show()
What can I do to get the Y value as well as X value? I simplified the UDF and sample data frame for the sake of being easy to understand and solve.
calculate udf is returning integer and also float type with the given input.
If your use case first value is integer and second value is float, you can return StructType
If both need to be same type, you can use the same code and change calculate udf which returns both integers
func = F.udf(lambda x: calculate(x), T.StructType(
[T.StructField("val1", T.IntegerType(), True),
T.StructField("val2", T.FloatType(), True)]))
I want to make this program do the summation of something with their input. My code thus far
def summation():
start = int(raw_input("Start value of n?: "))
end = int(raw_input("End value of n?: "))
eqn = lambda n: raw_input("Equation?: ")
sum = 0
for i in range(start , end + 1):
sum += eqn(i)
return sum
print summation() # start will be 1, end will be 5 , equation will be n + 1. Should print 20
I get the error that I can't add an integer and a string together so is there any way to make the raw_input for equation not a string. Like instead of it being 'n + 1', I want it to be n + 1.
You could use input instead of raw_input, but this isn't really a good idea, since every time eqn is called it will call a input and prompt you for the equation.
A better method is to store the equation beforehand (using raw_input), and then use eval in the lambda function. Something like:
def summation():
start = int(raw_input("Start value of n?: "))
end = int(raw_input("End value of n?: "))
fx = raw_input("Equation: ")
eqn = lambda n: eval(fx)
sum = 0
for i in range(start , end + 1):
sum += eqn(i)
return sum
print summation()
Don't you need to surround your raw_input in your eqn variable with an int()?
I use python 3, but that should fix your problems.
The input format is:
6
1
2 5
2 7
2 9
1
1
Input:
First line contains an integer Q, the number of queries. Q lines follow.
A Type-1 ( Customer) Query, is indicated by a single integer 1 in the line.
A Type-2 ( Chef) Query, is indicated by two space separated integers 2 and C (cost of the package prepared) .
I want to read the input from stdin console and here is my code
n = int(input())
stack1 = []
for i in range(n):
x = input()
x = int(x)
if x == 2:
y = input()
stack1.append(y)
elif x == 1:
length = len(stack1)
if length > 0:
print(stack1.pop())
else:
print("No Food")
I have tried x,y = raw_input().split() this statement also fails because sometimes input has single value. Let us know how to read the defined input from stdin ???
Use len() to find length of string based on that change your stdin.
n = int(input())
for i in range(n):
s = input()
if(len(s) > 1):
x,y = s.split()
x = int(x)
else:
x = int(s)
print(x)
Cheers.
Please help me why on executing the below program an error coming on m variable
x=int(input("Enter first number"))
y=int(input("Enter second number"))
def multiplication():
m=x*y
print("Multiplication result"m)
In Python 2, you should accept user inputs with raw_input(): Check this.
x=int(raw_input("Enter first number"))
y=int(raw_input("Enter second number"))
Please follow a proper indentation plan with python:
Also, you did not accept the variables while defining your function, and learn how to use print:
def multiplication(x, y):
m = x * y
print "Multiplication result: %d" % m
Finally, to call this function, use:
multiplication(x, y)
x=int(raw_input("enter first number"))
os=raw_input("Enter the sign of what you wanna do +,-,/,*")
y=int(raw_input("enter second number"))
You can also do like this if you want to keep it in functions.
def input_function():
x = int(raw_input("Enter first number"))
y = int(raw_input("Enter second number"))
return x,y
def multiplication():
x,y = input_function()
m = x * y
print "Multiplication result", m
multiplication()
Or like this, in one function. But it doesn't look so pretty.
def multiplication(x,y):
m = x * y
print "Multiplication result",m
multiplication(int(raw_input('Enter first number')),int(raw_input('Enter second number')))
def r():
v = int(input("voltage: "))
i = int(input("current: "))
resistance = v*i
return(resistance,'ohms')
I need to complete the function exponentialPDF but get an error:
'IndexError: index 0 is out of bounds for axis 0 with size 0'
The function looks like this:
def uniformPDF(x,a=0.0,b=4.0):
p = 1.0/(b-a)*ones((len(x),))
p[x<a] = 0.0
p[x>b] = 0.0
return(p)
def exponentialPDF(x,a=1.0):
"""
Call:
p = exponentialPDF(x,a)
Input argument:
vals: float (array)
Output argument:
p: float (array)
Examples:
In[1]: exponentialPDF([1,2],3)
Out[1]: array([ 0.14936121, 0.03332699])
"""
p = a * exp(-a*x)
p[x<0] = 0.0
return(p)
Can someone help me with the error?
Sounds like the list you are passing in to your function is empty. Read about python lists and see the following post:
IndexError: list assignment index out of range
I found the function works using numpy array, e.g. p = exponentialPDF(np.array([1,2]),3). Hope this help, you should check out the SO homework post and ask again if you're still stuck.
EDIT: As you are using numpy, I would add an explicit convert to numpy array in the function as follows:
def exponentialPDF(x,a=1.0):
"""
Call:
p = exponentialPDF(x,a)
Input argument:
vals: float (array)
Output argument:
p: float (array)
Examples:
In[1]: exponentialPDF([1,2],3)
Out[1]: array([ 0.14936121, 0.03332699])
"""
if type(x) is list:
x = array(x)
p = a * exp(-a*x)
p[x<0] = 0.0
return(p)
Hopefully this will fix your problem and you can use the function as needed (assuming returning a numpy array from the function is okay).