149 lines
4.6 KiB
Go
149 lines
4.6 KiB
Go
// Package education ranks public Arizona study options using transparent,
|
|
// balanced access signals. It does not claim to rank academic quality.
|
|
package education
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"sort"
|
|
|
|
"my-homelab-configs/cmd/nextpath/internal/opportunity"
|
|
)
|
|
|
|
var ErrZIPUnavailable = errors.New("ZIP coordinate is unavailable")
|
|
|
|
type RankedOption struct {
|
|
opportunity.EducationOption
|
|
DistanceMiles int `json:"distance_miles"`
|
|
ProximityScore int `json:"proximity_score"`
|
|
AffordabilityScore int `json:"affordability_score"`
|
|
AccessScore int `json:"access_score"`
|
|
ProgramStrength int `json:"program_strength_score"`
|
|
BalancedScore int `json:"balanced_score"`
|
|
CostLabel string `json:"cost_label"`
|
|
AccessLabel string `json:"access_label"`
|
|
StrengthLabel string `json:"strength_label"`
|
|
}
|
|
|
|
// Rank returns options that balance proximity, in-state tuition, admissions
|
|
// accessibility, and a public-data program-strength signal. Strength combines
|
|
// exact-program completion volume with the institution-wide completion rate;
|
|
// it is not a third-party rating or guarantee of program quality.
|
|
func Rank(snapshot opportunity.Snapshot, onet, zipCode string, limit int) ([]RankedOption, error) {
|
|
origin, ok := snapshot.ZIPCoordinates[zipCode]
|
|
if !ok {
|
|
return nil, ErrZIPUnavailable
|
|
}
|
|
options := snapshot.EducationOptions[onet]
|
|
if limit <= 0 || len(options) == 0 {
|
|
return nil, nil
|
|
}
|
|
maxCompletions := 1
|
|
for _, option := range options {
|
|
if option.ProgramCompletions > maxCompletions {
|
|
maxCompletions = option.ProgramCompletions
|
|
}
|
|
}
|
|
ranked := make([]RankedOption, 0, len(options))
|
|
for _, option := range options {
|
|
distance := int(math.Round(haversine(origin.Latitude, origin.Longitude, option.Latitude, option.Longitude)))
|
|
proximity := proximityScore(distance)
|
|
affordability, costLabel := affordabilityScore(option.AnnualInStateTuition)
|
|
access, accessLabel := admissionScore(option)
|
|
volume := 40 + int(math.Round(60*math.Sqrt(float64(option.ProgramCompletions)/float64(maxCompletions))))
|
|
completion := option.InstitutionCompletionRatePercent
|
|
if completion <= 0 {
|
|
completion = 50
|
|
}
|
|
strength := int(math.Round(float64(volume)*0.6 + float64(completion)*0.4))
|
|
balanced := int(math.Round(float64(proximity)*0.25 + float64(affordability)*0.25 + float64(access)*0.20 + float64(strength)*0.30))
|
|
ranked = append(ranked, RankedOption{
|
|
EducationOption: option, DistanceMiles: distance, ProximityScore: proximity,
|
|
AffordabilityScore: affordability, AccessScore: access, ProgramStrength: strength,
|
|
BalancedScore: balanced, CostLabel: costLabel, AccessLabel: accessLabel,
|
|
StrengthLabel: strengthLabel(strength),
|
|
})
|
|
}
|
|
sort.SliceStable(ranked, func(i, j int) bool {
|
|
if ranked[i].BalancedScore == ranked[j].BalancedScore {
|
|
return ranked[i].DistanceMiles < ranked[j].DistanceMiles
|
|
}
|
|
return ranked[i].BalancedScore > ranked[j].BalancedScore
|
|
})
|
|
if limit < len(ranked) {
|
|
ranked = ranked[:limit]
|
|
}
|
|
return ranked, nil
|
|
}
|
|
|
|
func haversine(lat1, lon1, lat2, lon2 float64) float64 {
|
|
const earthMiles = 3958.8
|
|
toRadians := math.Pi / 180
|
|
dLat := (lat2 - lat1) * toRadians
|
|
dLon := (lon2 - lon1) * toRadians
|
|
a := math.Sin(dLat/2)*math.Sin(dLat/2) + math.Cos(lat1*toRadians)*math.Cos(lat2*toRadians)*math.Sin(dLon/2)*math.Sin(dLon/2)
|
|
return earthMiles * 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
|
}
|
|
|
|
func proximityScore(miles int) int {
|
|
switch {
|
|
case miles <= 15:
|
|
return 100
|
|
case miles <= 40:
|
|
return 80
|
|
case miles <= 80:
|
|
return 60
|
|
case miles <= 150:
|
|
return 40
|
|
default:
|
|
return 20
|
|
}
|
|
}
|
|
|
|
func affordabilityScore(tuition int) (int, string) {
|
|
switch {
|
|
case tuition <= 0:
|
|
return 50, "tuition not reported"
|
|
case tuition <= 6000:
|
|
return 100, "lower-cost"
|
|
case tuition <= 12000:
|
|
return 80, "moderate-cost"
|
|
case tuition <= 20000:
|
|
return 60, "upper-middle cost"
|
|
case tuition <= 30000:
|
|
return 40, "higher-cost"
|
|
default:
|
|
return 20, "highest-cost tier"
|
|
}
|
|
}
|
|
|
|
func admissionScore(option opportunity.EducationOption) (int, string) {
|
|
if option.AdmissionBasis == "open admissions" {
|
|
return 100, "open admissions"
|
|
}
|
|
rate := option.AdmissionRatePercent
|
|
switch {
|
|
case rate >= 75:
|
|
return 90, "broad access"
|
|
case rate >= 50:
|
|
return 70, "moderate access"
|
|
case rate >= 25:
|
|
return 50, "selective"
|
|
case rate > 0:
|
|
return 30, "highly selective"
|
|
default:
|
|
return 50, "admission rate not reported"
|
|
}
|
|
}
|
|
|
|
func strengthLabel(score int) string {
|
|
switch {
|
|
case score >= 80:
|
|
return "strong public-data signal"
|
|
case score >= 60:
|
|
return "established public-data signal"
|
|
default:
|
|
return "developing or limited-data signal"
|
|
}
|
|
}
|