Beispiel #1
0
// NewWork method
func NewWork(rCount, frame, limit, hIn int, trainType, symbol string) *Work {
	result := new(Work)
	result.symbol = symbol
	result.Limit = limit
	result.frame = frame
	result.rangeCount = rCount
	result.trainType = trainType
	result.hIn = hIn
	result.mlp = neural.MlpCreate1(frame, frame, hIn)
	result.loopCount = 0
	result.ranges = nil
	result.resultRepo = repository.NewResultDataRepo(limit, true, symbol, nil)
	result.effRepo = repository.NewEfficiencyRepo(trainType, symbol, int32(rCount), int32(limit), int32(frame), nil)
	log.Printf("Created new work - Symbol: %s, ResultDataRepo length: %d, EfficiencyRepo length: %d\n", result.symbol, result.resultRepo.Len(), result.effRepo.Len())

	return result
}
Beispiel #2
0
func initializeRepo(r *http.Request) {
	_rateRepo = repository.New(historyLimit+5, false, r)
	_rates = _rateRepo.GetAll()

	_rubResultRepo = repository.NewResultDataRepo(historyLimit, false, "RUB", r)
	_rubEffRepo = repository.NewEfficiencyRepo("L-BFGS", "RUB", 6, 20, 5, r)

	_eurResultRepo = repository.NewResultDataRepo(historyLimit, false, "EUR", r)
	_eurEffRepo = repository.NewEfficiencyRepo("L-BFGS", "EUR", 6, 20, 5, r)

	_gbpResultRepo = repository.NewResultDataRepo(historyLimit, false, "GBP", r)
	_gbpEffRepo = repository.NewEfficiencyRepo("L-BFGS", "GBP", 6, 20, 5, r)

	_chfResultRepo = repository.NewResultDataRepo(historyLimit, false, "CHF", r)
	_chfEffRepo = repository.NewEfficiencyRepo("L-BFGS", "CHF", 6, 20, 5, r)

	_cnyResultRepo = repository.NewResultDataRepo(historyLimit, false, "CNY", r)
	_cnyEffRepo = repository.NewEfficiencyRepo("L-BFGS", "CNY", 6, 20, 5, r)

	_jpyResultRepo = repository.NewResultDataRepo(historyLimit, false, "JPY", r)
	_jpyEffRepo = repository.NewEfficiencyRepo("L-BFGS", "JPY", 6, 20, 5, r)

	_initialized = true
}
Beispiel #3
0
func (f *fetchRatesWorkItem) Process(rates []entities.Rate, r *http.Request) (int, error) {
	// prepare income data
	var rawSource []entities.Rate
	if len(rates) > f.Limit+1 {
		rawSource = rates[len(rates)-f.Limit-1:]
	}

	_time := rawSource[len(rawSource)-1].ID
	source, isValid := extractFloatSet(rawSource, f.symbol)
	sourceLength := len(source)

	// assess previous prediction
	if f.ranges != nil && len(f.ranges) > 0 && sourceLength > 1 {
		class, err := statistic.DetectClass(f.ranges, source[sourceLength-1]/source[sourceLength-2])
		if err != nil {
			return -1, err
		}
		resultRepo := repository.NewResultDataRepo(f.Limit, true, f.symbol, r)
		effRepo := repository.NewEfficiencyRepo(f.trainType, f.symbol, int32(f.rangeCount), int32(f.Limit), int32(f.frame), nil)
		if last, found := resultRepo.Get(rawSource[sourceLength-2].ID); found {
			eff, _ := effRepo.GetLast()
			last.Result = int32(class)
			if last.Prediction == int32(class) {
				eff.LastSD = append(eff.LastSD, 1)
			} else {
				rcHalf := float32(f.rangeCount-1) / 2
				fClass := float32(class)
				fPrediction := float32(last.Prediction)
				if (rcHalf < fClass && rcHalf < fPrediction) || (rcHalf > fClass && rcHalf > fPrediction) {
					eff.LastSD = append(eff.LastSD, 1)
				} else {
					eff.LastSD = append(eff.LastSD, 0)
				}
			}
			if len(eff.LastSD) > 100 {
				eff.LastSD = eff.LastSD[len(eff.LastSD)-100:]
			}

			eff.Timestamp = last.Timestamp

			if err := resultRepo.Sync(last); err != nil {
				return -1, err
			}
			if err := effRepo.Sync(eff); err != nil {
				return -1, err
			}
		}
	}

	if !isValid {
		f.ranges = nil
		return -1, errors.New("no activity detected")
	}

	// retrain mlp
	if f.loopCount > f.frame || f.ranges == nil {
		var err error
		if f.ranges, err = statistic.CalculateEvenRanges2(source, f.rangeCount); err != nil {
			f.ranges = nil
			return -1, err
		}

		// retrain mlp
		classes, err := statistic.CalculateClasses(source, f.ranges)
		if err != nil {
			return -1, err
		}

		train := make([][]float64, 1)
		train[0] = convertArrayToFloat64(classes)

		switch f.trainType {
		case TTLbfgs:
			info, _, err := neural.MlpTrainLbfgs(f.mlp, &train, 1, 0.001, 2, 0.01, 0)
			if err != nil {
				return -1, err
			} else if info != 2 {
				return -1, fmt.Errorf("MlpTrainLbfgs error info param: %d.", info)
			}
		default:
			return -1, errors.New("unknowen trainig type")
		}

		f.loopCount = 0
	}

	if f.ranges != nil {
		f.loopCount++
		source, err := statistic.CalculateClasses(source[len(source)-f.frame-1:], f.ranges)
		if err != nil {
			return -1, err
		}
		// process
		process := convertArrayToFloat64(source)
		rawResult := neural.MlpProcess(f.mlp, &process)
		result := entities.ResultData{
			RangesCount: int32(f.rangeCount),
			TrainType:   f.trainType,
			Limit:       int32(f.Limit),
			Step:        int32(f.frame),
			Symbol:      f.symbol,
			Timestamp:   _time,
			Source:      convertArrayToInt32(source),
			Prediction:  int32(math.Floor((*rawResult)[0] + .5)),
			Result:      -1}
		resultRepo := repository.NewResultDataRepo(f.Limit, true, f.symbol, r)
		if err := resultRepo.Push(result); err != nil {
			return -1, err
		}
		if rawResult != nil || len(*rawResult) > 0 {
			return int(result.Prediction), nil
		}
	}
	return -1, nil
}