Scott Wolf

Greenhorn

Posts: 4

posted 1 year ago

I'm supposed to write a generic class MyMathClass with at type parameter T where T is a numeric object.

There's no compiler errors or anything. It's just my standard deviation doesn't match with the expectation of standard deviation.

With this program, I get 2.872281323690143 while it is supposed to be 3.0276503540974917. I'm not sure if it is part of my calculation problem or I forget to add something to the program?

There's no compiler errors or anything. It's just my standard deviation doesn't match with the expectation of standard deviation.

With this program, I get 2.872281323690143 while it is supposed to be 3.0276503540974917. I'm not sure if it is part of my calculation problem or I forget to add something to the program?

posted 1 year ago

I'm no statistician, but I sort of recall that there's two kinds of standard deviation, one of them involves dividing by N and the other involves dividing by N-1. The latter would produce a larger result but you've used the former, so perhaps that's the source of your discrepancy.

(Any real statisticians out there, it's okay if you're still laughing at me.)

(Any real statisticians out there, it's okay if you're still laughing at me.)

Scott Wolf

Greenhorn

Posts: 4

posted 1 year ago

Oh right. I solved the issue. Thanks for reminding me about that there's two kinds of standard deviation. :)

- 1

Paul Clapham wrote:I'm no statistician, but I sort of recall that there's two kinds of standard deviation, one of them involves dividing by N and the other involves dividing by N-1. The latter would produce a larger result but you've used the former, so perhaps that's the source of your discrepancy.

(Any real statisticians out there, it's okay if you're still laughing at me.)

Oh right. I solved the issue. Thanks for reminding me about that there's two kinds of standard deviation. :)

Campbell Ritchie

Marshal

Posts: 55772

163

posted 1 year ago

Also, don't declare your method to take an

What if I had an instance of

`ArrayList`.What if I had an instance of

`LinkedList`or whatever type`Arrays.asList()`returns or`MyFancyCustomBetterThanAnyOtherList`? I wouldn't be able to use your class easily.OCPJP 6, 7, 8, OCMJD 6

posted 1 year ago

It's been a few geological epochs for me, but I seem to remember you use

Winston

Paul Clapham wrote:I'm no statistician, but I sort of recall that there's two kinds of standard deviation, one of them involves dividing by N and the other involves dividing by N-1. The latter would produce a larger result but you've used the former, so perhaps that's the source of your discrepancy.

It's been a few geological epochs for me, but I seem to remember you use

`n-1`for

*sample*variances (and therefore SD's). I'm sure Piet'll enlighten us if he's around.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Campbell Ritchie

Marshal

Posts: 55772

163

Tim Moores

Saloon Keeper

Posts: 3893

91

posted 1 year ago

You might also consider using the BigDecimal class instead of doubles, depending on how much accuracy is desired. It's not going to make much difference for 10 elements, but for much larger datasets, the floating point inaccuracies will eventually add up to noticeable amounts.

posted 1 year ago

Hmmm. Maybe, but since SD is a value in the same unit as the input, and the variance sum is divided by n (or n-1) anyway (and

Winston

Tim Moores wrote:You might also consider using the BigDecimal class instead of doubles, depending on how much accuracy is desired. It's not going to make much difference for 10 elements, but for much larger datasets, the floating point inaccuracies will eventually add up to noticeable amounts.

Hmmm. Maybe, but since SD is a value in the same unit as the input, and the variance sum is divided by n (or n-1) anyway (and

`(x - x̄)`reduces the likelihood of inaccuracy due to the

*magnitude*of the values), I doubt it'll make too much difference as long as you don't assume that the value is accurate to more than a few decimal places.

Winston