<commit_before>package com.milesoc.sa.models

import com.stackmob.core.rest.{ResponseToProcess, ProcessedAPIRequest}
import com.stackmob.sdkapi._
import scala.Some
import java.util.{List => JList}
import scala.List
import scala.Some
import scala.collection.JavaConverters._
import com.milesoc.sa.core.Reader

/**
 * Created by IntelliJ IDEA.
 * User: miles
 * Date: 2/10/13
 */
case class Market(id: String,
                  commodity: String,
                  price: Long,
                  history: List[Long]) {

  //price history should assume earlier dates come first
  def calculateTrend: Double = {
    if (calculateChanges.length == 0) return 0.0
    var currentDiscount = Market.TREND_DISCOUNT
    var changesSum = 0.0
    var discountsSum = 0.0
    calculateChanges foreach(change => {
      changesSum += currentDiscount * change
      discountsSum += currentDiscount
      currentDiscount *= Market.TREND_DISCOUNT
    })
    changesSum/discountsSum
  }

  /**
   * Most recent price changes come first
   */
  def calculateChanges: List[Double] = calculateChanges(List[Double](), history)

  private def calculateChanges(changes: List[Double], prices: List[Long]): List[Double] = prices match {
    case (p1 :: (p2 :: tl)) => {
      val newPrice = p2.toDouble
      val oldPrice = p1.toDouble
      val change = (newPrice-oldPrice)/((oldPrice+newPrice)/2) //economic % change
      val filtered = if (change == Double.NaN) 0 else change
      calculateChanges(filtered :: changes, p2 :: tl)
    }
    case _ => changes
  }
}

object Market {

  final val TREND_DISCOUNT = 0.8<commit_msg>NaN sucks<commit_after>package com.milesoc.sa.models

import com.stackmob.core.rest.{ResponseToProcess, ProcessedAPIRequest}
import com.stackmob.sdkapi._
import scala.Some
import java.util.{List => JList}
import scala.List
import scala.Some
import scala.collection.JavaConverters._
import com.milesoc.sa.core.Reader

/**
 * Created by IntelliJ IDEA.
 * User: miles
 * Date: 2/10/13
 */
case class Market(id: String,
                  commodity: String,
                  price: Long,
                  history: List[Long]) {

  //price history should assume earlier dates come first
  def calculateTrend: Double = {
    if (calculateChanges.length == 0) return 0.0
    var currentDiscount = Market.TREND_DISCOUNT
    var changesSum = 0.0
    var discountsSum = 0.0
    calculateChanges foreach(change => {
      changesSum += currentDiscount * change
      discountsSum += currentDiscount
      currentDiscount *= Market.TREND_DISCOUNT
    })
    changesSum/discountsSum
  }

  /**
   * Most recent price changes come first
   */
  def calculateChanges: List[Double] = calculateChanges(List[Double](), history)

  private def calculateChanges(changes: List[Double], prices: List[Long]): List[Double] = prices match {
    case (p1 :: (p2 :: tl)) => {
      val newPrice = p2.toDouble
      val oldPrice = p1.toDouble
      val change = (newPrice-oldPrice)/((oldPrice+newPrice)/2) //economic % change
      val filtered = if (change.isNaN) 0 else change
      calculateChanges(filtered :: changes, p2 :: tl)
    }
    case _ => changes
  }
}

object Market {

  final val TREND_DISCOUNT = 0.8
{'fraction_non_alphanumeric': 0.10651629072681704, 'fraction_numerical': 0.013157894736842105, 'mean_word_length': 3.041772151898734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '128927', 'n_tokens_mistral': 1007, 'n_tokens_neox': 964, 'n_words': 255}