Python: defining a function with mean and standard deviation calculation - python

I am trying to calculate mean and the population standard deviation without using stats module...and my code will be
total = 0
sum3 = 0
def stats():
global total
for numbers in range(0,len(my_list)):
total = total + my_list[numbers]
mean = total / len(my_list)
print(mean)
for numbers in range(0,len(my_list)):
global sum3
sum3 = sum3 + (my_list[numbers] - mean)**2
sum21 = sum3 / len(my_list)
standard_dev = sum21**(1/2)
print(standard_dev)
my_list1 = input()
my_list = my_list1.split()
print(my_list)
stats()
also help me to assign a list of numbers to int.....thank u

Try this code. The stats methods have not been used here.
Only python methods have been used to speed up the functions.
def mean(my_list):
sum = sum(my_list)
return sum/len(my_list)
def standard_deviation(my_list):
mean = mean(my_list)
temp = 0
for item in my_list:
temp = temp + ((item - mean) ** 2)
return (temp/len(my_list))**0.5

This seems like a nice place to use list comprehension for brevity's sake.
def mean(l):
return sum(l) / len(l)
def stdev(l):
# Get the mean of the list
m = mean(l)
# Subtract the mean from each item and square the result
# Take the mean from the resulting list
m_of_sqrd = mean([(i-m)**2 for i in l])
# Return the root
return m_of_sqrd ** 0.5
inp = input()
values = [int(item) for item in inp.split()]
print(mean(values))
print(stdev(values))

Related

How to define a variable in a function when calling the function?

When I run my code I get “NameError: name ‘mean’ is not defined”. I get this error when I try to call my function “calculateVariance(mean, nums)”. I can not seem to figure out how I can define ‘mean’ without having to changing my “calculateVariance(mean, nums)” function.. if that makes sense..
Here is the code:
import math
import matplotlib.pyplot as plt
def readFile(file_name):
with open('data_1.txt', 'r') as DataOne:
nums = DataOne.read()
print(nums)
return nums
def calculateMean(nums):
sumOfNums = 0
for i in range(len(nums)):
sumOfNums += i
mean = sumOfNums//len(nums)
print("The mean is : ", mean)
return mean
def calculateVariance(mean, nums):
squaredDifferences = 0
for number in nums:
difference =mean - number
squaredDiff = difference ** 2
squaredDifferences += squaredDiff
variance = squaredDifferences // (len(nums)-1)
print(" The variance is : ", variance)
return variance
def calculateSD(variance):
square_root = math.sqrt(number)
StandDev = square_root(variance)
print("Standard Deviation is : ", StandDev)
return StandDev
def showHistogram(nums):
num_bins = 10
plt.hist(listOfNums, num_bins)
plt.show()
nums = readFile('data_1.txt')
calculateMean(nums)
calculateVariance(mean, nums)
calculateSD(variance)
When you are calling the function which returns mean, you are not actually saving it. Therefore it cannot be used when calling a different function.
Try saving it as mean_result or similar to use for later (avoiding naming clashes).
You should store mean result from function calculateMean(nums) in a variable. You need to do it for another function as well.
import math
import matplotlib.pyplot as plt
def readFile(file_name):
with open('data_1.txt', 'r') as DataOne:
nums = DataOne.readlines()
print(nums)
return nums
def calculateMean(nums):
sumOfNums = 0
for i in range(len(nums)):
sumOfNums += i
mean = sumOfNums//len(nums)
print("The mean is : ", mean)
return mean
def calculateVariance(mean, nums):
squaredDifferences = 0
for number in nums:
if number != '':
difference = mean - int(number.replace('\n', ''))
squaredDiff = difference ** 2
squaredDifferences += squaredDiff
variance = squaredDifferences // (len(nums)-1)
print(" The variance is : ", variance)
return variance
def calculateSD(variance):
square_root = math.sqrt(number)
StandDev = math.sqrt(variance)
print("Standard Deviation is : ", StandDev)
return StandDev
def showHistogram(nums):
num_bins = 10
plt.hist(listOfNums, num_bins)
plt.show()
nums = readFile('data_1.txt')
mean = calculateMean(nums)
variance = calculateVariance(mean, nums)
stdev = calculateSD(variance)

Summing results from a monte carlo

I am trying to sum the values in the 'Callpayoff' list however am unable to do so, print(Callpayoff) returns a vertical list:
0
4.081687878300656
1.6000410648454846
0.5024316862043037
0
so I wonder if it's a special sublist ? sum(Callpayoff) does not work unfortunately. Any help would be greatly appreciated.
def Generate_asset_price(S,v,r,dt):
return (1 + r * dt + v * sqrt(dt) * np.random.normal(0,1))
def Call_Poff(S,T):
return max(stream[-1] - S,0)
# initial values
S = 100
v = 0.2
r = 0.05
T = 1
N = 2 # number of steps
dt = 0.00396825
simulations = 5
for x in range(simulations):
stream = [100]
Callpayoffs = []
t = 0
for n in range(N):
s = stream[t] * Generate_asset_price(S,v,r,dt)
stream.append(s)
t += 1
Callpayoff = Call_Poff(S,T)
print(Callpayoff)
plt.plot(stream)
Right now you're not appending values to a list, you're just replacing the value of Callpayoff at each iteration and printing it. At each iteration, it's printed on a new line so it looks like a "vertical list".
What you need to do is use Callpayoffs.append(Call_Poff(S,T)) instead of Callpayoff = Call_Poff(S,T).
Now a new element will be added to Callpayoffs at every iteration of the for loop.
Then you can print the list with print(Callpayoffs) or the sum with print(sum(Callpayoffs))
All in all the for loop should look like this:
for x in range(simulations):
stream = [100]
Callpayoffs = []
t = 0
for n in range(N):
s = stream[t] * Generate_asset_price(S,v,r,dt)
stream.append(s)
t += 1
Callpayoffs.append(Call_Poff(S,T))
print(Callpayoffs,"sum:",sum(Callpayoffs))
Output:
[2.125034975231003, 0] sum: 2.125034975231003
[0, 0] sum: 0
[0, 0] sum: 0
[0, 0] sum: 0
[3.2142923036024342, 4.1390018820809615] sum: 7.353294185683396

Copy float values from within lists of lists

I do apologize if I'm not looking in the right places, but I cannot for the life of me figure out how to get a value from say
list[[1,2,3][4,5,6.01]] , list[1][2] integrated into code as anything but a list.
import random
fruits = [
['mango',7],
['apple',4],
['kiwi',6],
['grape',12],
['pear',3]
]
#Finding Probability
def setup():
fsum = 0;
prob = 0;
i = 0
#Finding the sum
while i < len(fruits):
fsum += fruits[i][1]
i += 1
i = 0
#Calculating Probability
while i < len(fruits):
prob = [fruits[i][1] / fsum]
fruits[i].append(prob)
i += 1
print(fsum)
print(fruits)
setup()
def pick(x):
rand = random.random()
index = 0
while rand > 0:
#How do I get the value of the float in the list from the next line
#(fruits[index][2])
#to be stored in a variable that I can plug into this.
#rand = rand - (var)
index+=1
pick (fruits)
Any feedback would be greatly appreciated.
Your problem is this line:
prob = [fruits[i][1] / fsum]
You are defining prob to be a list with one value, just eliminate the unnecessary list, e.g.:
prob = fruits[i][1] / fsum
Then fruits[index][2] will be the probability.
You should consider replacing your while loops with for loops, e.g.:
while i < len(fruits):
fsum += fruits[i][1]
i += 1
i = 0
Is equivalent to:
for fruit in fruits:
fsum += fruit[1]
Which could be be accomplished with a generator expression:
fsum = sum(fruit[1] for fruit in fruits)
But if what you are looking to do is just pick the fruit based on the relative weights (fruits[i][1]) then there is an easier way to do this in Py3.6, without the setup(), e.g.:
def pick(fruits):
items, weights = zip(*fruits)
return random.choices(items, weights)[0]
Prior to Py3.6 you could do:
def pick(fruits):
return random.choice([f for fruit in fruits for f in [fruit[0]]*fruit[1]])
Just access the first item of the list/array, using the index access and the index 0:
var = fruits[index][2][0]

Summation from sub list

If n = 4, m = 3, I have to select 4 elements (basically n elements) from a list from start and end. From below example lists are [17,12,10,2] and [2,11,20,8].
Then between these two lists I have to select the highest value element and after this the element has to be deleted from the original list.
The above step has to be performed m times and take the summation of the highest value elements.
A = [17,12,10,2,7,2,11,20,8], n = 4, m = 3
O/P: 20+17+12=49
I have written the following code. However, the code performance is not good and giving time out for larger list. Could you please help?
A = [17,12,10,2,7,2,11,20,8]
m = 3
n = 4
scoreSum = 0
count = 0
firstGrp = []
lastGrp = []
while(count<m):
firstGrp = A[:n]
lastGrp = A[-n:]
maxScore = max(max(firstGrp), max(lastGrp))
scoreSum = scoreSum + maxScore
if(maxScore in firstGrp):
A.remove(maxScore)
else:
ai = len(score) - 1 - score[::-1].index(maxScore)
A.pop(ai)
count = count + 1
firstGrp.clear()
lastGrp.clear()
print(scoreSum )
I would like to do that this way, you can generalize it later:
a = [17,12,10,2,7,2,11,20,8]
a.sort(reverse=True)
sums=0
for i in range(3):
sums +=a[i]
print(sums)
If you are concerned about performance, you should use specific libraries like numpy. This will be much faster !
A = [17,12,10,2,7,11,20,8]
n = 4
m = 3
score = 0
for _ in range(m):
sublist = A[:n] + A[-n:]
subidx = [x for x in range(n)] + [x for x in range(len(A) - n, len(A))]
sub = zip(sublist, subidx)
maxval = max(sub, key=lambda x: x[0])
score += maxval[0]
del A[maxval[1]]
print(score)
Your method uses a lot of max() calls. Combining the slices of the front and back lists allows you to reduce the amounts of those max() searches to one pass and then a second pass to find the index at which it occurs for removal from the list.

Copy an array of floats to array of Strings

#ADD STRING MATRIX AND NUM MATRIX Fraction(3).limit_denominator(10)from fractions import Fraction
#ONLY WORKS FOR SQUARE ONES RIGHT NOW!
from fractions import Fraction
def make1(nm,x):
if nm[x][x]!=1:
print("Divide R1 by ",Fraction(nm[x][x]).limit_denominator(10))
tempr = multiply(nm[x],1/nm[x][x])
nm[x] = tempr
return nm
def convert(n):
try:
return float(n)
except ValueError:
num, denom = n.split('/')
return float(num) / float(denom)
def convertm(m):
lm = len(m)
lx = len(m[0])
tempn = [0]*lx
temps = [[]]*lm
print(temps)
cnt = 0
for x in m:
tempn = x
for n in x:
temps[cnt].append(str(Fraction(n).limit_denominator(10)))
print(n)
cnt+=1
print(temps)
def mprint(matrix):
s = [[str(e) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print('\n'.join(table))
def subtract(r1,r2): #r1-r2
tempr = [0]*len(r1)
for x in range (0,len(r1)):
tempr[x] = r1[x]-r2[x]
return tempr
def multiply(r1,n):
tempr = [0]*len(r1)
for x in range (0,len(r1)):
tempr[x] = r1[x]*n
return tempr
def ans(nm):
end = len(nm[0])
cnt = 0
for x in nm:
cnt+=1
print("X",cnt,"=",x[end-1])
equ = int(input("How many equasions are in the linear system? "))
#unk = int(input("How many unkowns are in the linear system? "))
nm = [0] * equ
sm = [0] * equ
for x in range (0,equ):
tempinput = input("Please enter line "+str(x+1)+" of the matrix: ")
templist = [convert(n) for n in tempinput.split()]
nm[x] = templist
sm[x] = tempinput.split()
mprint(nm)
nm = make1(nm,0)
mprint(nm)
for p in range (0,equ-1):
for x in range (p,equ-1):
print("Subtract ",Fraction(nm[x+1][p]).limit_denominator(10),"*",p+1,"by",p+2)
tempr = multiply(nm[p],nm[x+1][p])
nm[x+1] = subtract(tempr,nm[x+1])
print("FIRST X: ",x,"P",x)
mprint(nm)
nm = make1(nm,p+1)
mprint(nm)
#GOIN BACK
for p in range (0,equ-1):
for x in range (0,equ-(p+1)):
print("Subtract ",x,"by",Fraction(nm[x][2-p]).limit_denominator(10),"*",3)
tempr = multiply(nm[2-p],nm[x][2-p])
nm[x]= subtract(nm[x],tempr)
print("SECOND X: ",x,"P",x)
mprint(nm)
ans(nm)
##or x in range (0,equ):
# print()
#g = nm[1][0]-1
#print("")
#tempr = multiply(nm[0],g/nm[0][0])
#nm[0]=tempr
#tempr = subtract(nm[1],nm[0])
#nm[0] = tempr
Pastebin of my code
Ok so where my actual problem is in the unimplemented (because I couldn't get it working) def convertm. What this is supposed to do is take the matrix with numbers (nm) and take every value and convert it into a string as fractions (x/x) if needed and store it in the matrix of strings (sm).
Here is that segment of code I am referencing...
def convertm(m):
lm = len(m)
lx = len(m[0])
tempn = [0]*lx
temps = [[]]*lm
print(temps)
cnt = 0
for x in m:
tempn = x
for n in x:
temps[cnt].append(str(Fraction(n).limit_denominator(10)))
print(n)
cnt+=1
print(temps)
I added some prints in order to try and test what the heck was going on during it. I am getting an output of just the last row being repeated through all rows. I think I don't have a return statement currently just because I have been trying get this to work. Ok so for an example if an array is imported that is...
[ [1,2,3],
[4,5,6],
[7,8,9] ]
It will output (set temps to)
[ ['7','8','9'],
['7','8','9'],
['7','8','9'] ]
I want it to output (set temps to)
[ ['1','2','3'],
['4','5','6'],
['7','8','9'] ]
Also I am using Python 3.3.1
(probably should upgrade to 3.3.3 but that is not what we are discussing!)
I have absolutely no idea why it is doing this and any little bit of help would very appreciated!
THANK YOU
I also apologize if this formatting is horrible I am new to this and I copy/pasted this from another forum I am very desperate to know what is going on here.
The line
temps = [[]]*lm
makes a list of list, where each sublist points to the same list in memory. So, if you modify one, you modify them all. This is why you are seeing the behavior you are seeing.
Change it to
temps = [[] for _ in range(lm)] # xrange on python2
to get different sublists.

Categories

Resources