You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by mi...@TaosNet.com on 2008/09/17 11:30:08 UTC

[math] help writing a solver for equation

I have an equation like so:
z = Sum of a = 1 to n [ e**(y - x(a))/ e**(x(a)]
I want to solve for y.
I have z and x(a).

I have created a class EquationSolver which has these methods and inner
class:

private EstimatedParameter xa;

public VintageQualityFitter() {

  xa = new EstimatedParameter("xa", 0.0);

  // provide the parameters to the base class which
  // implements the getAllParameters and getUnboundParameters methods
  addParameter(xa);
}

public double theoreticalValue(double z) {
  double summand = ( Math.exp(y * xa.getEstimate()) /
Math.exp(xa.getEstimate());
  return summand;
}

public void addPoint(double xa) {
  addMeasurement(new LocalMeasurement(xa, 1.0)); // all weights same
}

public double getXa() {
  return xa.getEstimate();
}

private class LocalMeasurement extends WeightedMeasurement {

  private final double z;

  // constructor
  public LocalMeasurement(double xa, double weight) {
     super(xa, weight);
     this.z = xa;
  }

  public double getTheoreticalValue() {
    // the value is provided by the model for the local xa
    return theoreticalValue(z);
  }

// don't need this i think
  public double getPartial(EstimatedParameter parameter) {
    return 0.0;
  }
}

I use this like so:

EquationFitter equationFitter = new EquationFitter();

for (int a = 0; a <= numberOfAs; a++) {
  double xa = <get x(a)>
  equationFitter.addPoint(xa.doubleValue());
 }

try {
    // solve the problem, using a Levenberg-Marquardt algorithm with
default settings
	LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator();
    estimator.estimate(equationFitter);
} catch (EstimationException e) {
	e.printStackTrace();
}

// fitter.theoreticalValue() is alpha
<get some z>
y = equationFitter.theoreticalValue(z);

but something in all this isn't right, especially with theoreticalValue(),
i think.
Can anyone help?


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] help writing a solver for equation

Posted by mi...@TaosNet.com.
There is one z and some number of x(a)s

You may be right in that I am using the wrong thing, that i need to use
UnivariateRealSolver

(that I need to use
> mickeydog@TaosNet.com a écrit :
>> I have an equation like so:
>> z = Sum of a = 1 to n [ e**(y - x(a))/ e**(x(a)]
>> I want to solve for y.
>> I have z and x(a).
>>
>> I have created a class EquationSolver which has these methods and inner
>> class:
>>
>> private EstimatedParameter xa;
>>
>> public VintageQualityFitter() {
>>
>>   xa = new EstimatedParameter("xa", 0.0);
>
> If you want to solve for y and know all x(a), your parameter should be y.
>
>>
>>   // provide the parameters to the base class which
>>   // implements the getAllParameters and getUnboundParameters methods
>>   addParameter(xa);
>> }
>>
>> public double theoreticalValue(double z) {
>>   double summand = ( Math.exp(y * xa.getEstimate()) /
>> Math.exp(xa.getEstimate());
>>   return summand;
>> }
>
> the name "z" is confusing there, I don't understand if you have several
> x(a) and only one value z which is a sum (as seemed to be implied by the
> start of your message) or if you have one z for each x(a) as seemed to
> be implied by the previous method.
>
> If you have only one z, then the estimation algorithm is not what you
> need. You need either to rewrite your equation to solve it (has Ted
> proposed) or you need to use a UnivariateRealSolver from package
> analysis, and use a simple loop to compute your function for the set of
> x(a) you know.
>
>>
>> public void addPoint(double xa) {
>>   addMeasurement(new LocalMeasurement(xa, 1.0)); // all weights same
>> }
>>
>> public double getXa() {
>>   return xa.getEstimate();
>> }
>>
>> private class LocalMeasurement extends WeightedMeasurement {
>>
>>   private final double z;
>>
>>   // constructor
>>   public LocalMeasurement(double xa, double weight) {
>
>   This xa seems strange to me, isn't it rather a "z", or is it linked to
> a "z" ?
>
> Luc
>
>>      super(xa, weight);
>>      this.z = xa;
>>   }
>>
>>   public double getTheoreticalValue() {
>>     // the value is provided by the model for the local xa
>>     return theoreticalValue(z);
>>   }
>>
>> // don't need this i think
>>   public double getPartial(EstimatedParameter parameter) {
>>     return 0.0;
>>   }
>> }
>>
>> I use this like so:
>>
>> EquationFitter equationFitter = new EquationFitter();
>>
>> for (int a = 0; a <= numberOfAs; a++) {
>>   double xa = <get x(a)>
>>   equationFitter.addPoint(xa.doubleValue());
>>  }
>>
>> try {
>>     // solve the problem, using a Levenberg-Marquardt algorithm with
>> default settings
>> 	LevenbergMarquardtEstimator estimator = new
>> LevenbergMarquardtEstimator();
>>     estimator.estimate(equationFitter);
>> } catch (EstimationException e) {
>> 	e.printStackTrace();
>> }
>>
>> // fitter.theoreticalValue() is alpha
>> <get some z>
>> y = equationFitter.theoreticalValue(z);
>>
>> but something in all this isn't right, especially with
>> theoreticalValue(),
>> i think.
>> Can anyone help?
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] help writing a solver for equation

Posted by mi...@TaosNet.com.
Are there examples of how to use the UnivariateRealSolvers?
or better yet, can you sketch how I would approach with my equation?

> mickeydog@TaosNet.com a écrit :
>> I have an equation like so:
>> z = Sum of a = 1 to n [ e**(y - x(a))/ e**(x(a)]
>> I want to solve for y.
>> I have z and x(a).
>>
>> I have created a class EquationSolver which has these methods and inner
>> class:
>>
>> private EstimatedParameter xa;
>>
>> public VintageQualityFitter() {
>>
>>   xa = new EstimatedParameter("xa", 0.0);
>
> If you want to solve for y and know all x(a), your parameter should be y.
>
>>
>>   // provide the parameters to the base class which
>>   // implements the getAllParameters and getUnboundParameters methods
>>   addParameter(xa);
>> }
>>
>> public double theoreticalValue(double z) {
>>   double summand = ( Math.exp(y * xa.getEstimate()) /
>> Math.exp(xa.getEstimate());
>>   return summand;
>> }
>
> the name "z" is confusing there, I don't understand if you have several
> x(a) and only one value z which is a sum (as seemed to be implied by the
> start of your message) or if you have one z for each x(a) as seemed to
> be implied by the previous method.
>
> If you have only one z, then the estimation algorithm is not what you
> need. You need either to rewrite your equation to solve it (has Ted
> proposed) or you need to use a UnivariateRealSolver from package
> analysis, and use a simple loop to compute your function for the set of
> x(a) you know.
>
>>
>> public void addPoint(double xa) {
>>   addMeasurement(new LocalMeasurement(xa, 1.0)); // all weights same
>> }
>>
>> public double getXa() {
>>   return xa.getEstimate();
>> }
>>
>> private class LocalMeasurement extends WeightedMeasurement {
>>
>>   private final double z;
>>
>>   // constructor
>>   public LocalMeasurement(double xa, double weight) {
>
>   This xa seems strange to me, isn't it rather a "z", or is it linked to
> a "z" ?
>
> Luc
>
>>      super(xa, weight);
>>      this.z = xa;
>>   }
>>
>>   public double getTheoreticalValue() {
>>     // the value is provided by the model for the local xa
>>     return theoreticalValue(z);
>>   }
>>
>> // don't need this i think
>>   public double getPartial(EstimatedParameter parameter) {
>>     return 0.0;
>>   }
>> }
>>
>> I use this like so:
>>
>> EquationFitter equationFitter = new EquationFitter();
>>
>> for (int a = 0; a <= numberOfAs; a++) {
>>   double xa = <get x(a)>
>>   equationFitter.addPoint(xa.doubleValue());
>>  }
>>
>> try {
>>     // solve the problem, using a Levenberg-Marquardt algorithm with
>> default settings
>> 	LevenbergMarquardtEstimator estimator = new
>> LevenbergMarquardtEstimator();
>>     estimator.estimate(equationFitter);
>> } catch (EstimationException e) {
>> 	e.printStackTrace();
>> }
>>
>> // fitter.theoreticalValue() is alpha
>> <get some z>
>> y = equationFitter.theoreticalValue(z);
>>
>> but something in all this isn't right, especially with
>> theoreticalValue(),
>> i think.
>> Can anyone help?
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] help writing a solver for equation

Posted by Luc Maisonobe <Lu...@free.fr>.
mickeydog@TaosNet.com a écrit :
> I have an equation like so:
> z = Sum of a = 1 to n [ e**(y - x(a))/ e**(x(a)]
> I want to solve for y.
> I have z and x(a).
> 
> I have created a class EquationSolver which has these methods and inner
> class:
> 
> private EstimatedParameter xa;
> 
> public VintageQualityFitter() {
> 
>   xa = new EstimatedParameter("xa", 0.0);

If you want to solve for y and know all x(a), your parameter should be y.

> 
>   // provide the parameters to the base class which
>   // implements the getAllParameters and getUnboundParameters methods
>   addParameter(xa);
> }
> 
> public double theoreticalValue(double z) {
>   double summand = ( Math.exp(y * xa.getEstimate()) /
> Math.exp(xa.getEstimate());
>   return summand;
> }

the name "z" is confusing there, I don't understand if you have several
x(a) and only one value z which is a sum (as seemed to be implied by the
start of your message) or if you have one z for each x(a) as seemed to
be implied by the previous method.

If you have only one z, then the estimation algorithm is not what you
need. You need either to rewrite your equation to solve it (has Ted
proposed) or you need to use a UnivariateRealSolver from package
analysis, and use a simple loop to compute your function for the set of
x(a) you know.

> 
> public void addPoint(double xa) {
>   addMeasurement(new LocalMeasurement(xa, 1.0)); // all weights same
> }
> 
> public double getXa() {
>   return xa.getEstimate();
> }
> 
> private class LocalMeasurement extends WeightedMeasurement {
> 
>   private final double z;
> 
>   // constructor
>   public LocalMeasurement(double xa, double weight) {

  This xa seems strange to me, isn't it rather a "z", or is it linked to
a "z" ?

Luc

>      super(xa, weight);
>      this.z = xa;
>   }
> 
>   public double getTheoreticalValue() {
>     // the value is provided by the model for the local xa
>     return theoreticalValue(z);
>   }
> 
> // don't need this i think
>   public double getPartial(EstimatedParameter parameter) {
>     return 0.0;
>   }
> }
> 
> I use this like so:
> 
> EquationFitter equationFitter = new EquationFitter();
> 
> for (int a = 0; a <= numberOfAs; a++) {
>   double xa = <get x(a)>
>   equationFitter.addPoint(xa.doubleValue());
>  }
> 
> try {
>     // solve the problem, using a Levenberg-Marquardt algorithm with
> default settings
> 	LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator();
>     estimator.estimate(equationFitter);
> } catch (EstimationException e) {
> 	e.printStackTrace();
> }
> 
> // fitter.theoreticalValue() is alpha
> <get some z>
> y = equationFitter.theoreticalValue(z);
> 
> but something in all this isn't right, especially with theoreticalValue(),
> i think.
> Can anyone help?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] help writing a solver for equation

Posted by Ted Dunning <te...@gmail.com>.
Seems to me that a little bit of math would go a long way here.

z &= \sum_{a = 1}^n \left[ e^{y - x(a)}/ e^{x(a)} \right] \\
z &= \sum_{a = 1}^n \left[ e^{y - 2 x(a)} \right] \\
z &= \sum_{a = 1}^n \left[ e^y e^{ - 2 x(a)} \right]  \\
z &= e^y \sum_{a = 1}^n e^{ - 2 x(a) } \\
y &= \log \left[ z  { \sum_{a = 1}^n  e^{2 x(a) } } \right]

Perhaps you lost something in translation somewhere?  Surely, you problem
isn't this easy.

Also, in your code, it looks like your original equation is different from
what you said in your problem statement.  Still easy to solve, but very
different.

Finally, I don't see anyplace in your code that you connected your equation
to solve with the solver.

On Wed, Sep 17, 2008 at 2:30 AM, <mi...@taosnet.com> wrote:

> I have an equation like so:
> z = Sum of a = 1 to n [ e**(y - x(a))/ e**(x(a)]
> I want to solve for y.
> I have z and x(a).
>
> I have created a class EquationSolver which has these methods and inner
> class:
>
> private EstimatedParameter xa;
>
> public VintageQualityFitter() {
>
>  xa = new EstimatedParameter("xa", 0.0);
>
>  // provide the parameters to the base class which
>  // implements the getAllParameters and getUnboundParameters methods
>  addParameter(xa);
> }
>
> public double theoreticalValue(double z) {
>  double summand = ( Math.exp(y * xa.getEstimate()) /
> Math.exp(xa.getEstimate());
>  return summand;
> }
>
> public void addPoint(double xa) {
>  addMeasurement(new LocalMeasurement(xa, 1.0)); // all weights same
> }
>
> public double getXa() {
>  return xa.getEstimate();
> }
>
> private class LocalMeasurement extends WeightedMeasurement {
>
>  private final double z;
>
>  // constructor
>  public LocalMeasurement(double xa, double weight) {
>     super(xa, weight);
>     this.z = xa;
>  }
>
>  public double getTheoreticalValue() {
>    // the value is provided by the model for the local xa
>    return theoreticalValue(z);
>  }
>
> // don't need this i think
>  public double getPartial(EstimatedParameter parameter) {
>    return 0.0;
>  }
> }
>
> I use this like so:
>
> EquationFitter equationFitter = new EquationFitter();
>
> for (int a = 0; a <= numberOfAs; a++) {
>  double xa = <get x(a)>
>  equationFitter.addPoint(xa.doubleValue());
>  }
>
> try {
>    // solve the problem, using a Levenberg-Marquardt algorithm with
> default settings
>        LevenbergMarquardtEstimator estimator = new
> LevenbergMarquardtEstimator();
>    estimator.estimate(equationFitter);
> } catch (EstimationException e) {
>        e.printStackTrace();
> }
>
> // fitter.theoreticalValue() is alpha
> <get some z>
> y = equationFitter.theoreticalValue(z);
>
> but something in all this isn't right, especially with theoreticalValue(),
> i think.
> Can anyone help?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


-- 
ted