MODEL: 
! Standard Markowitz portfolio model (PORTCARD);
! Markowitz portfolio model with a constraint on number
  of assets in portfolio;
! Keywords: cardinality constrained, portfolio, 
    Markowitz portfolio;
SETS:
  STOCK/IBM FRD ZRX NSI ATT CTC JNJ DWC FDE ESK
        GEN TXA CLF DNF WEC GEY DUP SRC PEP/:
      AMT, RET, Y;
  COVMAT( STOCK, STOCK) |&1 #LE# &2: VAR;
 ENDSETS
DATA: ! Limit on stocks in portfolio; CARD = 7; ! Covariance matrix, upper diagonal only; VAR = 0.004064680 0.003439530 0.003203930 0.002116880 0.001381650 0.002353230 0.001916220 0.003256370 0.002544590 0.002383810 0.002887290 0.004472970 0.002902590 0.002304500 0.002682240 0.002045070 0.003047140 0.003258580 0.001369250 0.007070440 0.003981340 0.003520640 0.001706900 0.004317010 0.002764900 0.004991320 0.002808120 0.003726680 0.003814030 0.007738510 0.003776950 0.003122860 0.004173460 0.004346690 0.003910890 0.005007890 0.002677750 0.005744010 0.003203780 0.001643180 0.003125480 0.002085010 0.003984630 0.003767400 0.003516540 0.003746540 0.006332450 0.004440430 0.003245360 0.004038360 0.004049050 0.003480960 0.004691960 0.002246420 0.004625460 0.001945410 0.003703080 0.002602810 0.004070610 0.002150870 0.002513920 0.003218120 0.004398120 0.003588330 0.002674290 0.003965950 0.003489410 0.002840460 0.003791750 0.002854320 0.004842380 0.003102680 0.002054850 0.002865670 0.001505270 0.002172290 0.002800070 0.001518810 0.002425250 0.002057520 0.002110640 0.001430200 0.002138360 0.002228550 0.002466280 0.006854380 0.003046500 0.004298120 0.002264900 0.003064420 0.003839620 0.006239590 0.004114960 0.002832290 0.004110480 0.003503340 0.003573900 0.004581830 0.003428120 0.004029080 0.004023870 0.001346270 0.001742420 0.002713990 0.002349640 0.003877040 0.002192430 0.003035420 0.002235720 0.002585360 0.003357620 0.003053590 0.009103180 0.002205030 0.003452860 0.004457430 0.008538380 0.006366680 0.004055270 0.004985980 0.004657420 0.004172860 0.005239350 0.004012840 0.007514730 0.002323960 0.002891550 0.007558630 0.002517370 0.003186680 0.003046420 0.002871730 0.003029500 0.003391190 0.001828020 0.004948470 0.003272530 0.006785470 0.003044590 0.002384300 0.003397950 0.003117500 0.003338180 0.003694680 0.002506060 0.005011960 0.005255750 0.004030640 0.002755130 0.004120510 0.003456820 0.003525110 0.004143540 0.002953680 0.036453330 0.007314810 0.006338060 0.006490000 0.006214920 0.007227070 0.008546670 0.004896310 0.009188330 0.003039890 0.005095290 0.003931010 0.004101560 0.005159870 0.004304820 0.005751880 0.003461210 0.002748460 0.003232600 0.003970630 0.002519210 0.006448760 0.004704100 0.004079440 0.005114590 0.003375800 0.010212690 0.003641470 0.004999030 0.002212880 0.005098730 0.004248160 0.002960600 0.006697270 0.003755290 0.004951300; ! Expected monthly return for each asset; RET= 0.00049 0.02520 0.01408 0.01719 0.02033 0.01440 0.02394 0.03920 0.00840 0.01021 0.01895 0.02010 0.01680 0.01461 0.02360 0.01680 0.02133 0.01040 0.02955; ! Target return per month; TARG = .016; ! Scale factor of objective, suggest 100 for monthly returns; SCALE = 100; ENDDATA !---------------------------------------------------------; ! Minimize variance, with a 2 applied when I < J; [RISK] MIN = SCALE * ( @SUM( STOCK( I): AMT( I) * AMT( I) * VAR( I, I)) + 2 * @SUM( COVMAT( I, J)| I#LT# J: AMT( I) * AMT( J) * VAR( I, J))); ! Use exactly 100% of the starting budget; [BUDGET] @SUM( STOCK: AMT) = 1; ! Required return at end of period; [RETURN] @SUM( STOCK: AMT * RET) > TARG; ! Cardinality constraints; @FOR( STOCK( I): @BIN( Y( I)); AMT( I) <= Y( I); ); @SUM( STOCK( I): Y( I)) <= CARD; END