Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Chance-core: initial continuous distribution support. Gaussian PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
sotty committed Jun 8, 2013
1 parent 6ba168a commit 0587c5c
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 4 deletions.
Expand Up @@ -38,8 +38,6 @@
public class Chance {




public static void initialize() {

ChanceStrategyFactory.initDefaults();
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.drools.chance.common;

import org.drools.chance.distribution.probability.gaussian.GaussianDistributionStrategyFactory;
import org.drools.chance.rule.constraint.core.connectives.ConnectiveFactory;
import org.drools.chance.rule.constraint.core.connectives.factories.fuzzy.linguistic.FuzzyConnectiveFactory;
import org.drools.chance.rule.constraint.core.connectives.factories.fuzzy.mvl.ManyValuedConnectiveFactory;
Expand Down Expand Up @@ -76,12 +77,18 @@ public class ChanceStrategyFactory<T> {
cacheConnective.put(
new Pair<ImpKind, ImpType>( ImpKind.FUZZINESS, ImpType.MVL ),
new ManyValuedConnectiveFactory() );

cacheConnective.put(
new Pair<ImpKind, ImpType>( ImpKind.FUZZINESS, ImpType.BASIC ),
new ManyValuedConnectiveFactory() );

cacheConnective.put(
new Pair<ImpKind, ImpType>( ImpKind.PROBABILITY, ImpType.BASIC ),
new ManyValuedConnectiveFactory() );

cacheConnective.put(
new Pair<ImpKind, ImpType>( ImpKind.PROBABILITY, ImpType.GAUSSIAN ),
new ManyValuedConnectiveFactory() );
}

public static void registerConnectiveFactory( ImpKind kind, ImpType model, ConnectiveFactory connFac ) {
Expand Down Expand Up @@ -181,6 +188,13 @@ public static void initDefaults() {
e.printStackTrace();
}

try {
DistributionStrategyFactory factory8 = (DistributionStrategyFactory) Class.forName( "org.drools.chance.distribution.probability.gaussian.GaussianDistributionStrategyFactory" ).newInstance();
ChanceStrategyFactory.register( factory8.getImp_Kind(), factory8.getImp_Model(), factory8 );
} catch ( Exception e ) {
e.printStackTrace();
}

ChanceDegreeTypeRegistry.getSingleInstance().registerDegreeType( DegreeType.SIMPLE, SimpleDegree.class);

ChanceDegreeTypeRegistry.getSingleInstance().registerDegreeType( DegreeType.INTERVAL, IntervalDegree.class);
Expand Down
Expand Up @@ -16,6 +16,10 @@

package org.drools.chance.distribution;

import org.drools.chance.degree.Degree;

public interface ContinuousDomainDistribution<T> {

public Degree getCumulative( T object );

}
Expand Up @@ -16,7 +16,11 @@

package org.drools.chance.distribution;

import org.drools.chance.degree.Degree;

import java.util.Set;

public interface ContinuousProbabilityDistribution<T> extends
DiscreteDomainDistribution<T>, ProbabilityDistribution<T> {
ContinuousDomainDistribution<T>, ProbabilityDistribution<T> {

}
Expand Up @@ -23,6 +23,7 @@ public enum ImpType {
TBM ("TBM"),
DISCRETE ("discrete"),
DIRICHLET ("dirichlet"),
GAUSSIAN ("gaussian"),
BASIC ("basic"),
MVL ("many-valued");

Expand Down
Expand Up @@ -78,4 +78,8 @@ public boolean isNormalized() {
public void setNormalized(boolean normalized) {
this.normalized = normalized;
}

public Degree getCumulative( Number object ) {
return null;
}
}
@@ -0,0 +1,103 @@
package org.drools.chance.distribution.probability.gaussian;

import org.drools.chance.degree.Degree;
import org.drools.chance.distribution.ContinuousProbabilityDistribution;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

public class GaussianDistribution<T extends Number> implements ContinuousProbabilityDistribution<T> {

private Degree falze;
private Double mu;
private Double sigma;

public GaussianDistribution( Double mean, Double stdev, Degree f ) {
mu = mean;
sigma = stdev;
falze = f;
}

public Double getMu() {
return mu;
}

public void setMu( Double mu ) {
this.mu = mu;
}

public Double getSigma() {
return sigma;
}

public void setSigma( Double sigma ) {
this.sigma = sigma;
}

public Set<T> getSupport() {
return new RealNumberSet();
}

public Degree getDegree( T value ) {
return falze;
}

public Degree get( T object ) {
return falze;
}

public Number domainSize() {
return Double.POSITIVE_INFINITY;
}

public boolean isDiscrete() {
return false;
}

public boolean isNormalized() {
return true;
}

public void setNormalized( boolean normalized ) {

}

public int size() {
return Integer.MAX_VALUE;
}

public Iterator<T> iterator() {
return Collections.EMPTY_SET.iterator();
}

public Degree getCumulative( T object ) {
double d = object.doubleValue();
return falze.fromConst( Phi( d, mu, sigma ) );
}

public static double phi( double x ) {
return Math.exp( -x * x / 2 ) / Math.sqrt( 2 * Math.PI );
}

public static double Phi( double x ) {
if ( x < -8.0 ) {
return 0.0;
}
if ( x > 8.0 ) {
return 1.0;
}
double sum = 0.0;
double term = x;
for ( int i = 3; sum + term != sum; i += 2 ) {
sum = sum + term;
term = term * x * x / i;
}
return 0.5 + sum * phi( x );
}

public static double Phi( double x, double mu, double sigma ) {
return Phi( ( x - mu ) / sigma );
}
}
@@ -0,0 +1,182 @@
/*
* Copyright 2011 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.chance.distribution.probability.gaussian;

import org.drools.chance.degree.ChanceDegreeTypeRegistry;
import org.drools.chance.degree.Degree;
import org.drools.chance.degree.DegreeType;
import org.drools.chance.degree.simple.SimpleDegree;
import org.drools.chance.distribution.DiscreteProbabilityDistribution;
import org.drools.chance.distribution.Distribution;
import org.drools.chance.distribution.DistributionStrategies;
import org.drools.chance.distribution.probability.discrete.DiscreteDistribution;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;


/**
* Strategy and level III factory for discrete probability distributions
*/
public class GaussianDistributionStrategy implements DistributionStrategies<Double> {



private DegreeType degreeType;
private Class<Double> domainType;

private Constructor degreeStringConstr = null;



GaussianDistributionStrategy( DegreeType degreeType, Class<Double> domainType ){
this.degreeType = degreeType;
this.domainType = domainType;
}


private Constructor getDegreeStringConstructor() {
if (degreeStringConstr == null) {
degreeStringConstr = ChanceDegreeTypeRegistry.getSingleInstance().getConstructorByString(degreeType);
}
return degreeStringConstr;
}


public Distribution<Double> toDistribution( Double value ) {
return new GaussianDistribution( value.doubleValue(), 1.0, ChanceDegreeTypeRegistry.getSingleInstance().buildDegree( degreeType, 0.0 ) );
}

public Distribution<Double> toDistribution( Double value, String strategy ) {
return new GaussianDistribution( value.doubleValue(), 1.0, ChanceDegreeTypeRegistry.getSingleInstance().buildDegree( degreeType, 0.0 ) );
}

public Distribution<Double> toDistribution( Double value, Object... params ) {
return new GaussianDistribution( value.doubleValue(),
Double.valueOf( params[ 0 ].toString() ),
ChanceDegreeTypeRegistry.getSingleInstance().buildDegree( degreeType, 0.0 ) );
}

public Distribution<Double> parse( String distrAsString ) {
if ( distrAsString.startsWith( "N" ) ) {
StringTokenizer tok = new StringTokenizer( distrAsString.substring( 1 ), "(,) " );
return new GaussianDistribution( tok.hasMoreTokens() ? Double.valueOf( tok.nextToken() ) : 0.0,
tok.hasMoreTokens() ? Double.valueOf( tok.nextToken() ) : 1.0,
ChanceDegreeTypeRegistry.getSingleInstance().buildDegree( degreeType, 0.0 ) );
} else {
return newDistribution();
}
}

public Distribution<Double> newDistribution() {
return new GaussianDistribution( 0.0, 1.0, ChanceDegreeTypeRegistry.getSingleInstance().buildDegree( degreeType, 0.0 ) );
}

public Distribution<Double> newDistribution( Set<Double> focalElements ) {
Iterator<Double> iter = focalElements.iterator();
return new GaussianDistribution( iter.next().doubleValue(), iter.next().doubleValue(), ChanceDegreeTypeRegistry.getSingleInstance().buildDegree( degreeType, 0.0 ) );
}

public Distribution<Double> newDistribution( Map<? extends Double, ? extends Degree> elements ) {
throw new UnsupportedOperationException( "Build Gaussian with map " );
}

public Double toCrispValue( Distribution<Double> dist ) {
return ((GaussianDistribution) dist).getMu();
}

public Double toCrispValue( Distribution<Double> dist, String strategy ) {
return ((GaussianDistribution) dist).getMu();
}

public Double toCrispValue( Distribution<Double> dist, Object... params ) {
return ((GaussianDistribution) dist).getMu();
}



public Double sample( Distribution<Double> dist ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Double sample( Distribution<Double> dist, String strategy ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Double sample( Distribution<Double> dist, Object... params ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> merge( Distribution<Double> current, Distribution<Double> newBit ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> merge( Distribution<Double> current, Distribution<Double> newBit, String strategy ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> merge( Distribution<Double> current, Distribution<Double> newBit, Object... params ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> mergeAsNew( Distribution<Double> current, Distribution<Double> newBit ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> mergeAsNew( Distribution<Double> current, Distribution<Double> newBit, String strategy ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> mergeAsNew( Distribution<Double> current, Distribution<Double> newBit, Object... params ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> remove( Distribution<Double> current, Distribution<Double> newBit ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> remove( Distribution<Double> current, Distribution<Double> newBit, String strategy ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> remove( Distribution<Double> current, Distribution<Double> newBit, Object... params ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> removeAsNew( Distribution<Double> current, Distribution<Double> newBit ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> removeAsNew( Distribution<Double> current, Distribution<Double> newBit, String strategy ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Distribution<Double> removeAsNew( Distribution<Double> current, Distribution<Double> newBit, Object... params ) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public void normalize( Distribution<Double> distr ) {
//To change body of implemented methods use File | Settings | File Templates.
}
}

0 comments on commit 0587c5c

Please sign in to comment.