001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.sandbox.ml.linear.learner.stream.experiments; 031 032import java.io.IOException; 033import java.net.MalformedURLException; 034import java.util.Arrays; 035import java.util.List; 036import java.util.Map; 037 038import org.openimaj.demos.sandbox.ml.linear.learner.stream.IncrementalLearnerFunction; 039import org.openimaj.demos.sandbox.ml.linear.learner.stream.IncrementalLearnerWorldSelectingEvaluator; 040import org.openimaj.demos.sandbox.ml.linear.learner.stream.ModelStats; 041import org.openimaj.demos.sandbox.ml.linear.learner.stream.StockPriceAggregator; 042import org.openimaj.demos.sandbox.ml.linear.learner.stream.twitter.USMFStatusBagOfWords; 043import org.openimaj.demos.sandbox.ml.linear.learner.stream.twitter.USMFTickMongoDBQueryStream; 044import org.openimaj.ml.linear.evaluation.SumLossEvaluator; 045import org.openimaj.ml.linear.learner.BilinearLearnerParameters; 046import org.openimaj.ml.linear.learner.init.FirstValueInitStrat; 047import org.openimaj.ml.linear.learner.init.SingleValueInitStrat; 048import org.openimaj.ml.linear.learner.init.SparseZerosInitStrategy; 049import org.openimaj.tools.twitter.modes.preprocessing.StopwordMode; 050import org.openimaj.twitter.USMFStatus; 051import org.openimaj.util.data.Context; 052import org.openimaj.util.function.Operation; 053import org.openimaj.util.function.context.ContextFunctionAdaptor; 054import org.openimaj.util.stream.window.WindowAverage; 055 056import com.mongodb.ServerAddress; 057 058/** 059 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 060 * 061 */ 062public class FinancialMongoStreamLearningExperiment { 063 /** 064 * @param args 065 * @throws MalformedURLException 066 * @throws IOException 067 */ 068 public static void main(String[] args) throws MalformedURLException, IOException { 069 BilinearLearnerParameters params = new BilinearLearnerParameters(); 070 params.put(BilinearLearnerParameters.ETA0_U, 0.02); 071 params.put(BilinearLearnerParameters.ETA0_W, 0.02); 072 params.put(BilinearLearnerParameters.LAMBDA, 0.001); 073 params.put(BilinearLearnerParameters.BICONVEX_TOL, 0.01); 074 params.put(BilinearLearnerParameters.BICONVEX_MAXITER, 10); 075 params.put(BilinearLearnerParameters.BIAS, true); 076 params.put(BilinearLearnerParameters.ETA0_BIAS, 0.5); 077 params.put(BilinearLearnerParameters.WINITSTRAT, new SingleValueInitStrat(0.1)); 078 params.put(BilinearLearnerParameters.UINITSTRAT, new SparseZerosInitStrategy()); 079 params.put(BilinearLearnerParameters.EXPANDEDUINITSTRAT, new SparseZerosInitStrategy()); 080 params.put(BilinearLearnerParameters.EXPANDEDWINITSTRAT, new SingleValueInitStrat(0.05)); 081 FirstValueInitStrat biasInitStrat = new FirstValueInitStrat(); 082 params.put(BilinearLearnerParameters.BIASINITSTRAT, biasInitStrat); 083 084 List<ServerAddress> serverList = Arrays.asList( 085 new ServerAddress("rumi", 27017), 086 new ServerAddress("hafez", 27017) 087 ); 088 // Get the USMF status objects and financial ticks from the mongodb 089 new USMFTickMongoDBQueryStream(serverList) 090 // Transform the usmf status instances to bags of words 091 .map( 092 new ContextFunctionAdaptor<List<USMFStatus>, Map<String,Map<String,Double>>>( 093 new USMFStatusBagOfWords(new StopwordMode()),"usmfstatuses", 094 "bagofwords" 095 ) 096 ) 097 // transform the financial ticks to the average tick 098 .map( 099 new ContextFunctionAdaptor<List<Map<String,Double>>,Map<String,Double>>(new WindowAverage(),"ticks","averageticks") 100 ) 101 // Group together identical stock ticks 102 .transform(new StockPriceAggregator(0.0001)) 103 // Train the model 104 .map( 105 new IncrementalLearnerWorldSelectingEvaluator( 106 new SumLossEvaluator(), 107 new IncrementalLearnerFunction(params) 108 ) 109 ) 110 // Consume the model statistics 111 .forEach(new Operation<Context>() { 112 113 @Override 114 public void perform(Context context) { 115 ModelStats object = context.getTyped("modelstats"); 116 object.printSummary(); 117 } 118 }); 119 120 } 121}