简体   繁体   English

如何在J中编写最大化或最小化函数

[英]How to write maximize or minimize function in J

例如,如果我想使收益函数E [r] = w1r1 + w2r2的期望最大化,并求解权重w1和w2的优化值。

The only constraint that you have really given is that w1+w2=1 您真正给出的唯一约束是w1 + w2 = 1

   w1 =.0.25
   (,~ -.)w1
0.25 0.75

That takes care of both w1 and w2 given the value of w1. 给定w1的值,这将照顾到w1和w2。

r1 r2 +/@:* w1 w2 calculates r1w1 + r2w2 r1 r2 +/@:* w1 w2计算r1w1 + r2w2

   r1 =. 5
   r2 =.10
   (r1,r2) (+/@:* (,-.))w1
8.75
   (r1,r2) (+/@:* (,-.))0.9
5.5
   (r1,r2) (+/@:* (,-.))0.01
9.95

If you really wanted to maximize you would need to add equations for the value of r1 and r2 and take those into account as well, but perhaps I don't understand your question? 如果您真的想最大化,则需要为r1和r2的值添加方程式,并将它们也考虑在内,但是也许我不明白您的问题?

Responding to the comment below: If the constraint of w1+w2=1 still is in play, then the matter just becomes summing the values in r1 and r2, then whichever is bigger should get the w value of 1 and the other will get the w value of 0 回应以下评论:如果w1 + w2 = 1的约束仍在起作用,那么问题就变成了对r1和r2中的值求和,那么取较大者应将w值设为1,另一个取w w值为0

   r1=.2 4 6 3 2
   r2=.2.1 4 6 3 2
   r3=.2 4 6 3 2.3
   r1 (,-.)@:>/@:(+/@:,.) r2
0 1
   r2 (,-.)@:>/@:(+/@:,.) r1
1 0
   r3 (,-.)@:>/@:(+/@:,.) r2
1 0
   'w1 w2'=.r3 (,-.)@:>/@:(+/@:,.) r2
   w1
1
   w2
0
   'w1 w2'=.r1 (,-.)@:>/@:(+/@:,.) r2
   w1
0
   w2
1
   (r1,.r2) +/@:,@:(+ . *) (0 1) NB. w1=.0 w2=.1
17.1
   (r1,.r2) +/@:,@:(+ . *) (1 0) NB. w1=.1 w2=.0
17
   (r1,.r2) +/@:,@:(+ . *) (0.5 0.5) NB. w1=.0.5 w2=.0.5
17.05

Based on the follow up comment below I would approach it in one of two ways. 基于下面的后续评论,我将以两种方式之一进行处理。 I could dig up all my linear programming texts from the 1980's and come up with the definitive mathematical solution (including degenerative cases and local maxima/ minima) or using the same technique as above but for a larger case than n=2. 我可以挖掘出1980年代的所有线性编程文本,并得出确定的数学解决方案(包括退化的情况和局部极大值/极小值),或者使用与上述相同的技术,但对于大于n = 2的情况。 I'm going with the second option. 我要选择第二个选项。

Let's look first at the r matrix which will be a set of constants. 首先让我们看一下r矩阵,它是一组常数。 For this example I am taking a random 5 X 10 matrix with values from 1 to 10. 在此示例中,我采用一个5 X 10随机矩阵,其值从1到10。

   r=. >: ? 5 10 $ 10
   r
 4 4 8  1  4 3 6  9  6 2
 2 6 5  4  4 7 5 10  4 6
 2 4 9 10  1 1 9  8  2 7
 5 6 5  4  7 9 2  6 10 6
10 3 6  2 10 2 7 10  4 2

Now the trick that I am going to use is that I want to find the column with the highest average to be multiplied by the largest value of w. 现在,我要使用的技巧是我想找到平均值最高的列乘以w的最大值。 Easy to do with J using (+/ % #) 使用(+/ % #)轻松实现J

   (+/ % #) r
4.6 4.6 6.6 4.2 5.2 4.4 5.8 8.6 5.2 4.6

Then find the ranking of the list to be able to reorder the columns of the original r matrix. 然后找到列表的排名,以便能够对原始r矩阵的列进行重新排序。 The leading 7 means that 7 { r is the largest average etc. 前导7表示7 { r是最大平均值,等等。

   \:@:(+/ % #) r
7 2 6 4 8 0 1 9 5 3

I use this to in turn reorder the columns of the matrix r using {"1 since I am working columns. The result is that I have reordered the columns of r so that the column with the largest average is on the left and smallest on the right. 由于我正在使用列,因此我使用{“ 1依次对矩阵r的列进行重新排序。结果是,我对r的列进行了重新排序,以使平均数最大的列在左侧,最小的列在对。

   (\:@:(+/ % #) {"1 ]) r
 9 8 6  4  6  4 4 2 3  1
10 5 5  4  4  2 6 6 7  4
 8 9 9  1  2  2 4 7 1 10
 6 5 2  7 10  5 6 6 9  4
10 6 7 10  4 10 3 2 2  2

Once I have that, then the next thing is to develop the w vector. 一旦有了这些,接下来的事情就是开发w向量。 Since I now have all the largest averages on the left I will just maximize the values to the left of w to be as large as possible within the noted constraints. 由于我现在在左侧具有所有最大的平均值,因此我将在已知约束内将w左侧的值最大化,以使其尽可能大。

   w=. 0.2 0.2 0.2 0.2 0.15 0.01 0.01 0.01 0.01 0.01
   #w  NB. w1 through w10 
10
   +/w NB. sum of the values in w
1
   >./w NB. largest value in w
0.2
   <./w NB. smallest value in w
0.01

Because the r matrix has been reordered using + . * 因为r矩阵已使用+ . *重新排序+ . * + . * the dot product gives values for w1r1 , w2r2 , w3r3 ... w10r10 + . *点积给出w1r1 , w2r2 , w3r3 ... w10r10

   (({"1~ \:@: (+/ % #))r) + . * w
1.8 1.6 1.2 0.8 0.9 0.04 0.04 0.02 0.03 0.01
  2   1   1 0.8 0.6 0.02 0.06 0.06 0.07 0.04
1.6 1.8 1.8 0.2 0.3 0.02 0.04 0.07 0.01  0.1
1.2   1 0.4 1.4 1.5 0.05 0.06 0.06 0.09 0.04
  2 1.2 1.4   2 0.6  0.1 0.03 0.02 0.02 0.02

to actually get the weight of the matrix ravel all the values then sum 实际获得矩阵的权重,然后取所有值

   +/ , (({"1~ \:@: (+/ % #))r) + . * w
31.22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM