lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Implement loading of XML actuarial tables.


From: Vaclav Slavik
Subject: Re: [lmi] Implement loading of XML actuarial tables.
Date: Wed, 23 May 2012 17:35:21 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20120428 Thunderbird/12.0.1

On 2012-05-17 17:36, Vaclav Slavik wrote:
> It does not yet implement parallel use of both implementations.

The patch below does that.

BTW, I forgot to mention one possible issue with my patches that this
code reminded me off: in LMI sources, actuarial_table is only used
through the actuarial_table_rates() and
actuarial_table_rates_elaborated() functions, never instantiated
directly. In other words, every time an actuarial table is accessed, it
is loaded from the disk, parsed and then promptly discarded again.

Should you encounter performance problems with the XML implementation,
this is a likely cause. I expect loading from XML to be considerably
slower than the old binary format. The fix would be, of course, to keep
parsed actuarial tables around.

Regards,
Vaclav


diff --git a/actuarial_table.cpp b/actuarial_table.cpp
index ac7912f..83319a7 100644
--- a/actuarial_table.cpp
+++ b/actuarial_table.cpp
@@ -903,6 +903,29 @@ std::vector<double> soa_actuarial_table::specific_values
     return v;
 }
 
+namespace
+{
+    inline bool almost_equal_doubles(double a, double b)
+    {
+        return std::abs(a - b) < 0.00000001;
+    }
+
+    bool almost_equal_doubles(std::vector<double> const& a, 
std::vector<double> const& b)
+    {
+        if(a.size() != b.size())
+            return false;
+
+        size_t const size = a.size();
+        for(size_t i = 0; i < size; i++)
+            {
+            if(!almost_equal_doubles(a[i], b[i]))
+                return false;
+            }
+
+        return true;
+    }
+} // Unnamed namespace.
+
 std::vector<double> actuarial_table_rates
     (std::string const& table_filename
     ,int                table_number
@@ -910,8 +933,16 @@ std::vector<double> actuarial_table_rates
     ,int                length
     )
 {
-    actuarial_table z(table_filename, table_number);
-    return z.values(issue_age, length);
+    actuarial_table     z    (table_filename, table_number);
+    soa_actuarial_table z_soa(table_filename, table_number);
+
+    std::vector<double> values    (z.values(issue_age, length));
+    std::vector<double> values_soa(z_soa.values(issue_age, length));
+
+    // TODO !! Temporarily verify correctness of XML implementation,
+    //         remove this once satisfied
+    LMI_ASSERT(almost_equal_doubles(values, values_soa));
+    return values;
 }
 
 std::vector<double> actuarial_table_rates_elaborated
@@ -924,13 +955,27 @@ std::vector<double> actuarial_table_rates_elaborated
     ,int                      reset_duration
     )
 {
-    actuarial_table z(table_filename, table_number);
-    return z.values_elaborated
+    actuarial_table     z    (table_filename, table_number);
+    soa_actuarial_table z_soa(table_filename, table_number);
+
+    std::vector<double> values(z.values_elaborated
         (issue_age
         ,length
         ,method
         ,inforce_duration
         ,reset_duration
-        );
+        ));
+    std::vector<double> values_soa(z_soa.values_elaborated
+        (issue_age
+        ,length
+        ,method
+        ,inforce_duration
+        ,reset_duration
+        ));
+
+    // TODO !! Temporarily verify correctness of XML implementation,
+    //         remove this once satisfied
+    LMI_ASSERT(almost_equal_doubles(values, values_soa));
+    return values;
 }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]