# Ryan Martin
# macid: abcd123
# student number: #######
# July 12, 2011
#
# This is a solution for Major Assignment 2 (Lab 06).

from math import sqrt


print """\

This program is a solution to Major Assignment 2 (Lab 06).

"""

#INPUTS
xFileName = raw_input("Enter the name of the input file containing x-values: ")
xFile = open(xFileName, "r")
xList = xFile.read().split()
xFile.close()

yFileName = raw_input("Enter the name of the input file containing y-values: ")
yFile = open(yFileName, "r")
yList = yFile.read().split()
yFile.close()
print 

#CALCULATION
listSize = len(xList)
sumY = 0
sumXY = 0
sumYY = 0
sumXXY = 0
for i in range(listSize): #from 0 to n - 1
    xList[i] = float(xList[i])
    yList[i] = float(yList[i])
    
    sumY = sumY + yList[i]
    sumXY = sumXY + (xList[i] * yList[i])
    sumYY = sumYY + yList[i]**2
    sumXXY = sumXXY + (yList[i] * xList[i]**2)

sigma = sqrt(((sumXXY * sumY) - sumXY**2) / (sumY**2 - sumYY))

#OUTPUT

print "The standard deviation, sigma =", sigma
print ""


'''
Design Question

There are four individual sums in the formula for sigma, 
since the sum of y values is used twice.
In this case, it is possible to calculate sigma using 
just one loop. None of the sums are dependent on the 
final results of any other sums, so they can all
be calculated simultaneously.

Test Plan

The following is given in XValues1.txt:
-19
-42
3
5
22
-7
-21
-33
-42
The following is given in YValues1.txt:
24
13
45
17
18
37
13
0
35
The following is given in XValues2.txt:
-4.41
25.17
-29.20
-50.21
35.36
47.7
-11.43
-47.41
-19.6
31.30
-5.27
The following is given in YValues2.txt:
56.70
21.58
32.66
65.55
8.58
48.19
60.14
80.54
38.60
100.82
64.30
The following is given in XValues3.txt:
35.36
47.7
-11.43
-47.41
The following is given in YValues3.txt:
0
0
0
1

Test: 1
Input: [XValues1.txt, YValues1.txt]
Expected Output: [21.8873355202]

Test: 2
Input: [XValues2.txt, YValues2.txt]
Expected Output: [34.41376075]

Test: 3
Input: [XValues3.txt, YValues3.txt]
Expected Output: [ZeroDivisionError]

Test: 4
Input: [Invalid File Name]
Expected Output: [IOError]

Test: 5
Input: [Empty files]
Expected Output: [error - zero division]

'''
