1

I'm quite new to programming with python.

I was wondering, if there is a smart way to solve a function, which includes a gamma function with a certain shape and scale.

I already created a function G(x), which is the cdf of a gamma function up to a variable x. Now I want to solve another function including G(x). It should look like: 0=x+2*G(x)-b. Where b is a constant.

My code looks like that:

b= 10

def G(x): 
  return gamma.cdf(x,a=4,scale=25)

f = solve(x+2*G(x)-b,x,dict=True)

How is it possible to get a real value for G(x) in my solve function?

Thanks in advance!

Hello3456
  • 25
  • 4

2 Answers2

1

To get roots from a function there are several tools in the scipy module.

Here is a solution with the method fsolve()

from scipy.stats import gamma
from scipy.optimize import fsolve

def G(x): 
  return gamma.cdf(x,a=4,scale=25)

# we define the function to solve
def f(x,b):
  return x+2*G(x)-b

b = 10
init = 0. # The starting estimate for the roots of f(x) = 0.
roots = fsolve(f,init,args=(b))

print roots

Gives output :

[9.99844838]

Given that G(10) is close to zero this solution seems likely

Sorry, I didn't take into account your dict=True option but I guess you are able to put the result in whatever structure you want without my help.

manu190466
  • 1,557
  • 1
  • 9
  • 17
0
rom sympy import *
# from scipy.stats import gamma
# from sympy.stats import Arcsin, density, cdf
x, y, z, t, gamma, cdf = symbols('x y z t gamma cdf')

#sol = solve([x - 3, y - 1], dict=True)

from sympy.stats import Cauchy, density
from sympy import Symbol
x0 = Symbol("x0")
gamma = Symbol("gamma", positive=True)
z = Symbol("z")
X = Cauchy("x", x0, gamma)
density(X)(z)
print(density(X)(z))

sol = solve([x+2*density(X)(z)-10, y ], dict=True)
print(sol)

Or:

from scipy.stats import gamma
from sympy import solve, Poly, Eq, Function, exp
from sympy.abc import x, y, z, a, b

def G(x): 
  return gamma.cdf(x,a=4,scale=25)

b= 10

f = solve(x+2*G(x)-b,x,dict=True)

stats cdf gamma solve sympy

Mahsa Hassankashi
  • 2,086
  • 1
  • 15
  • 25
  • I'm sorry, I don't know what you mean. My gamma is already defined, I wanted to access my gamma function G(x) in f(x). I want to solve f(x), which is depending on G(x) so that I get a value for x. – Hello3456 May 29 '20 at 17:35
  • Sorry for the misunderstanding... You have assigned once a=10 in the global section and other times inside G(x): a=4? I edited my answer I hope this time I understood you well. I thought that a=10 is x=10, let me correct me... – Mahsa Hassankashi May 29 '20 at 18:14
  • Ah my bad, sorry! The a in G(x) is another variable than the a in f(x). I changed the one in f(x) to b. I hope now it gets more clear. My problem is, that I don't know what's the value of x, that's why I want to solve f(x) as a function. I hope now it's clear, thank you so much! – Hello3456 May 29 '20 at 18:43
  • 1
    No problem, do you have value for x parameter? – Mahsa Hassankashi May 29 '20 at 18:55
  • Do you want to have test? https://docs.python.org/3/library/unittest.mock.html – Mahsa Hassankashi May 29 '20 at 19:01
  • No, I have no value for x. I want to solve for x, depending on x + 2*G(x) - b, where I know all parameters. Only x is unknown. – Hello3456 May 29 '20 at 19:24