Home /Modules/advantage/SLTradingApproach.sleela

SLTradingApproach.sleela

lib/advantage/trading/SLTradingApproach.sleela · 212 line(s) · sleela

<< lib/advantage/trading/SLTradingApproach.sleela
   SLeeLa Standard Library Definition.
   Definition: One trading approach as a source of leverage: recorded evidence, assumptions
   held or doubted, and the honest score (mean x confidence x trust). >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Counsel building block. Holds observations (reward and baseline), converts an >
< evaluated SLTradingResult into evidence, and scores the approach so it never >
< claims more than its evidence supports: few results earn little confidence, >
< and doubted assumptions discount trust. No evidence is unknown, not zero. >
class SLTradingApproach {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 39014;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-11";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-11";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 7;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }


  << ---- identity and evidence ----
     Each observation is a reward and a baseline; the advantage of one
     observation is reward minus baseline (weight 1). >>
  String id = "";
  String name = "";
  double[] rewards;
  double[] baselines;
  int count = 0;
  int capacity = 0;
  int assumptionsHeld = 0;
  int assumptionsTotal = 0;

  < The held count must be between 0 and the total. Returns false otherwise. >
  boolean configure(String approachId, String approachName, int held, int total) {
    if (held < 0 || total < 0 || held > total) { return false; }
    id = approachId;
    name = approachName;
    assumptionsHeld = held;
    assumptionsTotal = total;
    capacity = 1024;
    rewards = new double[1024];
    baselines = new double[1024];
    count = 0;
    return true;
  }

  String getId() { return id; }
  String getName() { return name; }
  int getCount() { return count; }
  int getAssumptionsHeld() { return assumptionsHeld; }
  int getAssumptionsTotal() { return assumptionsTotal; }

  boolean finiteNumber(double v) { return (v - v) == 0.0; }

  < Record one observation. Rejects non-finite values and a full ledger. >
  boolean addObservation(double reward, double baseline) {
    if (count >= capacity) { return false; }
    if (finiteNumber(reward) == false) { return false; }
    if (finiteNumber(baseline) == false) { return false; }
    rewards[count] = reward;
    baselines[count] = baseline;
    count = count + 1;
    return true;
  }

  << Record an evaluated trade as evidence: reward is the expected proceeds and
     baseline is the cost plus the opportunity cost of capital. With
     perUnitCost true both are divided by the total cost, so trades of very
     different size compare per unit of capital. Returns false (and records
     nothing) for a rejected result, an invalid rate, or a zero-cost trade
     under per-unit scaling. >>
  boolean addTrade(SLTradingResult r, double costOfCapital, boolean perUnitCost) {
    if (r.isOk() == false) { return false; }
    if (r.rateOk(costOfCapital) == false) { return false; }
    double reward = r.getExpectedProceeds();
    double base = r.baseline(costOfCapital);
    if (perUnitCost) {
      double cost = r.getTotalCost();
      if (cost <= 0.0) { return false; }
      reward = reward / cost;
      base = base / cost;
    }
    return addObservation(reward, base);
  }

  < Mean advantage over the evidence; 0.0 when there is none (unknown). >
  double mean() {
    if (count == 0) { return 0.0; }
    double sum = 0.0;
    int i = 0;
    while (i < count) {
      sum = sum + (rewards[i] - baselines[i]);
      i = i + 1;
    }
    return sum / count;
  }

  < Square root by Newton iteration, so no external math library is needed. >
  double sqrtD(double x) {
    if (x <= 0.0) { return 0.0; }
    double g = x;
    if (g < 1.0) { g = 1.0; }
    int i = 0;
    while (i < 100) {
      g = 0.5 * (g + x / g);
      i = i + 1;
    }
    return g;
  }

  < Sample standard deviation of the per-observation advantage; 0.0 below 2. >
  double spread() {
    if (count < 2) { return 0.0; }
    double m = mean();
    double acc = 0.0;
    int i = 0;
    while (i < count) {
      double d = (rewards[i] - baselines[i]) - m;
      acc = acc + d * d;
      i = i + 1;
    }
    return sqrtD(acc / (count - 1));
  }

  << Confidence that the mean deserves trust: n / (n + prior). A few results
     earn little trust. 0.0 with no evidence; 1.0 if prior is not positive. >>
  double confidence(double prior) {
    if (count == 0) { return 0.0; }
    if (prior <= 0.0) { return 1.0; }
    return count / (count + prior);
  }

  < Share of the assumptions still held; 1.0 when none are recorded. >
  double trust() {
    if (assumptionsTotal == 0) { return 1.0; }
    return (assumptionsHeld * 1.0) / assumptionsTotal;
  }

  < score = mean advantage x confidence x trust >
  double score(double prior) {
    return mean() * confidence(prior) * trust();
  }

  < Plain-language reasons the score should be taken with care. >
  String cautions(double prior, int minEvidence) {
    String out = "";
    if (count == 0) {
      out = out + "no evidence yet: advantage is unknown, not zero; ";
    } else {
      if (count < minEvidence) {
        out = out + "thin evidence (" + count + " of " + minEvidence + "): heavily discounted; ";
      }
      if (mean() < 0.0) {
        out = out + "evidence shows a disadvantage on average; ";
      }
      double m = mean();
      if (m < 0.0) { m = 0.0 - m; }
      if (count >= 2 && m > 0.0 && spread() > m) {
        out = out + "inconsistent: spread exceeds the average gain; ";
      }
    }
    if (assumptionsHeld < assumptionsTotal) {
      out = out + (assumptionsTotal - assumptionsHeld) + " assumption(s) doubted: verify before relying; ";
    }
    return out;
  }

  String describe(double prior) {
    return name + " [id=" + id + "] score=" + score(prior)
         + " mean=" + mean() + " spread=" + spread()
         + " confidence=" + confidence(prior) + " (n=" + count + ")"
         + " trust=" + trust() + " (" + assumptionsHeld + "/" + assumptionsTotal + " held)";
  }
}