cre_property_matcher 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 617f73f0d0ce70224793a55dc6ac680990d95699
4
+ data.tar.gz: f2a06e739e318e5b83cc6c59bf7f86cdcc3c6469
5
+ SHA512:
6
+ metadata.gz: f23890b70af0eb32b9f7b857d01d0f234e44ea881722d3e3e9deb84e0235d5c20625b70bc1bfa7c089238520e93eb384463fa1d35816fca3b75baa659d60c5c8
7
+ data.tar.gz: 3eb950eabe40c8e9951ec2dcb9a78abd9e976ff2b16876df5f1ca5294899429c317a846c5915fe42a99d1c0bfb3390d021d3819078b1b7fee97d2adefb756654
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cre_property_matcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brad Arner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # CrePropertyMatcher
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cre_property_matcher'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cre_property_matcher
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/cre_property_matcher/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cre_property_matcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cre_property_matcher"
8
+ spec.version = CrePropertyMatcher::VERSION
9
+ spec.authors = ["Brad Arner"]
10
+ spec.email = ["barner@chapelle.us"]
11
+ spec.summary = %q{A basic implementation of the kNN Machine Learning Algorithm for Commercial Real Estate Analysis}
12
+ spec.description = %q{CREPropertyMatcher facilitates the analysis of Commercial Real Estate properties by identifying the most similar properties from a set of training data.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,140 @@
1
+ require "date"
2
+ require File.expand_path("../property_attributes.rb", __FILE__)
3
+
4
+ class Property
5
+ extend PropertyAttributes
6
+
7
+ create_property_attributes
8
+
9
+ def initialize(data)
10
+ @distance = 50000
11
+ if data.class == CSV::Row
12
+ load_from_csv_row(data)
13
+ elsif data.class == Hash
14
+ load_from_hash(data)
15
+ end
16
+ clean_property_types
17
+ clean_money_values
18
+ clean_percentages
19
+ parse_dates
20
+ end
21
+
22
+ def calculate_distance_from_neighbors(neighbors, features, scales)
23
+ neighbors.each do |neighbor|
24
+ values = []
25
+ features.each do |feature|
26
+ neighbor_value = neighbor.instance_variable_get(feature)
27
+ feature_delta = neighbor_value.nil? ? 1 : (neighbor_value - self.instance_variable_get(feature))
28
+ feature_delta = feature_delta / scales[feature][:range].to_f
29
+ value = feature_delta * feature_delta
30
+ values << value
31
+ end
32
+
33
+ unless neighbor.general_property_type == self.general_property_type
34
+ values << 0.005
35
+ end
36
+
37
+ neighbor.distance = Math.sqrt(values.reduce(:+))
38
+ end
39
+ end
40
+
41
+ def distance
42
+ @distance
43
+ end
44
+
45
+ def distance=(value)
46
+ @distance = value
47
+ end
48
+
49
+ def show(features = [:@appraised_value, :@noi, :@ncf, :@occupancy, :@uw_revenue, :@uw_expenses, :@distance, :@general_property_type])
50
+ self.instance_variables.each do |i_var|
51
+ value = instance_variable_get(i_var)
52
+ puts "#{instance_variable_to_string(i_var)}: #{value}" if features.include?(i_var)
53
+ end
54
+ end
55
+
56
+ def load_from_csv_row(data)
57
+ PropertyAttributes.attrs_to_sym_hash.each do |key, value|
58
+ data_point = data[key]
59
+ instance_variable_set(:"@#{value.to_s}", data_point)
60
+ end
61
+ end
62
+
63
+ def load_from_hash(data)
64
+ if validate_hash(data)
65
+ PropertyAttributes.attrs_to_sym_hash.each do |key, value|
66
+ data_point = data[value]
67
+ instance_variable_set(:"@#{value.to_s}", data_point)
68
+ end
69
+ end
70
+ end
71
+
72
+ def clean_property_types
73
+ if @general_property_type.nil?
74
+ return
75
+ elsif @general_property_type.include?("Manufactured")
76
+ @general_property_type = "Manufactured Housing"
77
+ elsif @general_property_type == "Hospitality"
78
+ @general_property_type = "Hotel"
79
+ end
80
+ end
81
+
82
+ def clean_money_values
83
+ attrs = ["noi", "ncf", "loan_amount", "appraised_value", "annual_debt_service", "uw_revenue", "uw_expenses"]
84
+
85
+ attrs.each do |attr|
86
+ value = instance_variable_get(:"@#{attr}")
87
+ instance_variable_set(:"@#{attr}", clean_money(value))
88
+ end
89
+ end
90
+
91
+ def clean_percentages
92
+ attrs = ["ltv", "interest_rate", "occupancy"]
93
+
94
+ attrs.each do |attr|
95
+ value = instance_variable_get(:"@#{attr}")
96
+ instance_variable_set(:"@#{attr}", clean_percentage(value))
97
+ end
98
+ end
99
+
100
+ def parse_dates
101
+ attrs = ["loan_date", "maturity_date", "appraisal_date"]
102
+
103
+ attrs.each do |attr|
104
+ value = instance_variable_get(:"@#{attr}")
105
+ instance_variable_set(:"@#{attr}", date_parsed(value))
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def date_parsed(value)
112
+ Date.strptime(value, "%m/%d/%y") if value
113
+ end
114
+
115
+ def clean_percentage(value)
116
+ return if value.nil?
117
+
118
+ new_value = value.strip.delete("%").to_f
119
+ if new_value > 100.0
120
+ # puts "Property: #{@property_name} says it has a percentage greater than 100% => #{new_value}"
121
+ new_value /= 100.0
122
+ # puts "New Value: #{new_value}"
123
+ end
124
+
125
+ new_value / 100.0
126
+ end
127
+
128
+ def clean_money(value)
129
+ value.strip.delete("$").delete(",").to_i if value
130
+ end
131
+
132
+ def validate_hash(hash)
133
+ true
134
+ end
135
+
136
+ def instance_variable_to_string(instance_variable)
137
+ instance_variable.to_s.delete("@").split("_").map(&:capitalize).join(" ")
138
+ end
139
+ end
140
+
@@ -0,0 +1,50 @@
1
+ module PropertyAttributes
2
+ def self.attrs_to_sym_hash
3
+ {
4
+ "ID" => :id,
5
+ "CIK Number" => :cik_number,
6
+ "Property Name" => :property_name,
7
+ "Address" => :address,
8
+ "City" => :city,
9
+ "State" => :state,
10
+ "Zip Code" => :zip_code,
11
+ "County" => :county,
12
+ "General Property Type" => :general_property_type,
13
+ "Specific Property Type" => :specific_property_type,
14
+ "Year Built" => :year_built,
15
+ "Year Renovated" => :year_renovated,
16
+ "Loan Amount" => :loan_amount,
17
+ "Interest Rate" => :interest_rate,
18
+ "PPP" => :ppp,
19
+ "Loan Date" => :loan_date,
20
+ "Maturity Date" => :maturity_date,
21
+ "Originator" => :orginator,
22
+ "Appraised Value" => :appraised_value,
23
+ "Appraisal Date" => :appraisal_date,
24
+ "LTV" => :ltv,
25
+ "DSCR" => :dscr,
26
+ "NOI" => :noi,
27
+ "NCF" => :ncf,
28
+ "Square Feet" => :square_feet,
29
+ "Title / Collateral" => :title_collateral,
30
+ "Ground Lease Expiration" => :ground_lease_expiration,
31
+ "Number of Units" => :number_of_units,
32
+ "Unit of Measure" => :unit_of_measure,
33
+ "Amortization" => :amortization,
34
+ "Loan Term" => :loan_term,
35
+ "Accrual Type" => :accrual_type,
36
+ "I/O" => :io,
37
+ "I/O Period" => :io_period,
38
+ "Annual Debt Service" => :annual_debt_service,
39
+ "Occupancy %" => :occupancy,
40
+ "U/W Revenue (EGI)" => :uw_revenue,
41
+ "U/W Expenses" => :uw_expenses
42
+ }
43
+ end
44
+
45
+ def create_property_attributes
46
+ PropertyAttributes.attrs_to_sym_hash.each do |key, value|
47
+ attr_accessor value
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,73 @@
1
+ require "csv"
2
+ require File.expand_path("../property.rb", __FILE__)
3
+
4
+ class PropertyDataLoader
5
+
6
+ def initialize(manner = :csv)
7
+ @manner = manner
8
+ @properties = []
9
+ @csv_data = nil
10
+ end
11
+
12
+ def self.run_and_return_properties
13
+ pdl = PropertyDataLoader.new
14
+ pdl.run_and_return_properties
15
+ end
16
+
17
+ def run
18
+ if @manner && @manner == :csv
19
+ load_csv if data.nil?
20
+ csv_to_properties
21
+ end
22
+ end
23
+
24
+ def run_and_return_properties
25
+ run
26
+ property_array
27
+ end
28
+
29
+ def properties
30
+ @properties
31
+ end
32
+
33
+ def property_array
34
+ result = []
35
+ properties.each do |property|
36
+ result << property
37
+ end
38
+ result
39
+ end
40
+
41
+ def add_property(property)
42
+ if property.class == Property
43
+ @properties << property
44
+ else
45
+ raise "Unable to Add Property because it is not of the Property Class"
46
+ end
47
+ end
48
+
49
+ def data
50
+ @csv_data
51
+ end
52
+
53
+ def property_data_path
54
+ File.expand_path("../training_data/property_data.csv", __FILE__)
55
+ end
56
+
57
+ def load_csv
58
+ @csv_data = CSV.read(property_data_path, {headers: true})
59
+ end
60
+
61
+ def csv_to_properties
62
+ data.each do |row|
63
+ property = Property.new(row)
64
+ add_property(property)
65
+ end
66
+ end
67
+ end
68
+
69
+ # l = PropertyDataLoader.new
70
+ # l.run
71
+ # p l.properties
72
+ # p l.properties.first
73
+
@@ -0,0 +1 @@
1
+ ID,CIK Number,Property Name,Address,City,State,Zip Code,County,General Property Type,Specific Property Type,Year Built,Year Renovated,Loan Amount,Interest Rate,PPP,Loan Date,Maturity Date,Originator,Appraised Value,Appraisal Date,LTV,DSCR,NOI,NCF,Square Feet,Title / Collateral,Ground Lease Expiration,Number of Units,Unit of Measure,Borrower,Guarantor,Sponsor,Amortization,Loan Term,Accrual Type,I/O,I/O Period,37,ARD Loan,39,Annual Debt Service,First Payment Date,Balance at Maturity,Lockbox,Occupancy %,Monthly Debt Service,Monthly CapEx,Monthly Tax Escrow,Monthly Insurance Escrow,Monthly TI/LC Escrow,U/W Revenue (EGI),U/W Expenses,Single Tenant,Purpose,Portfolio,Portfolio ID,Related Portfolio,Largest Tenant,Largest Tenant SqFt,Largest Tenant Lease End Date,2nd Largest Tenant,2nd Largest Tenant SqFt,2nd Largest Tenant Lease End Date,Mezz Debt,Mezz Rate,APN,3rd Largest Tenant,3rd Largest Tenant Sq.Ft.,3rd Largest Tenant Lease End Date,4th Largest Tenant,4th Largest Tenant Sq.Ft.,4th Largest Tenant Lease End Date,5th Largest Tenant,5th Largest Tenant Sq. Ft.,5th Largest Tenant Lease End Date,2009 EGI,2009 EXP,2010 EGI,2010 EXP,2011 EGI,2011 EXP,2012 EGI,2012 EXP,2013 EGI,2013 EXP,2014 EGI,2014 EXP,2015 EGI,2015 EXP,2016 EGI,2016 EXP,2017 EGI,2017 EXP,Closing Costs,T-12 Statement Date,T-12 EGI,T-12 EXP
@@ -0,0 +1,3 @@
1
+ module CrePropertyMatcher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,95 @@
1
+ require File.expand_path("../cre_property_matcher/version.rb", __FILE__)
2
+ require File.expand_path("../cre_property_matcher/property_data_loader.rb", __FILE__)
3
+
4
+ module CrePropertyMatcher
5
+ class PropertyMatcher
6
+ attr_accessor :reference_property, :comparison_properties
7
+
8
+ def initialize(reference_property)
9
+ @reference_property = Property.new(reference_property)
10
+ @comparison_properties = PropertyDataLoader.run_and_return_properties
11
+ @features = [:@appraised_value, :@noi, :@ncf, :@uw_revenue, :@uw_expenses]
12
+ @scales = scale_features
13
+ end
14
+
15
+ def reference
16
+ @reference_property
17
+ end
18
+
19
+ def comps
20
+ @comparison_properties
21
+ end
22
+
23
+ def get_suitable_comps
24
+ suitable = []
25
+
26
+ comps.each do |property|
27
+ all_features_not_nil = true
28
+ features.each do |feature|
29
+ unless property.instance_variable_get(feature)
30
+ all_features_not_nil = false
31
+ end
32
+ end
33
+ if all_features_not_nil
34
+ suitable << property
35
+ end
36
+ end
37
+
38
+ suitable
39
+ end
40
+
41
+ def features
42
+ @features
43
+ end
44
+
45
+ def scales
46
+ @scales
47
+ end
48
+
49
+ def head_comparison_properties(n = 6)
50
+ result = []
51
+ 1.upto(n) do |index|
52
+ result << comps[index]
53
+ end
54
+ result
55
+ end
56
+
57
+ def property_type_count
58
+ count_hash = Hash.new(0)
59
+ @comparison_properties.each do |property|
60
+ count_hash[property.general_property_type] += 1
61
+ end
62
+ count_hash
63
+ end
64
+
65
+ def filter_by_feature(feature)
66
+ result = []
67
+ comps.each do |property|
68
+ result << property.instance_variable_get(feature)
69
+ end
70
+ result
71
+ end
72
+
73
+ def scale_features
74
+ scales = {}
75
+ features.each do |feature|
76
+ array = filter_by_feature(feature).compact
77
+ scales[feature] = {min: array.min, max: array.max, range: array.max - array.min}
78
+ end
79
+ scales
80
+ end
81
+
82
+ def calculate_distances
83
+ props = get_suitable_comps
84
+ reference.calculate_distance_from_neighbors(props, features, scales)
85
+ end
86
+
87
+ def sort_comps_by_distance
88
+ comps.sort { |a, b| a.distance <=> b.distance }
89
+ end
90
+
91
+ def get_k_nearest(k = 3)
92
+ sort_comps_by_distance.take(k)
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cre_property_matcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brad Arner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: CREPropertyMatcher facilitates the analysis of Commercial Real Estate
42
+ properties by identifying the most similar properties from a set of training data.
43
+ email:
44
+ - barner@chapelle.us
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - cre_property_matcher.gemspec
55
+ - lib/cre_property_matcher.rb
56
+ - lib/cre_property_matcher/property.rb
57
+ - lib/cre_property_matcher/property_attributes.rb
58
+ - lib/cre_property_matcher/property_data_loader.rb
59
+ - lib/cre_property_matcher/training_data/property_data.csv
60
+ - lib/cre_property_matcher/version.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.14
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A basic implementation of the kNN Machine Learning Algorithm for Commercial
85
+ Real Estate Analysis
86
+ test_files: []