Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
corrected compile error
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
examples/undocumented/python_modular/statistics_quadratic_time_mmd.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# | ||
# This program is free software you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Written (C) 2012 Heiko Strathmann | ||
# | ||
from numpy import * | ||
from tools.two_distributions_data import TwoDistributionsData | ||
|
||
gen_data=TwoDistributionsData() | ||
|
||
def statistics_linear_time_mmd(): | ||
from shogun.Features import RealFeatures | ||
from shogun.Kernel import GaussianKernel | ||
from shogun.Statistics import QuadraticTimeMMD | ||
from shogun.Statistics import BOOTSTRAP | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
# note that the quadratic time mmd has sometimes to store kernel matrices | ||
# which upper bounds the sample size massively | ||
n=500 | ||
dim=2 | ||
difference=0.5 | ||
|
||
# data is standard normal distributed. only one dimension of Y has a mean | ||
# shift of difference | ||
(X,Y)=gen_data.create_mean_data(n,dim,difference) | ||
|
||
print "dimension means of X", [mean(x) for x in X] | ||
print "dimension means of Y", [mean(x) for x in Y] | ||
|
||
# create shogun feature representation | ||
features_x=RealFeatures(X) | ||
features_y=RealFeatures(Y) | ||
|
||
# use a kernel width of sigma=2, which is 8 in SHOGUN's parametrization | ||
# which is k(x,y)=exp(-||x-y||^2 / tau), in constrast to the standard | ||
# k(x,y)=exp(-||x-y||^2 / (2*sigma^2)), so tau=2*sigma^2 | ||
kernel=GaussianKernel(10,8) | ||
|
||
mmd=QuadraticTimeMMD(kernel,features_x, features_y) | ||
|
||
# perform test: compute p-value and test if null-hypothesis is rejected for | ||
# a test level of 0.05 | ||
statistic=mmd.compute_statistic() | ||
|
||
p_value=mmd.compute_p_value(statistic) | ||
alpha=0.05 | ||
print "p_value:", p_value | ||
print "p_value <", alpha, ", i.e. test sais p!=q:", p_value<alpha | ||
|
||
# sample from null distribution (these may be plotted or whatsoever) | ||
# mean should be close to zero, variance stronly depends on data/kernel | ||
mmd.set_null_approximation_method(BOOTSTRAP) | ||
mmd.set_bootstrap_iterations(100) | ||
null_samples=mmd.bootstrap_null() | ||
print "null mean:", mean(null_samples) | ||
print "null variance:", var(null_samples) | ||
|
||
if __name__=='__main__': | ||
print('LinearTimeMMD') | ||
statistics_linear_time_mmd() |