Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17/18 ST Q1c #381

Open
fwenyin opened this issue Apr 27, 2021 · 6 comments
Open

17/18 ST Q1c #381

fwenyin opened this issue Apr 27, 2021 · 6 comments
Labels
🧠 Finals Q&A Questions related to Finals

Comments

@fwenyin
Copy link

fwenyin commented Apr 27, 2021

how do i go about doing q1c? im not sure how to store the values of each (Xk - μ)^2 as wont the value require μ to be generated first?

@fwenyin fwenyin added the 🧠 Finals Q&A Questions related to Finals label Apr 27, 2021
@Buwoo
Copy link

Buwoo commented Apr 27, 2021

The question didn't state that we are required to complete everything within a single stream pipeline, only criteria is cannot use explicit looping/recursive implementations and data structures.
So I think its easiest (though may not be the most elegant solution) to just use the IntStream average method to get the mean, then if the mean is not empty, you can perform the necessary calculations using the mean value as generated earlier.
Hope this helps!

@jesslynlee
Copy link

jesslynlee commented Apr 27, 2021

hello, this is my attempt.

public static OptionalDouble variance(int[] data) {
	if (data.length < 2) { 
		return OptionalDouble.empty();
	}
	int n = data.length;
	double mean = Arrays.stream(data).asDoubleStream().sum() / n;
	double num = Arrays.stream(data).mapToDouble(x -> (x-mean)*(x-mean)).sum();
	return OptionalDouble.of(num/(n-1));
}

@davidwang98
Copy link

My friends and I found the solution. Here is our working.

public static OptionalDouble variance(int[] data) {
    return OptionalDouble.of(IntStream.of(data)
		.mapToDouble(x -> (double) x) // convert everything to double
		.map(x -> x - IntStream.of(data).average().getAsDouble()) // x - minus average
		.map(x -> x * x) // this is to square the numerator
		.reduce(0, (x,y) -> (x + (y/(data.length - 1))))); // sum everything together
}

@Buwoo
Copy link

Buwoo commented Apr 28, 2021

My friends and I found the solution. Here is our working.

public static OptionalDouble variance(int[] data) {
    return OptionalDouble.of(IntStream.of(data)
		.mapToDouble(x -> (double) x) // convert everything to double
		.map(x -> x - IntStream.of(data).average().getAsDouble()) // x - minus average
		.map(x -> x * x) // this is to square the numerator
		.reduce(0, (x,y) -> (x + (y/(data.length - 1))))); // sum everything together
}

I don't think this method works when there are 0 elements in the data array. It will return OptionalDouble[0.0] instead of OptionalDouble.empty. If there is only 1 element in the data array, it will give out a OptionalDouble[NaN], which isn't ideal either. I think can use an if statement to catch these corner cases

@fwenyin
Copy link
Author

fwenyin commented Apr 28, 2021

thanks all for the help!

@danieltwh
Copy link

danieltwh commented Apr 28, 2021

My friends and I found the solution. Here is our working.

public static OptionalDouble variance(int[] data) {
    return OptionalDouble.of(IntStream.of(data)
		.mapToDouble(x -> (double) x) // convert everything to double
		.map(x -> x - IntStream.of(data).average().getAsDouble()) // x - minus average
		.map(x -> x * x) // this is to square the numerator
		.reduce(0, (x,y) -> (x + (y/(data.length - 1))))); // sum everything together
}

I don't think this method works when there are 0 elements in the data array. It will return OptionalDouble[0.0] instead of OptionalDouble.empty. If there is only 1 element in the data array, it will give out a OptionalDouble[NaN], which isn't ideal either. I think can use an if statement to catch these corner cases

Good catch @Buwoo! We can actually change the last reduce to .reduce((x, y) -> (x + (y/(data.length - 1)))) to handle the corner cases. This way, we also don't need to do OptionalDouble.of(...). The method will return a OptionalDouble.empty if there 0 elements in the data array.

However, this does not handle the case when there is only 1 element in the data array. We should probably check this before running this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧠 Finals Q&A Questions related to Finals
Projects
None yet
Development

No branches or pull requests

5 participants