Skip to content

Commit

Permalink
calculation standard deviation and median
Browse files Browse the repository at this point in the history
  • Loading branch information
hehu80 committed Sep 13, 2024
1 parent 1fc5bae commit fe7b795
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Test

on:
pull-request:
push:
branches-ignore:
main
workflow_call:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/render v1.0.3
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/montanaflynn/stats v0.7.1
)

require github.com/ajg/form v1.5.1 // indirect
20 changes: 13 additions & 7 deletions internal/resource/emissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/montanaflynn/stats"
"net/http"
)

Expand All @@ -14,23 +15,28 @@ type EmissionResource struct {
}

type airPollutionResponse struct {
Average float64 `json:"average"`
Median float64 `json:"median"`
Variance float64 `json:"variance"`
Average float64 `json:"average"`
Median float64 `json:"median"`
StandardDeviation float64 `json:"standard_deviation"`
}

func newAirPollutionResponse(emissions []*model.Emissions, f func(emission *model.Emissions) float64) airPollutionResponse {
total := 0.0
values := make([]float64, len(emissions))

for _, emission := range emissions {
for i, emission := range emissions {
current := f(emission)
values[i] = current
total = total + current
}

median, _ := stats.Median(values)
standardDeviation, _ := stats.StandardDeviation(values)

return airPollutionResponse{
Average: total / float64(len(emissions)),
Median: 0.0, // TODO
Variance: 0.0, // TODO
Average: total / float64(len(emissions)),
Median: median,
StandardDeviation: standardDeviation,
}
}

Expand Down

0 comments on commit fe7b795

Please sign in to comment.