added a weighted average function

Added a weighted average function. I'm not 100% familiar with all the code, so though this seems to work well enough for me, It could certainly benefit from some code review.