php/Math   
Recreational Mathematics   
   home  |  library  |  contact
 Math Notes
 Math Programming [25]
 Regression [3]
 Data Mining [17]
 Notation [6]
 Linear Algebra [9]
 Stats & Prob [15]
 Math Cognition [5]
 Space & Physics [6]
 Formulas [5]
 Fun & Games [2]
 Haskell [1]
 Bayes Theory [1]
 Site News [0]
 Math Projects [5]
 Polynomials [1]
 Calculus [9]
 Number Theory [3]
 Optimization [2]
 Financial [1]

 Math Links
 PHP/ir
 Andrew Gelman
 Chance Wiki
 Daniel Lemire
 KD Knuggets
 Social Stats
 MySQL Performance
 Hunch.net
 Matthew Hurst
 JMLR
 JSS
 Hal Daume III
 Math Notes >> Permanent Link

Triangle Inequality Experiments using the Euclidean Metric [Regression
Posted on July 27, 2006 @ 06:34:55 PM by Paul Meagher

Here is a stochoastic proof that the Euclidean metric obeys the triangle inequality.

<?php

include "Array_Distance.php";

function 
randomIntegers($size$lo$hi) {
  
$int_vector = array();
  for (
$i=0$i<$size$i++)
    
$int_vector[] = mt_rand($lo$hi);
  return 
$int_vector;

  
$num_experiments  10;  
$num_dimensions 2;
$num_rule_matches  0;
$num_rule_failures 0;
?>

<h1>Triangle Inequality Experiments using the Euclidean Metric</h1>
<pre>
d(x, z) <= d(x, y) + d(y, z) (triangle inequality)
</pre>

<?php  
for($e=0$e<$num_experiments$e++) {

  
$X randomIntegers($num_dimensions09);
  
$Y randomIntegers($num_dimensions09);
  
$Z randomIntegers($num_dimensions09);

  
$d = new Array_Distance($X$Z);
  
$dXZ $d->euclidean();

  
$d = new Array_Distance($X$Y);
  
$dXY $d->euclidean();

  
$d = new Array_Distance($Y$Z);
  
$dYZ $d->euclidean();
 
  if (
$dYZ <= ($dXY $dYZ)) {
    echo 
"Experiment #".($e+1)." obeys triangle inequality: ";
    echo 
$dYZ."&lt;=(".$dXY." + ".$dYZ.")<br/>"
    
$num_rule_matches++;
  } else {
    echo 
"Does not obey triangle inequality<br />"
    
$num_rule_failures++;
  }  

}
?>

<h1>Results</h1>

<?php  
echo "<b>Num Experiments: </b> $num_experiments <br />";   
echo 
"<b>Num Rule Matches: </b> $num_rule_matches <br />";   
echo 
"<b>Num Rule Failures: </b> $num_rule_failures <br />";     
echo 
"<b>Rule Success Rate: </b> ".(($num_rule_matches/$num_experiments)*100)."%<br />";
?>

You can see the ouput of this script here.

I wonder what would happen if I substituted the minkowski($exp) method for the euclidean() method in the above test_triangle_inequality.php script?

Permalink 

No comments entered ...

 Archive 
 

php/Math Project
© 2011. All rights reserved.