Weighted sum using multiple pandas series? - python

I have 3 pandas series which I want to make a weighted sum.
Basically i want to sum all of the elements in p,r,y and do the following operation
(1/n) * ((r * y) / p) where n is the number of elements
p = pd.Series(np.random.random(5))
r = pd.Series([1,0,0,1,0])
y = pd.Series([1,0,0,0,1])
How can I do this in a clean way?
I did it like so?
(1/len(r))((r.sum() * y.sum()) / p.sum())

I can only think of lambda for sum to make the code cleaner:
s = lambda x: x.sum()
(s(r)*s(y)/s(p))/len(r)
N = 5
p = pd.Series(np.random.random(N))
r = pd.Series([1,0,0,1,0])
y = pd.Series([1,0,0,0,1])
(s(r)*s(y)/s(p)) /N

Related

Converting Mathematica Fourier series code to Python

I have some simple Mathematica code that I'm struggling to convert to Python and could use some help:
a = ((-1)^(n))*4/(Pi*(2 n + 1));
f = a*Cos[(2 n + 1)*t];
sum = Sum[f, {n, 0, 10}];
Plot[sum, {t, -2 \[Pi], 2 \[Pi]}]
The plot looks like this:
For context, I have a function f(t):
I need to plot the sum of the first 10 terms. In Mathematica this was pretty straighforward, but for some reason I just can't seem to figure out how to make it work in Python. I've tried defining a function a(n), but when I try to set f(t) equal to the sum using my list of odd numbers, it doesn't work because t is not defined, but t is a variable. Any help would be much appreciated.
Below is a sample of one of the many different things I've tried. I know that it's not quite right in terms of getting the parity of the terms to alternate, but more important I just want to figure out how to get 'f' to be the sum of the first 10 terms of the summation:
n = list(range(1,20,2))
def a(n):
return ((-1)**(n))*4/(np.pi*n)
f = 0
for i in n:
f += a(i)*np.cos(i*t)
modifying your code, look the part which are different, mostly the mistake was in the part which you are not calculating based on n 0-10 :
n = np.arange(0,10)
t = np.linspace(-2 * np.pi, 2 *np.pi, 10000)
def a(n):
return ((-1)**(n))*4/(np.pi*(2*n+1))
f = 0
for i in n:
f += a(i)*np.cos((2*i +1) * t)
however you could write you could in matrix form, and avoid looping, using the vector and broadcasting:
n = np.arange(10)[:,None]
t = np.linspace(-2 * np.pi, 2 *np.pi, 10000)[:,None]
a = ((-1) ** n) * 4 / (np.pi*(2*n + 1))
f = (a * np.cos((2 * n + 1) * t.T )).sum(axis=0)

Removing a row of data that contains values more than two standard deviations away from the mean

So I have a list of lists as my dataset X. I computed the means and standard deviations of each row and stored them each into their own list. My goal is finding which rows of X have outliers (values that are more than two standard deviations away from the mean) and deleting the entire row. I have only accomplished removing the outliers from a single test list and not a list of lists:
from math import sqrt
def std_dev(lst): # standard deviation function
mean = float(sum(lst)) / len(lst)
return sqrt(sum((x - mean)**2 for x in lst) / len(lst))
def compute_std(X):
std = []
std.append([std_dev(char) for char in X])
return std
std = compute_std(X)
def means(lst):
return float(sum(lst)) / len(lst)
def compute_mean(X):
mean = []
mean.append([means(chars) for chars in X])
return mean
mean = compute_mean(X)
final_list1 = [x for x in X if (x > mean - 2 * std)]
final_list = [x for x in final_list1 if (x < mean + 2 * std)]
The last two lines of code have only worked on a single list and I want it to iterate through each list in X. I am new to python and list comprehension.
I did not use a list comprehension to get the valid rows of X, and I'm not sure I have found the qualifying rows correctly, (with the stdev and mean).
Perhaps someone else can create a comprehension, but I thought there were too many calculations (mean, stddev, and the comparison) to make a succinct list comprehension.
from statistics import mean, stdev
from random import randint, seed
#seed(1)
X = []
final_list = []
for i in range(10):
M = []
for j in range(25):
M.append(randint(-25, 1000))
X.append(M)
for row in X:
sd = stdev(row)
avg = mean(row)
#print(avg, sd)
high = avg + 2 * sd
low = avg - 2 * sd
if all(low < num < high for num in row):
final_list.append(row)
print(len(final_list))

How to calculate sum of terms of Taylor series of sin(x) without using inner loops or if-else?

I can't use inner loops
I can't use if-else
I need to compute the following series:
x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...
I am thinking something like the following:
n =1
x =0.3
one=1
fact1=1
fact2=1
term =0
sum =0
for i in range(1, n+1, 2):
one = one * (-1)
fact1 = fact1*i
fact2 = fact2*i+1
fact = fact1*fact2
x = x * x
term = x/fact
sum = sum + term
But, I am finding hard times in keeping the multiplications of both fact and x.
You want to compute a sum of terms. Each term is the previous term mutiplied by -1 * x * x and divided by n * (n+1). Just write it:
def func(x):
eps = 1e-6 # the expected precision order
term = x
sum = term
n = 1
while True:
term *= -x * x
term /= (n+1) * (n+2)
if abs(term) < eps: break
sum += term
n += 2
return sum
Demo:
>>> func(math.pi / 6)
0.4999999918690232
giving as expected 0.5 with a precision of 10e-6
Note: the series is the well known development of the sin function...
Isn't that a Taylor series for sin(x)? And can you use list comprehension? With list comprehension that could be something like
x = 0.3
sum([ (-1)**(n+1) * x**(2n-1) / fact(2n-1) for n in range(1, numOfTerms)])
If you can't use list comprehension you could simply loop that like this
x=0.3
terms = []
for n in range(1, numberOfTerms):
term = (-1)**(n+1)*x**(2n-1)/fact(2n-1)
terms.append(term)
sumOfTerms = sum(terms)
Then calculating the factorial by recursion:
def fact(k):
if (k == 1):
return n
else:
return fact(k-1)*k
Calcualting the factorial using Striling's approximation:
fact(k) = sqrt(2*pi*k)*k**k*e**(-k)
No if-else here nor inner loops. But then there will be precision errors and need to use math lib to get the constants or get even more precision error and use hard coded values for pi and e.
Hope this can help!
n = NUMBER_OF_TERMS
x = VALUE_OF_X
m = -1
sum = x # Final sum
def fact(i):
f = 1
while i >= 1:
f = f * i
i = i - 1
return f
for i in range(1, n):
r = 2 * i + 1
a = pow (x , r)
term = a * m / fact(r);
sum = sum + term;
m = m * (-1)

More information on output array with equation and indicies

I have a math function whose output is defined by two variables, x and y.
The function is e^(x^3 + y^2).
I want to calculate every possible integer combination between 1 and some defined integer for x and y, and place them in an array so that each output is aligned with the cooresponding x value and y value index. So something like:
given:
x = 3
y = 5
output would be an array like this:
f(1,1) f(1,2) f(1,3)
f(2,1) f(2,2) f(2,3)
f(3,1) f(3,2) f(3,3)
f(4,1) f(4,2) f(4,3)
f(5,1) f(5,2) f(5,3)
I feel like this is an easy problem to tackle but I have limited knowledge. The code that follows is the best description.
import math
import numpy as np
equation = math.exp(x**3 + y**2)
#start at 1, not zero
i = 1
j = 1
#i want an array output
output = []
#function
def shape_f (i,j):
shape = []
output.append(shape)
while i < x + 1:
while j < y +1:
return math.exp(i**3 + j**2)
#increase counter
i = i +1
j = j +1
print output
I've gotten a blank array recently but I have also gotten one value (int instead of an array)
I am not sure if you have an indentation error, but it looks like you never do anything with the output of the function shape_f. You should define your equation as a function, rather than expression assignment. Then you can make a function that populates a list of lists as you describes.
import math
def equation(x, y):
return math.exp(x**3 + y**2)
def make_matrix(x_max, y_max, x_min=1, y_min=1):
out = []
for i in range(x_min, x_max+1):
row = []
for j in range(y_min, y_max+1):
row.append(equation(i, j))
out.append(row)
return out
matrix = make_matrix(3, 3)
matrix
# returns:
[[7.38905609893065, 148.4131591025766, 22026.465794806718],
[8103.083927575384, 162754.79141900392, 24154952.7535753],
[1446257064291.475, 29048849665247.426, 4311231547115195.0]]
We can do this very simply with numpy.
First, we use np.arange to generate a range of values from 0 (to simplify indexing) to a maximum value for both x and y. We can perform exponentiation, in a vectorised manner, to get the values of x^3 and y^2.
Next, we can apply np.add on the outer product of x^3 and y^3 to get every possible combination thereof. The final step is taking the natural exponential of the result:
x_max = 3
y_max = 5
x = np.arange(x_max + 1) ** 3
y = np.arange(y_max + 1) ** 2
result = np.e ** np.add.outer(x, y)
print(result[2, 3]) # e^(2 ** 3 + 3 ** 2)
Output:
24154952.753575277
A trivial solution would be to use the broadcasting feature of numpy with the exp function:
x = 3
y = 5
i = np.arange(y).reshape(-1, 1) + 1
j = np.arange(x).reshape(1, -1) + 1
result = np.exp(j**3 + y**2)
The reshape operations make i into a column with y elements and j into a row with x elements. Exponentiation does not change those shapes. Broadcasting happens when you add the two arrays together. The unit dimensions in one array get expanded to the corresponding dimension in the other. The result is a y-by-x matrix.

Use index of summation as the order of derivation

Given some function f, I want to compute the following sum using sympy:
In general I want to use the index of summation as the order of differentiation of the function but I could not find out how to do it with sympy.
Given n is an int you know in advance, you can construct a function:
from sympy import diff
def sum_diff_order(f,x,n):
g = 0
for i in range(n+1):
g += diff(f,x,i)
return g
So if you take f to be x**10 and n=5, we get:
>>> x = symbols('x')
>>> f = x**10
>>> sum_diff_order(f,x,5)
x**10 + 10*x**9 + 90*x**8 + 720*x**7 + 5040*x**6 + 30240*x**5
import sympy as sp
x = sp.symbols('x')
f = sp.Function('f')
n = 2
sum([f(x).diff(x,i) for i in range(n+1)])
f(x) + Derivative(f(x), x) + Derivative(f(x), x, x)
If n is an known integer, you can use something like Add(*[diff(f(x), x, i) for i in range(n+1)]). For symbolic n or infinity, it isn't possible yet, as there is no way yet to represent derivatives of symbolic order.

Categories