Topography.java

  1. package org.drip.spaces.graph;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  *
  10.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  11.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  12.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  13.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  14.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  15.  *      and computational support.
  16.  *  
  17.  *      https://lakshmidrip.github.io/DROP/
  18.  *  
  19.  *  DROP is composed of three modules:
  20.  *  
  21.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  22.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  23.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  24.  *
  25.  *  DROP Product Core implements libraries for the following:
  26.  *  - Fixed Income Analytics
  27.  *  - Loan Analytics
  28.  *  - Transaction Cost Analytics
  29.  *
  30.  *  DROP Portfolio Core implements libraries for the following:
  31.  *  - Asset Allocation Analytics
  32.  *  - Asset Liability Management Analytics
  33.  *  - Capital Estimation Analytics
  34.  *  - Exposure Analytics
  35.  *  - Margin Analytics
  36.  *  - XVA Analytics
  37.  *
  38.  *  DROP Computational Core implements libraries for the following:
  39.  *  - Algorithm Support
  40.  *  - Computation Support
  41.  *  - Function Analysis
  42.  *  - Model Validation
  43.  *  - Numerical Analysis
  44.  *  - Numerical Optimizer
  45.  *  - Spline Builder
  46.  *  - Statistical Learning
  47.  *
  48.  *  Documentation for DROP is Spread Over:
  49.  *
  50.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  51.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  52.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  53.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  54.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  55.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  56.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  57.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  58.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  59.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  60.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  61.  *
  62.  *  Licensed under the Apache License, Version 2.0 (the "License");
  63.  *      you may not use this file except in compliance with the License.
  64.  *  
  65.  *  You may obtain a copy of the License at
  66.  *      http://www.apache.org/licenses/LICENSE-2.0
  67.  *  
  68.  *  Unless required by applicable law or agreed to in writing, software
  69.  *      distributed under the License is distributed on an "AS IS" BASIS,
  70.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  71.  *  
  72.  *  See the License for the specific language governing permissions and
  73.  *      limitations under the License.
  74.  */

  75. /**
  76.  * <i>Topography</i> holds Vertexes and the Edges between them. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Wikipedia (2018a): Graph (Abstract Data Type)
  82.  *              https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
  83.  *      </li>
  84.  *      <li>
  85.  *          Wikipedia (2018b): Graph Theory https://en.wikipedia.org/wiki/Graph_theory
  86.  *      </li>
  87.  *      <li>
  88.  *          Wikipedia (2018c): Graph (Discrete Mathematics)
  89.  *              https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)
  90.  *      </li>
  91.  *      <li>
  92.  *          Wikipedia (2018d): Dijkstra's Algorithm https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
  93.  *      </li>
  94.  *      <li>
  95.  *          Wikipedia (2018e): Bellman-Ford Algorithm
  96.  *              https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
  97.  *      </li>
  98.  *  </ul>
  99.  *
  100.  * <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  104.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  105.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/graph/README.md">Graph Representation and Traversal Algorithms</a></li>
  106.  *  </ul>
  107.  * <br><br>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class Topography
  112. {
  113.     private org.drip.spaces.graph.TopographyEdgeMap _topographyEdgeMap = new
  114.         org.drip.spaces.graph.TopographyEdgeMap();

  115.     private java.util.Map<java.lang.String, org.drip.spaces.graph.Vertex> _vertexMap = new
  116.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.spaces.graph.Vertex>();

  117.     /**
  118.      * Empty Topography Constructor
  119.      */

  120.     public Topography()
  121.     {
  122.     }

  123.     /**
  124.      * Retrieve The Vertex Name Set
  125.      *
  126.      * @return The Vertex Name Set
  127.      */

  128.     public java.util.Set<java.lang.String> vertexNameSet()
  129.     {
  130.         return _vertexMap.keySet();
  131.     }

  132.     /**
  133.      * Add The Vertex
  134.      *
  135.      * @param vertexName Name of the Vertex
  136.      *
  137.      * @return TRUE - The Vertex successfully added
  138.      */

  139.     public boolean addVertex (
  140.         final java.lang.String vertexName)
  141.     {
  142.         if (null == vertexName || vertexName.isEmpty())
  143.         {
  144.             return false;
  145.         }

  146.         try
  147.         {
  148.             _vertexMap.put (
  149.                 vertexName,
  150.                 new org.drip.spaces.graph.Vertex (vertexName)
  151.             );
  152.         }
  153.         catch (java.lang.Exception e)
  154.         {
  155.             e.printStackTrace();
  156.         }

  157.         return false;
  158.     }

  159.     /**
  160.      * Verify if the Vertex is available
  161.      *
  162.      * @param vertexName Name of the Vertex
  163.      *
  164.      * @return TRUE - The Vertex is available
  165.      */

  166.     public boolean vertexExists (
  167.         final java.lang.String vertexName)
  168.     {
  169.         return null != vertexName && _vertexMap.containsKey (vertexName);
  170.     }

  171.     /**
  172.      * Retrieve the Named Vertex
  173.      *
  174.      * @param vertexName Name of the Vertex
  175.      *
  176.      * @return The Vertex
  177.      */

  178.     public org.drip.spaces.graph.Vertex vertex (
  179.         final java.lang.String vertexName)
  180.     {
  181.         return vertexExists (vertexName) ? _vertexMap.get (vertexName) : null;
  182.     }

  183.     /**
  184.      * Add an Edge
  185.      *
  186.      * @param edge The Edge to be added
  187.      *
  188.      * @return TRUE - The Edge successfully added
  189.      */

  190.     public boolean addEdge (
  191.         final org.drip.spaces.graph.Edge edge)
  192.     {
  193.         if (null == edge || !_topographyEdgeMap.addEdge (edge))
  194.         {
  195.             return false;
  196.         }

  197.         double weight = edge.weight();

  198.         java.lang.String source = edge.source();

  199.         java.lang.String destination = edge.destination();

  200.         org.drip.spaces.graph.Vertex sourceVertex = vertex (source);

  201.         org.drip.spaces.graph.Vertex destinationVertex = vertex (destination);

  202.         return null != sourceVertex && null != destinationVertex && sourceVertex.addEgress (
  203.             destination,
  204.             weight
  205.         ) && destinationVertex.addEgress (
  206.             source,
  207.             weight
  208.         );
  209.     }

  210.     /**
  211.      * Retrieve the Map of Vertex
  212.      *
  213.      * @return The Map of Vertex
  214.      */

  215.     public java.util.Map<java.lang.String, org.drip.spaces.graph.Vertex> vertexMap()
  216.     {
  217.         return _vertexMap;
  218.     }

  219.     /**
  220.      * Retrieve the Topography Edge Map
  221.      *
  222.      * @return The Topography Edge Map
  223.      */

  224.     public org.drip.spaces.graph.TopographyEdgeMap topographyEdgeMap()
  225.     {
  226.         return _topographyEdgeMap;
  227.     }

  228.     /**
  229.      * Indicate if the Pair of Vertexes are Adjacent
  230.      *
  231.      * @param source The Source Vertex
  232.      * @param destination The Destination Vertex
  233.      *
  234.      * @return TRUE - The Pair of Vertexes are Adjacent
  235.      */

  236.     public boolean adjacent (
  237.         final java.lang.String source,
  238.         final java.lang.String destination)
  239.     {
  240.         org.drip.spaces.graph.Vertex sourceVertex = vertex (source);

  241.         org.drip.spaces.graph.Vertex destinationVertex = vertex (destination);

  242.         if (null == sourceVertex || null == destinationVertex)
  243.         {
  244.             return false;
  245.         }

  246.         return sourceVertex.egressMap().containsKey (destination);
  247.     }

  248.     /**
  249.      * Compute the Weight between Source and Destination if Adjacent
  250.      *
  251.      * @param source The Source Vertex
  252.      * @param destination The Destination Vertex
  253.      *
  254.      * @return The Weight between Source and Destination if Adjacent
  255.      *
  256.      * @throws java.lang.Exception Thrown if the Source and the Destination are not Adjacent
  257.      */

  258.     public double adjacentWeight (
  259.         final java.lang.String source,
  260.         final java.lang.String destination)
  261.         throws java.lang.Exception
  262.     {
  263.         if (!adjacent (
  264.             source,
  265.             destination
  266.         ))
  267.         {
  268.             throw new java.lang.Exception ("Topography::adjacentDistance => Invalid Adjacency Check");
  269.         }

  270.         return vertex (source).egressMap().get (destination);
  271.     }

  272.     /**
  273.      * Generate the Connection Map between valid Pairs of Source and Destination
  274.      *
  275.      * @return The Connection Map
  276.      */

  277.     public java.util.Map<java.lang.String, java.lang.Double> connectionMap()
  278.     {
  279.         java.util.Set<java.lang.String> vertexNameSet = _vertexMap.keySet();

  280.         java.util.Map<java.lang.String, java.lang.Double> connectionMap = new
  281.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  282.         for (String source : vertexNameSet)
  283.         {
  284.             for (String destination : vertexNameSet)
  285.             {
  286.                 try
  287.                 {
  288.                     if (adjacent (
  289.                         source,
  290.                         destination
  291.                     ))
  292.                     {
  293.                         double adjacentWeight = adjacent (
  294.                             source,
  295.                             destination
  296.                         ) ? adjacentWeight (
  297.                             source,
  298.                             destination
  299.                         ) : 0.;

  300.                         connectionMap.put (
  301.                             source + "_" + destination,
  302.                             adjacentWeight
  303.                         );

  304.                         connectionMap.put (
  305.                             destination + "_" + source,
  306.                             adjacentWeight
  307.                         );
  308.                     }
  309.                 }
  310.                 catch (java.lang.Exception e)
  311.                 {
  312.                     e.printStackTrace();
  313.                 }
  314.             }
  315.         }

  316.         return connectionMap;
  317.     }
  318. }