Analytical integration using sympy

51 views Asked by At

I have this integral, the semi standard deviation for calculation the Sortino ratio, however I cannot seem to get an analytical solution to this

The mathematical expression of the integral

import sympy as sy 
c = sy.Symbol('B')
x = sy.Symbol('x') 
a= sy.Symbol('-sy.oo')

x = sy.Symbol('x')
f=sy.Symbol('f')
d=sy.Symbol('d')

f1 = sy.integrate(sy.sqrt((c-x)**2* f(x)*dx),(x, -sy.oo, c))

I get the below error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/hc/13tll2g535x553nmqlfcyztr0000gn/T/ipykernel_70280/1712613650.py in <module>
----> 1 f1 = sy.integrate(sy.sqrt((c-x)**2* f(x)*dx),(x, -sy.oo, 0.2))

TypeError: 'Symbol' object is not callable
1

There are 1 answers

0
Stef On

You defined f as a Symbol, so sympy doesn't know what to do when you write f(x).

Try with sympy.Function instead of sympy.Symbol.

import sympy as sy

c, x, d = sy.symbols('B x c')
f = sy.Function('f')

f1 = sy.integrate(sy.sqrt((c-x)**2* f(x)),(x, -sy.oo, c))

print(f1)
# Integral(sqrt((B - x)**2*f(x)), (x, -oo, B))