Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Standard Deviation and Cancellation Problem Credits: Computer Problem 1.12 from

ID: 3753743 • Letter: S

Question

Standard Deviation and Cancellation Problem Credits: Computer Problem 1.12 from "Scientific Computing: An Introductory Survey" by Michael T Heath Write a program to compute the mean i and standard deviation of a finite sequence xi. Your program should accept a vector x of dimension n as input and produce the standard deviation of the sequence as output. For the standard deviation, try both the two-pass formula and the one-pass formula 1/2 and compare the results for an input sequence of your choice Then, devise an input data sequence that dramatically illustrates the numerical difference between these two mathematically equivalent formulas. (Caution: Beware of taking the square root of a negative number.) The setup code gives the following variables Your code snippet should define the following variables Name sequence var seg tp var_seq_op bad_sequence 1D numpy array Type 1D numpy array floating point number floating point number Description first sequence, at least size 10 result of using the two pass formula on sequence result of using the one pass formula on sequence sequence that leads to different results, at least size 10

Explanation / Answer

code:

import math

def CalculateStandardDeviation(sequence):
n=len(sequence);
total=float(0.0);
for i in sequence:
total=total+float(i);
mean=float(total/float(n));
var_seq_tp=float(0.0);
var_seq_op=float(0.0);
for i in sequence:
var_seq_op=var_seq_op+math.pow(i,2)-n*math.pow(mean,2);
var_seq_tp=var_seq_tp+math.pow(i-mean,2);
var_seq_op=var_seq_op/(n-1);
var_seq_tp=var_seq_tp/(n-1);
if(var_seq_op<0 or var_seq_tp<0):
bad_sequence=sequence;
print("Its bad sequence");
else:
var_seq_op=math.sqrt(var_seq_op);
var_seq_tp=math.sqrt(var_seq_tp);
print("var_seq_op : ",var_seq_op);
print("var_seq_tp : ",var_seq_tp);

def main():
A=[23.434,434.434,454.3232,43.323,12.4343,3.4343,6.665,6.66,4,42,35,54,45,65];
CalculateStandardDeviation(A)

main()
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote